1 / 29

CS261 Data Structures

CS261 Data Structures. AVL Trees. Goals . Pros/Cons of a BST AVL Solution Height-Balanced Trees. Binary Search Tree: Balance. 50. 25. 75. 20. 35. 60. 85. 30. 45. 65. 80. Binary Search Tree: Balance. 50. 75. 85. 92. 90. Complete Binary Trees. Alex. Abner. Angela.

erling
Download Presentation

CS261 Data Structures

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. CS261 Data Structures AVL Trees

  2. Goals • Pros/Cons of a BST • AVL Solution • Height-Balanced Trees

  3. Binary Search Tree: Balance 50 25 75 20 35 60 85 30 45 65 80

  4. Binary Search Tree: Balance 50 75 85 92 90

  5. Complete Binary Trees Alex Abner Angela Abigail Adela Alice Adam • Very costly to maintain a complete binary tree Add Adam to tree

  6. Height-Balanced BST 3(3) 2(1) 8(2) 1(0) 9(0) 5(1) 4(0) 6(0) • For each node, the height difference between the left and right subtreesis at most one • Trees are locally balanced, but globally they can be slightly more unbalanced • Null child height = -1 Height-Balanced Tree

  7. Quiz 32 50 3 16 16 2 40 60 50 70 55 • Are these trees height balanced? If not, which node is out of balance?

  8. Quiz 100 100 155 115 110 50 50 150 120 160 130 • Are these trees height balanced? If not, which node is out of balance?

  9. Height-Balanced Trees • Mathematically, the longest path in a height-balanced tree has been shown to be, at worst, 44% longer than log n • Therefore, algorithms on height-balanced trees that run in time proportional to the path length are still O(log n) • So.....How do we maintain height balance??

  10. AVL Trees 1(0) 1(2) 2(1) 2(1) 3(0) 3(0) • Named after the inventors’ initials: G.M. Adelson-Velskii, E.M. Landis • Maintain the height balanced property of a BST through a series of rotations • When unbalanced, performs a “rotation” to balance the tree Node data Height field 1(2) Unbalanced node Rotate left

  11. Two Cases to Remember (Mirror for Right) 1 1 1 1 2 2 2 2 unbalanced After operation Heavy left child becomes heavy itself on the left! (Single Rot) Heavy left child becomes heavy itself on the right ! (Double Rot) After operation

  12. Fix with Rotations… 2(1) 3(0) 1(2)

  13. Case 1: Single Rotation 1(0) 2(1) 2(1) 3(0) 3(0) 1(2) Unbalanced “top” node Rotate left

  14. Case 1: Single Rotation Example 2 2(3) 2(1) 4(2) 4(2) 1(0) 3(0) 1(0) 3(0) 5(1) 5(1) 6(0) 6(0) New “top” node Unbalanced “top” node Rotate left

  15. AVL Trees: Rotation Pseudocode 2(3) 2(1) 4(2) 4(2) 1(0) 3(0) 3(0) 1(0) 5(1) 5(1) 6(0) 6(0) Pseudocode to rotate current (“top”) node left: • New top node is the current topnode’s right child • Current node’s new right child is the new top node’s left child • New top’s left child is the current node • Set height of current node • Set height of new top node • Return new top node “New” top node “Current” top node Rotate left

  16. Case 2: Double Rotation 1 1(1) 3 3(2) 2(0) 2 • Sometimes a single rotation will not fix the problem: • Can happen when an insertion is made on the left (or right) side of a node that is itself a heavy right (or left) child Unbalanced node Doesn’t work!!! “Heavy” right child Rotate left

  17. Case 2: Double Rotation 1(2) 1(2) 1(0) 3(1) 2(1) 2(1) 2(0) 3(0) 3(0) • Fortunately, this case is easily handled by rotating the child before the regular rotation: • First rotate the heavy right (or left) child to the right (or left) • Rotate the unbalancednode to the left (or right) Unbalanced node “Heavy” right child Rotate unbalanced node left Rotate heavy child right

  18. Case 1: Single Rotation (another view) 1 1 2 2 • Have a node (2) that is heavy on the left (1) • Operation makes (2) unbalanced and that heavy child heavy on left After Insertion

  19. Case 1: Single Rotation 1 2 2 1 • Single rotation fixes the problem Single Rotation

  20. Case 2: Double Rotation 1 1 3 3 • Have a node (3) with a heavy left child (1) • Operation makes (3) unbalanced and (1) is heavy on the right After Deletion 2 2

  21. Case 2: Double Rotation 1 1 3 3 • First, rotate heavy child (1) to the left After Rotation 1 2 2

  22. Case 2: Double Rotation 1 1 3 3 • Next, rotate unbalanced node (3) to the right 2 After Rotation 2 2

  23. AVL Trees: BalacingPseudocode Balancing pseudocode(to rebalance an unbalanced node): If left child is tallest (by more than 1): If left child is heavy on the right side: // Double rotation needed. Rotate the left child to the left Rotate unbalanced “top” node to the right Elseif right child is tallest(by more than 1) If right child is heavy on the left side: // Double rotation needed. Rotate the right child to the right Rotate unbalanced “top” node to the left Return new “ top” node

  24. Rotations • Can be the result of additions or removals • All cases hold for the “right” side as well, just replace all lefts with rights and all rights with lefts

  25. More Examples… 3(3) 3(4) 2(1) 2(1) 8(2) 8(3) 1(0) 1(0) 9(0) 9(0) 5(1) 5(2) 4(0) 4(0) 6(0) 6(1) 7(0) Balanced Tree Unbalanced Tree Unbalanced “top”node Add data: 7 “Heavy” left child Added to right side of heavy left child

  26. AVL Trees: Double Rotation Example 3(4) 3(4) 2(1) 2(1) 8(3) 1(0) 1(0) 9(0) 9(0) 8(2) 5(2) 5(3) 4(0) 4(0) 6(1) 6(1) 7(0) 7(0) Unbalanced Tree Tree Still Unbalanced Unbalanced “top” node (still) Single rotation

  27. AVL Trees: Double Rotation Example 3(4) 3(4) 2(1) 2(1) 8(3) 8(3) 1(0) 1(0) 9(0) 9(0) 5(1) 5(2) 4(0) 4(0) 6(2) 6(1) 7(0) 7(0) Unbalanced Tree Tree Still Unbalanced, but … Rotate heavy child “Heavy” left child

  28. AVL Trees: Double Rotation Example 3(3) 3(4) 2(1) 2(1) 8(3) 1(0) 1(0) 9(0) 9(0) 8(1) 5(1) 5(1) 6(2) 4(0) 4(0) 6(2) 7(0) 7(0) Unbalanced Tree (after 1st rotation) Tree Now Balanced Rotate top node Unbalanced “top” node

  29. Your Turn • Worksheet: AVL Practice

More Related