Jump to content
  • Advertisement
Sign in to follow this  
Prozak

AVL: Branch Depth after Rotation

This topic is 4353 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!