1 / 32

AVL Trees

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.

Download Presentation

AVL Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AVL Trees

  2. Tree Height = 3(Length of longest Path) 0 K G 1 P H X D 2 3 A F

  3. 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)

  4. 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

  5. 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.

  6. 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.)

  7. 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

  8. 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

  9. Example AVL Trees 1 K 1 -1 G P 0 0 0 H X D 0 0 A F (Balance info above nodes)

  10. 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

  11. 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

  12. LL & RR w/ Children • What happens when rotated nodes have children?

  13. Example AVL Trees 1 K 1 -1 G P 0 0 0 H X D 0 0 A F Try to Insert B

  14. 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

  15. 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.

  16. H F B Completed Insertion with Rotation 3 K 2 1 D P 1 0 1 G X A 0 0

  17. 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; }

  18. 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; }

  19. RL/LR Violations Fixed by Double Rotation • Why not a single rotation?

  20. 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?

  21. 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

  22. AVL Double Rotation • Call the deepest node that is unbalanced X. • Rotate X’s child with its grandchild. • Rotate X and its new child.

  23. 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

  24. 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

  25. A E Completed Double Rotation 3 K 1 2 P F 0 1 1 X G D 0 0 0 H

  26. 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 ); }

  27. 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 ); }

  28. 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.

  29. 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.

  30. 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)

  31. 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.

More Related