
Balanced Trees and AVL Trees
Learn about balanced trees, maintaining tree balance, forcing good behavior, AVL trees, and the importance of rotations in tree balancing. Discover how to keep trees shallow and prevent deep unbalanced structures.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Balanced Trees Debojit Das 4/13/2025 1
A Good Tree In a "good" BST we have depth of T = O( log n ) n = num of nodes in the tree Theorem: If the tree is constructed from n inputs given in random order, then we can expect the depth of the tree to be log2n. (no proof given) But if the input is already (nearly, reverse, ) sorted we are in trouble. 4/13/2025 2
Forcing good behavior We can show that for any n inputs, there always is a BST containing these elements of logarithmic depth. But if we just insert the standard way, we may build a very unbalanced, deep tree. Can we somehow force the tree to remain shallow? At low cost? 4/13/2025 3
Balanced Trees A balanced tree is where equal ( almost ) number of nodes exists in left and right sub trees of each node However in practice this is hard to enforce We can expect the trees to remain shallow by randomizing a data set before inserting to a tree Need O(n) operations to randomize the data set A relaxed balanced condition can be used in building a good tree AVL trees 4/13/2025 4
AVL Trees AVL trees requires a relaxed balanced condition Define balanced factor of a node to be the absolute difference in heights between left and right sub trees AVL tree is defined as a tree such that, for each node, balanced factor 1 4/13/2025 5
AVL-Trees G. M. Adelson-Velskii and E. M. Landis, 1962 1 or less 4/13/2025 6
AVL-Trees An AVL-tree is a BST with the property that at every node the difference in the depth of the left and right subtree is at most 1. not OK OK 4/13/2025 7
Bad News Insertion in an AVL-tree is more complicated: inserting a new element as a leaf may break the balance of the tree. But we can't just place the new element somewhere else, we have to maintain the BST property. Solution: insert in standard place, but then rebalance the tree. 4/13/2025 8
But How? The magic concept is rotation. A rotation rearranges a BST, but preserve the BST property. Can be done out in O(1) steps. To fix up an AVL tree, we have to perform several rotations, but only along a branch: total damage is O(log #nodes). 4/13/2025 9
But How? rotate right on y x y y x C A B C B A rotate left on x 4/13/2025 10
So? Rotation does not change the flattening, so we still have a BST. But the depth of the leaves change by -1 in A, stay the same in B, and change by +1 in C. 4/13/2025 11
Well, not quite Unfortunately, there are other cases to consider, depending on where exactly the balance condition is violated. To fix some of them requires a double rotation. There is a nice demo at: http://www.site.uottawa.ca/~stan/csi2514/applet s/avl/BT.html 4/13/2025 12
Tree Balance Rotations Note that after an insertion only the nodes in the path from root to the node have their balance information altered Find the node that violates the AVL condition and rebalance the tree. Assume that X is the node that has violated the AVL condition I.e The left and right sub trees of X is differ by 2 in height. 4/13/2025 13
Four Cases Case I Case I The left subtree of the left child of X violates the property. Rotate RIGHT y X X y C B C A A B 4/13/2025 14
Code for Case 1 /** * Rotate binary tree node with left child. * For AVL trees, this is a single rotation for case 1. */ static <AnyType> BinaryNode<AnyType> rotateWithLeftChild( BinaryNode<AnyType> k2 ) { BinaryNode<AnyType> k1 = k2.left; k2.left = k1.right; k1.right = k2; return k1; } 4/13/2025 15
Four Cases Case 2 Case 2 The right subtree of the right child of X violates the property. X Rotate LEFT y X y C B A A C B 4/13/2025 16
Code for Case 2 /** * Rotate binary tree node with right child. * For AVL trees, this is a single rotation for case 4. */ static <AnyType> BinaryNode<AnyType> rotateWithRightChild( BinaryNode<AnyType> k1 ) { BinaryNode<AnyType> k2 = k1.right; k1.right = k2.left; k2.left = k1; return k2; } 4/13/2025 17
Four Cases Case 3 Case 3 The right subtree of the left child of X violates the property X about X s child and Grandchild X Rotate the tree LEFT Z y y A Z A B D C D C B Ctd.. 4/13/2025 18
Four Cases Case 3 ctd.. Rotate the tree RIGHT about X and its new child Z X X y Z y A C D B A B D C 4/13/2025 19
Code for Case 3 /** * Double rotate binary tree node: first right child * with its left child; then node k1 with new right child. * For AVL trees, this is a double rotation for case 3. */ static <AnyType> BinaryNode<AnyType> doubleRotateWithRightChild( BinaryNode<AnyType> k1 ) { k1.right = rotateWithLeftChild( k1.right ); return rotateWithRightChild( k1 ); } 4/13/2025 20
Four Cases Case 4 Case 4 The left subtree of the right child of X violates the property Rotate the tree RIGHT about Y and Z X X Z D y D y Z A C B A C B 4/13/2025 21 Ctd..
Four Cases Case 4 ctd.. Rotate the tree LEFT about X and Z Z X y X Z D y D C B A C A B 4/13/2025 22
Code for Case 4 /** Double rotate binary tree node: first left child * with its right child; then node k3 with new left child. * For AVL trees, this is a double rotation for case 2. */ static <AnyType> BinaryNode<AnyType> doubleRotateWithLeftChild( BinaryNode<AnyType> k3 ) { k3.left = rotateWithRightChild( k3.left ); return rotateWithLeftChild( k3 ); } 4/13/2025 23
AVL Tree Examples Insert 12, 8, 7, 14, 18, 10, 20 with AVL rotations 4/13/2025 24
Implementing AVL trees The main complications are insertion and deletion of nodes. For deletion: Don t actually delete nodes. Just mark nodes as deleted. Called lazy deletion. 4/13/2025 25
Lazy deletion 5 3 7 2 4 6 9 On average, deleting even half of the nodes by marking leads to depth penalty of only 1. 4/13/2025 26
AVL Summary Insert a new node in left or right subtree. Test the height information along the path of the insertion. If not changed we are done Otherwise do a single or double rotation based on the four cases Question: What is the best thing to do about height info? 4/13/2025 27