320 likes | 493 Views
AVL Trees. Tree Height = 3 ( Length of longest Path). 0. K. G. 1. P. H. X. D. 2. 3. A. F. AVL Tree. First Balanced Binary Search Tree G.M. Adelson-Velskii / E.M.Landis 1962 BST w/ balance condition Ensures tree depth is O(log n). AVL Tree Definition.
E N D
Tree Height = 3(Length of longest Path) 0 K G 1 P H X D 2 3 A F
AVL Tree • First Balanced Binary Search Tree • G.M. Adelson-Velskii / E.M.Landis 1962 • BST w/ balance condition • Ensures tree depth is O(log n)
AVL Tree Definition • An AVL tree is a BST such that for any node in the tree: • the height of the left and right subtrees • differ at most by 1 • Height of a subtree with two nodes is 1 • Height of a subtree with one node is 0 • The height of an empty subtree is -1
L/R Sub-Tree Heights 2 1 K G 1 0 -1 0 P H -1 -1 X D 0 0 -1 -1 A F -1 -1 -1 -1 We will say sub-trees without a a root node have a height of 0.
Tree Balance Info Values(Height of L. Subtree - Height of R. Subtree) 1 K 1 -1 G P 0 0 0 H X D 0 0 A F (Tree is balanced based on AVL def.)
Insert Operation • Only nodes in path from insertion point to root have their sub-trees altered • Insert a new node as with a BST • Follow path from insertion point to root updating balance information • At any node, the new balance may violate the AVL condition • Re-balancing the deepest node • Re-balancing deepest node guarantees the entire tree satisfies AVL
4 AVL Insertion Violations • Insert left child of X’sleft subtree LL • Insert right child of X’sright subtree RR • Insert left child of X’sright subtree LR • Insert right child of X’sleft subtree RL
Example AVL Trees 1 K 1 -1 G P 0 0 0 H X D 0 0 A F (Balance info above nodes)
RR/LL Violations Fixed by Single Rotation • Insertion in Left Subtree of Left Child of X • Insert in Right Subtree of Right Child of X • Switch parent (X) and child • Maintain search order
Simple LL & RR Rotations • http://www.cgc.cs.jhu.edu/~jkloss/htmls/structures/avltree.html • http://www.seanet.com/users/arsen/avltree.html BAD • http://www.cse.iitk.ac.in/users/dsrkg/cs210/applets/AVLtree/avl.html K G G D K D
LL & RR w/ Children • What happens when rotated nodes have children?
Example AVL Trees 1 K 1 -1 G P 0 0 0 H X D 0 0 A F Try to Insert B
F B AVL Insertion 2 K 2 -1 G P 0 0 1 H X D 0 -1 A G and K are now unbalanced G is the deepest nodeThis is still a LL rotation beginning at node G 0
F B AVL Single Rotation K G P H X D A Note if D node goes up and G goes down, G becomes D's right child. What happens to F? It becomes G's left child.
H F B Completed Insertion with Rotation 3 K 2 1 D P 1 0 1 G X A 0 0
C++ Implementation of LL Single Rotation // For AVL trees, this is a single LL rotation TreePtrType RotateWithLeftChild(TreePtrType k2) { TreePtrType k1 = k2->left; k2->left = k1->right; k1->right = k2; return k1; }
C++ Implementation of RR Single Rotation // For AVL trees, this is a single RR rotation TreePtrType RotateWithRightChild(TreePtrType k1) { TreePtrType k2 = k1->right; k1->right = k2->left; k2->left = k1; return k2; }
RL/LR Violations Fixed by Double Rotation • Why not a single rotation?
Another Case 3 K 2 1 G P 0 0 1 H X D 0 0 A F What if we try to add E?
F E Harder AVL Insertion 2 K 2 1 G P 0 0 1 H X D 1 0 A G and K are now unbalanced 0
AVL Double Rotation • Call the deepest node that is unbalanced X. • Rotate X’s child with its grandchild. • Rotate X and its new child.
F E AVL Double Rotation - First Rotation 4 K 3 1 G P 0 0 2 H X D 1 0 A First rotate D and F 0
A E AVL Double Rotation - Second Rotation 4 K 3 1 G P 0 0 2 H X F 1 D Second rotate G and F 0 0
A E Completed Double Rotation 3 K 1 2 P F 0 1 1 X G D 0 0 0 H
C++ Implementation of LR Double Rotation // Double rotate binary tree node: first left child // with its right child; then parent with new left child // For AVL trees, this is a LR double rotation TreePtrType DoubleRotateWithLeftChild(TreePtrType k3) { k3->left = RotateWithRightChild( k3->left ); return RotateWithLeftChild( k3 ); }
C++ Implementation of RL Double Rotation // Double rotate binary tree node: first right child // with its left child; then parent with new right child // For AVL trees, this is a RL double rotation TreePtrType DoubleRotateWithRightChild(TreePtrType k1) { k1->right = RotateWithRightChild( k1->right ); return RotateWithRightChild( k1 ); }
Keeping Track of Heights for AVL Trees • Store in each node: the difference in the heights of its left and right subtrees. • If we increase the height of the left subtree, then add one to the balance. • If we increase the height of the right subtree, then subtract one from the balance.
Determining Subtree to Rotate in AVL Insertions • When returning up the tree, the first node whose adjusted height is -2 or +2 requires rotation. • After rotation, the subtree is the same height as it was before, so no nodes further up towards the root will need to be updated.
Deletion from AVL Trees • Deletion is the same as deletion from a BST. • May require rotations as with insertion. • Same type of rotations will balance the tree. • Balance is checked after deletion beginning with root • First out of balance node is rebalanced • Simplest rotation is selected (e.g., LL over LR)
Rebalancing cost/benefit • Balanced binary trees with n nodes will have depth O(log2n). • AVL trees thus guarantee search time will be O(log 2n). • Overhead involved in rebalancing as the AVL tree • justified when search operations exceed insertions • faster searches compensate for slower insertions. • Empirical studies indicate that on the average, rebalancing is required for approximately 45 percent of the insertions. Roughly one-half of these rotations require are double rotations.