### AVL Tree Error

I'm getting an error on my getHeightDifference() for my AVL tree. I've included my insert, rebalance, updateHeight, and getHeightDifference functions for people to look. I'm not sure if I have my updateHeight and rebalance in the right locations though. Advice is greatly appreciated.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697`` ``````void avlTree::insert(Node*& n, int item) { // If node is null, create a new node with input data if(n==nullptr) { n=new Node(item); //n->updateHeight(n); } else { // Check to see if data being inserted is < current node->data if(item < (n->data)) { // recursive call with left child node input node insert((n->left), item); //n->updateHeight(n); rebalance(n->left); } // Check to see if data being inserted is > current node->data else if(item > (n->data)) { // recursive call with left child node input node insert((n->right), item); rebalance(n->right); //n->updateHeight(n); } else // If item is a duplicate, output duplicate message cout<updateHeight(n); int heightDifference=n->getHeightDifference(n); // Node n's left subtree is taller than its right subtree by more than 1 if(heightDifference > 1) { // Addition was in Node n's left subtree // Left child of Node n has a left subtree that is taller than // its right subtree if(n->getHeightDifference(n->left) > 0) // Addition was in left subtree of left child n=rotateRight(n); else // Addition was in right subtree of left child n=rotateLeftRight(n) ; } // Node n's right subtree is taller than its right subtree by less than // -1 else if(heightDifference < -1) { // Addition was in Node n's right subtree // Right child of Node n has a right subtree that is taller // than its left subtree if(n->getHeightDifference(n->right) < 0) // Addition was in right subtree of right child n=rotateLeft(n); else // Addition was in left subtree of left child n=rotateRightLeft(n); } else return; } int Node::getHeightDifference(Node *n) { return n->left->height - n->right->height; //ERROR IS HERE. } // If anyone knows of a more simplified version of this that would be nice but // not necessary void Node::updateHeight(Node *n) { if(n->left == nullptr && n->right == nullptr) { n->height=1; return; } if(n->left != nullptr) updateHeight(n->left); if(n->right != nullptr) updateHeight(n->right); if(n->left != nullptr && n->right != nullptr) { if(n->left->height > n->right->height) n->height=n->left->height+1; else n->height=n->right->height+1; } if(n->left != nullptr && n->right == nullptr) n->height=n->left->height+1; else n->height=n->right->height+1; }``````
Topic archived. No new replies allowed.