• Advertisement
Sign in to follow this  

AVL: Branch Depth after Rotation

This topic is 4175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to come up with an algorithm to calculate the depth of a branch of an AVL tree, after it has sufered a right/ left rotation. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Do you want something based on the depths of the sbutrees (which also need to be adjusted)?

Or are you wanting to write a validation function that you can run at any time between operations on it, to check if the tree is properly balanced etc?
Here is my own code for the later which performs complete verifiction of all height values and checks ordering of all nodes as well. (It could be even more comprehensive though):
template <class TNode>
bool AVLTreeVerify(TNode *head) {
while (head != NULL) {
if (head->left != NULL) {
if ((*head < *(head->left))
|| !AVLTreeVerify(head->left)) return false;
}
if (head->right != NULL)
if (*(head->right) < *head) return false;
if (head->height != Max(Height(head->left), Height(head->right)) + 1) return false;
head = head->right;
}
return true;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement