Binary Tree Desctructor

I'm using a structure called tree like this...\

1
2
3
4
5
6
7
8
struct tree
{
    int grade;
    char name[20];
    tree* right;
    tree* left;
    tree(tree*, tree*, int, char*);
};


Then, I'm using a class to do some operations with the tree I'll be working with like this...

1
2
3
4
5
6
7
class Ctree
{
public:
     Ctree();
     ~Ctree();
...
}


Right now it may all seem confusing but I can't seem to figure out how to write a proper destructor for my dynamically allocated nodes. I can't figure out the logic. I can't add any other structure and class member functions other than the ones provided for me which are...

for the structure, 1) 4 argument constructure.
for the class, 1) constructor, 2) destructor, 3) adding a node, 4) viewing the tree, 5) adding an item (for recursion). As stated before, I can't add any other member functions, therefore, recursion seems to be out of the question + I can't give my destructor any arguments. I've been trying at it for a loooong time now and I don't seem to get it right. Please help me...
A useful definition of a binary tree is:
a node pointer points to
1. null
2. a node with two node pointers

If you want to create a binary tree class, it needs to respect this definition. I would expect the implementation to hold just the root node pointer. The constructor initialises it to null. The destructor traverses the tree and deletes each node after the left and right nodes are deleted. The view and add algorithms are standard, you can search for those.
http://en.wikipedia.org/wiki/Binary_tree#Definitions_for_rooted_trees
Last edited on
Topic archived. No new replies allowed.