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