Hello,
I'm working through an example from the book Data Structures and Algorithms for Game Developers. I've reached a brick wall and suspect an issues with the book's source code. Dealing with trees, we have the following data structure and implementation:
#include<iostream>
using namespace std;
class Node
{
public:
Node(int obj) : m_object(obj), m_next(NULL),
m_prev(NULL), m_child(NULL)
{
cout << "Node created!" << endl;
}
~Node()
{
m_prev = NULL;
if(m_child != NULL)
delete m_child;
if(m_next != NULL)
delete m_next;
m_child = NULL;
m_next = NULL;
cout << "Node deleted!" << endl;
}
void AddChild(Node *node)
{
if(m_child == NULL)
m_child = node;
else
m_child->AddSibling(node);
}
void AddSibling(Node *node)
{
Node *ptr = m_next;
if(m_next == NULL)
{
m_next = node;
node->m_prev = this;
}
else
{
while(ptr->m_next != NULL)
ptr = ptr->m_next;
ptr->m_next = node;
node->m_prev = ptr;
}
}
void DisplayTree()
{
cout << m_object;
if(m_next != NULL)
{
cout << " ";
m_next->DisplayTree();
}
if(m_child != NULL)
{
cout << endl;
m_child->DisplayTree();
}
}
bool Search(int value)
{
if(m_object == value)
return true;
if(m_child != NULL)
{
if(m_child->Search(value) == true)
return true;
}
if(m_next != NULL)
{
if(m_next->Search(value) == true)
return true;
}
return false;
}
private:
int m_object;
Node *m_next, *m_prev, *m_child;
};
int main(int args, char *arg[])
{
cout << "Simple Tree Data Structure" << endl;
cout << "Chapter 9: Trees" << endl << endl;
// Manually create the tree...
Node *root = new Node(1);
Node *subTree1 = new Node(3);
root->AddChild(new Node(2));
subTree1->AddChild(new Node(5));
subTree1->AddChild(new Node(6));
root->AddChild(subTree1);
root->AddChild(new Node(4));
cout << endl;
// Display the tree...
cout << "Tree contents by level:" << endl;
root->DisplayTree();
cout << endl << endl;
delete subTree1;
root->DisplayTree();
cout << endl << endl;
return 1;
}
The issue is with deleting the "subTree1". When I do this, and then call DisplayTree() on the root node, I get an infinite loop. Any suggestions on what the issue is? I would expect calling delete on the subTree1 would remove that node and any sub nodes, but clearly this is not the case. Please help!