So my code is below. I’m not getting any errors and it places everything in the node just fine. But based on my debug statements Everytime anything is inserted it’s finding the root. I’m not sure if that is right. But according to output file for the assignment, my answers are different when it comes to the height of the tree, the traversals, and I just flat am still having troubles with my leaf count function. Another story though.
Based on the debug statements it looks like everything is going right where they should. But I figure I might need fresh eyes. I don’t see how my traversals could change at all since it is really only a matter of where I’m proccessing the node that should effect the Inorder, preorder, and postorder.
template <class T> void BT<T>::insert(const T& item) { Node<T>* newNode; newNode = new Node<T>(item); insert(root, newNode); } template <class T> void BT<T>::insert(struct Node<T> *&root, struct Node<T> *newNode) { if (root == NULL) { cout << 'Root Found' << newNode->data << endl; root = newNode; } else { if (newNode->data < root->data) { insert(root->left, newNode); cout << 'Inserting Left' << newNode-> data << endl; } else { insert(root->right, newNode); cout << 'Inserting Right' << newNode->data << endl; } } }
My height function is as follows just in case my insert is actually fine.
template <class T> int BT<T>::height() const { return height(root); } template <class T> int BT<T>::height(Node<T>* root) const { if (root == NULL) return 0; else { if (height(root->right) > height(root->left)) return 1 + height(root-> right); return 1 + height(root->left); } }
You need to change the wording of your debug statements
Really it should read (not Root node)
It is only the root when it is first called after that any call with node->left or node->right makes it an intermediate node.
To write height() I would do this: