Binary Tree

I am trying to create a simple binary tree.


I have pasted my code below.

I am getting two errors :
ld: symbol(s) not found for architecture x86_64
Description Resource Path Location Type
make: *** [BTreeLab] Error 1 BTreeLab C/C++ Problem


Does anyone see what I am doing wrong? I can't seem to figure it out.


Any help is appreciated, thanks!




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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include<iomanip>
using namespace std;


struct node
{
node *left;
node *right;
int data;

};

class BinTree
{
  private:
	node *root;
	int count;



  public:

   BinTree()
   {
	   root = NULL;
	   count = 0;
   }

   void add(int a)
   {
	   add_node(a,root, count);
   };

   void add_node(int a, node *rot, int count)
   {
	   node *curr; curr = rot;

	   if(curr==NULL)
	   {
		   curr = new node;
		   curr->data = a;
		   curr->left = NULL;
		   curr->right = NULL;
		   root = curr;

		   return;
	   }

       if(a >= curr->data)
       {
    	   curr=curr->right,add_node(a,curr, count);
       }
		   if(a < curr->data)
		   {
			   curr=curr->left,add_node(a,curr, count);
		   }
   }

   void print()
   {
	   inorder(root);
   }

   void inorder(node *curr)
   {
     if(curr->left != NULL)
     {
    	 inorder(curr->left);
     }

     cout << curr->data << " ";

		 if(curr->right != NULL)
		 {
			 inorder(curr->right);
		 }
   }
};



void add(int a);

void print();

int main()
{
	//struct node* tree = NULL;

	    int i;

	    do
	    {
	        cout << "Enter a number into the tree: ";
	        cin >> i;

	        if(i == -1)
	        {
	        	break;
	        }

	        add(i);

	    } while( i != -1);

	    print();


	return 0;
}



Last edited on
Why does add() take the count as a parameter? It's a class member and you want to update that member.

Speaking of which, you need to update count in add_node().

Line 45 is wrong because it sets the root of the entire tree to the new node, so even if the tree somehow had 5,000 nodes before, it would have just one node after. To fix this:
- pass rot by reference (node * &rot).
- change line 45 to rot = curr;
- change line 52 and 56 to add_node(a, curr->left) and add_node(a, curr->right) respectively. It's very important to pass the actual left and right pointers as the parameters because they are the ones that will be changed.

To handle printing an empty tree, change inorder to check if curr is NULL, rather than checking if the left and right pointers are NULL. This will both simplify the code and handle the case of an empty tree.

Can you post the full guidelines? I suspect I'm missing something.

-Data array field (private)
-Count field
Add method(data)
-The add method takes a data point, add it to the count
position in the array then increases the count.

This confuses me. What is this "data array" field? In a binary tree there is only one piece of data in the node. add() should add to the count position? That sounds more like an expanding array than a binary tree. Some


Topic archived. No new replies allowed.