Binary Trees Question

Hello everyone,

I am learning about Binary Trees and I got stuck. I am trying to create a tree, insert the nodes and then I am planning to implement a bunch of operations but I got some errors. Here is my code, and the errors are at the end. Any information helps. I did research the errors and I think that this might have something to do with the way I named and structured things in my code.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  class Tree
{
private:
	int _value;
	Tree* _left;
	Tree* _right;
	Tree* _root;
public:

	Tree()
	{}
	Tree(int value, Tree* left, Tree* right) 
	{};

	void insert(Tree* &, Tree* &);

	//inserts node



	void Tree::insertNode(int num)
	{
		Tree* newNode = nullptr; //pointer to a new node

		newNode = new Tree; 
		newNode->value=num; 
		newNode->left=newNode->right=nullptr; 
	
	 insert(root, newNode); 
	}
	
void Tree::insert(Tree* &nodePtr, Tree* &newNode)
	{
		if(nodePtr ==nullptr)
			nodePtr =newNode; 
		else if (newNode->value < nodePtr->value) 
			insert(nodePtr->left, newNode);    
		else
		{
			insert(nodePtr->right, newNode);
		}                                         
	}

};

int main()
{
	Tree objTree;

	cout<<"Inserting Nodes.\n";
	objTree.insertNode(1);
	objTree.insertNode(2);
	objTree.insertNode(3);
	objTree.insertNode(4);
	objTree.insertNode(5);
	objTree.insertNode(6);
	objTree.insertNode(7);
	objTree.insertNode(8);
	objTree.insertNode(9);
	return 0;
}


Errors:
'=' : function as left operand
'=' : function as left operand
error C2065: 'root' : undeclared identifier
error C3867: 'Tree::value': function call missing argument list; use '&Tree::value' to create a pointer to member
error C2296: '<' : illegal, left operand has type 'int (__thiscall Tree::*
error C2297: '<' : illegal, right operand has type 'int (__thiscall Tree::* )(void)'
error C3867: 'Tree::left': function call missing argument list; use '&Tree::left' to create a pointer to member
error C3867: 'Tree::right': function call missing argument list; use '&Tree::right' to create a pointer to member
Last edited on
When you define member functions inside the class definition you should not put the class name in front of the function names.
 
void Tree::insertNode(int num)


The order you declare member functions doesn't matter You can declare insert before or after insertNode, even if you call insert inside that function. You can not declare and later define a member function inside the class definition. What you can do is to declare the function and then define the function it outside the class definition, or you could just get rid of the declaration.
 
void insert(Tree* &, Tree* &);


The member variables start with an underscore so you have to use underscore when you use them inside the functions.
 
newNode->_value=num; 
Last edited on
Thanks for the tips Peter87, had a lot of "Doh!" moments right now. I will work on it and report back :)
Followup:
I worked on the code some more and I have questions regarding some functions that I need to implement:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Tree
{
private:
	int _value;
	Tree* _left;
	Tree* _right;
	
public:
	Tree(int value, Tree* left, Tree* right) 
	{
		_value=value;
		_left=left;
		_right=right;
	};

	int value()
	{
		return _value;
	}

	Tree* left()
	{
		return _left;
	}

	Tree* right()
	{
		return _right;
	}


	void addElem(int n, Tree* bstree)
	{
		if(bstree->value > n)
		{
			if(bstree->_left==NULL)
				bstree->_left=new Tree(n, NULL, NULL);
			else
				addElem(n, bstree->_left);
		}
		else
		{
			if(bstree->_right==NULL)
				bstree->_right=new Tree(n, NULL, NULL);
			else
				addElem(n, bstree->_right);
		}
	}



	int size(Tree* bstree)
	{
		if (bstree ==NULL)
			return 0;
		else
			return size(bstree._left)+ 1 + size(bstree._right);
	}

	
	int depth(Tree* bstree)
	{
		if (bstree==NULL)
			return 0;
		else
		{
			int leftDepth = depth(bstree->_left);
			int rightDepth = depth(bstree->_right);
		}
		if (leftDepth > rightDepth)
			return(leftDepth +1);
		else
			return(rightDepth+1);
	}

	
	int sum(Tree* bstree)
	{
		if(bstree==NULL)
			return 0;
		else
			return bstree->value+sum(bstree->left) +sum(bstree->right);
	}


Errors that I am getting:
size function- bstree - Error expression mus have class type
depth function-leftDepth and rightDepth when trying to compare appear as unidentified

Questions:
1. How can the size function be explained?
I understand that we have a base case (if bstree is NULL then return 0).
BUT the else statement is a bit confusing due to the +1. Please explain.

2. I do not understand the sum function entirely. Please explain

bstree is a pointer so access its value using operator->().

1
2
3
4
5
return size(
            bstree->_left) +     // size of left subtree
            1 +                  // our node counts 1
            size(bstree->_right  // size of right subtree
           );


Now try to explain the sum function yourself.
Topic archived. No new replies allowed.