AVLTree-restructure

AVLTree-restructure - void AVLTree:restructure(AVLNode *n)...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
void AVLTree::restructure(AVLNode *n) { // balance tree starting at node n // Find "z" that is the first unbalanced node starting at "z". AVLNode *z = n->parent; while(z != NULL) { // while w not reach root // Find parent of unbalanced tree int hleft = 0; if(z->left != NULL) { hleft = z->left->height; } int hright = 0; // height right child if(z->right != NULL) { hright = z->right->height; } int hdiff = hleft - hright; if(hdiff < 0) hdiff = -hdiff; // make diff absolute // Check if z is unbalanced if(hdiff <= 1) { // z is balanced TO BE CONTINUED. .. // Recompute height of z just in case it was modified with a rotation previously. if(hleft>hright) { z->height = 1+hleft; } else { z->height = 1+ hright; } // test if z is unbalanced if(diff <= 1) { // z is balanced z = z->parent; continue;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
} // At this point z is parent of an unbalanced subtree // Find y, y is the child of z that has highest height. AVLNode *y = NULL;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/04/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University.

Page1 / 5

AVLTree-restructure - void AVLTree:restructure(AVLNode *n)...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online