binary Tree class

This is a bit long, sorry about that but I've been writing this tree class and I can't get it to construct properly. I've run the debugger and believe the problem is due to the copy constructor not copying the correct values when I try to make a new tree.


I may also have a problem with the destructor, the one the teacher defined is in the header and just deletes the root. I think it may be clashing with my code. So far I have been unable to fix the problem. Are the constructors correct for the Tree class or do I need to do a bit more reading? I posted this both for outside opinions and so that I could see my code in another context besides the compiler. I hope nobody minds but It seems to be so much easier to read code when its posted like this than when i'm looking at visual studio.

First up is the TreeNode class, which is used extensively in the Tree classes constructors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

//Treenode class header

template < class Comparable>
class TreeNode {
    
  public:  

      Comparable  item;         // The data in this node.
      TreeNode *left;   // Pointer to the left subtree.
      TreeNode *right;  // Pointer to the right subtree.
      
      TreeNode  (Comparable value) ;     // Constructor.  Make a node containing value.
          
      TreeNode  (Comparable value, TreeNode *leftTree, TreeNode *rightTree) ;
                // Constructor.  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        

//Treenode class definitions

template<class Comparable>   //constructs a TreeNode with no children
TreeNode<Comparable>::TreeNode(Comparable value){
	item=value;
	left=NULL;
	right=NULL;
};

template<class Comparable>    //constructs a treenode with two children
TreeNode<Comparable>::TreeNode(Comparable value, TreeNode<Comparable> *leftTree, TreeNode<Comparable> *rightTree){
	item=value;
	left=leftTree;
	right=rightTree;
};


END OF TREENODE CLASS
-------------------------------------------------------------------------------------

TREE CLASS HEADER:

The makeEmpty function is supposed to clear the tree, it is actually overloaded with one definition in the class header and another definition below. Also present is the clone method which assists the copy constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

//header for Tree class clone method and MakEmpty helper function included 
template <class Comparable>
class Tree{

TreeNode<Comparable>* clone(TreeNode<Comparable>* other){
	if(other==NULL){
	return NULL;
	}
	return new TreeNode<Comparable>(other->item, clone(other->left), clone(other->right));	


void MakeEmpty(TreeNode<Comparable> *&t);
void MakeEmpty(){
	MakeEmpty(root);
};

public :
	TreeNode< Comparable> *root ;
	Tree () {    // dedault constructor 
	root = NULL ;
	}
		   
	Tree  (Comparable value) ;  // constructor ;create a single node tree

	Tree(Comparable value , Tree left, Tree right) ;  // constructor 

	Tree (Tree  &other) ;   // copy constructor

	Tree(TreeNode<Comparable> *r );   //  constructor taking a pointer to a tree node

    Tree & operator = (const Tree &rhs) ;        // overload assignment operator

	~Tree(){	// destructor  <-------PROBLEM?
		delete root;




MakeEmpty function which is supposed to assist in the destruction of the tree but I believe it may be conflicting with the already defined destructor.

1
2
3
4
5
6
7
8
9
10
11
12
//MakeEmpty function

template <class Comparable>
void Tree<Comparable>::MakeEmpty(TreeNode<Comparable> *&t){
	if(t!=NULL){
		MakeEmpty(t->left);
		MakeEmpty(t->right);
		delete t;
	}
	t=NULL;
}


copy constructor
1
2
3
4
5
6
7
8
9
10
11
12
//copy constructor

template <class Comparable>
Tree<Comparable>::Tree(Tree<Comparable> &other)
{
	if(other.root==NULL){
		root=NULL;
	}
	else{
		clone(other.root);
	}
};


operator = constructor
1
2
3
4
5
6
7
8
9
10
// constructor to overload the operator =, uses the makeEmpty and clone functions

template <class Comparable>
Tree<Comparable> & Tree<Comparable>::operator = (const Tree &rhs){
	if(this!=&rhs){
		MakeEmpty();
		root=clone(rhs.root);
		}
	return *this;
};


Parameterized Tree constructor that is supposed to link an object with two other subtrees.
1
2
3
4
5
6
7
8
9
10

// constructor, uses treenode class to begin construction of a tree with a node and two children

template <class Comparable>
Tree<Comparable>::Tree(Comparable value, Tree left, Tree right){
	
	root=new TreeNode<Comparable>(value);
	root->right=right.root;
	root->left=left.root;
}
Last edited on
Topic archived. No new replies allowed.