Upcoming SlideShare
×

# AVL Trees

1,041 views

Published on

Learn About AVL trees And rotations

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,041
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
23
0
Likes
0
Embeds 0
No embeds

No notes for slide

### AVL Trees

1. 1. AVL Trees
2. 2. Recall Motivation: Linked lists and queues work well enough for most applications, but provide slow service for large data sets. 3 3 4 4 835 835 837 837 836 836 Ordered insertion takes too long for large sets.
3. 3. Solution: Trees 4 4 1 1 Binary Trees provide quick access to data 7 7 6 6 1 1 9 9 4 4 6 6 O (logN) O (logN) vs vs O (N) O (N) 7 7 9 9
4. 4. 15 O(N2) Why it Why it matters matters O(N) # Steps 10 O(log N) 5 0 5 10 # Items 15 20
5. 5. 4 4 1 1 Trees: Balance Consider an imbalanced Binary Search Tree. Search performance degrades back into a linked list. 7 7 6 6 The promised O(log N) will approach O(N). 9 9 12 12 17 17
6. 6. Balance If we had some way of keeping the tree “in balance”, our searches would be closer to O(log N) 9 9 4 4 1 1 12 12 7 7 17 17 6 6 But how does one measure balance?
7. 7. Measuring “Balance” 4 4 1 2 3 4 5 1 1 Perhaps we measure the height... 7 7 6 6 9 9 4 4 1 1 9 9 12 12 12 12 7 7 6 6 17 17 …but the height alone does not give us a indication of imbalance 17 17
8. 8. Balance: The Key 4 4 1 1 Subtree’s right child too tall for its sibling 7 7 6 6 Balance is a relative property, not an absolute measure... 9 9 4 4 1 1 9 9 12 12 12 12 7 7 6 6 17 17 …as with all things. 17 17
9. 9. Measuring “Balance” Adelson-Velskii and Landis suggested that, since trees may be defined recursively, balance may also be determined recursively. 9 9 Internal nodes are also roots of subtrees. If each node’s subtree has a similar height, the overall tree is balanced 4 4 1 1 12 12 7 7 6 6 17 17
10. 10. Measuring Balance Thus, the actual numerical height does not determine balance; rather, it’s the relative height of both subtrees. 9 9 4 4 H 1 7 1 7 9 9 12 12 17 17 H-2 H-1 6 6 Balanced Tree Test: subtrees differ in height by no more than one. This recursively holds true for all nodes!
11. 11. Balance Factor To measure the balance of a node, there are two steps: Record the node height (NH) --an absolute numeric measure Record the node’s balance factor (BF) --a relative measure The node height alone does not indicate balance: we have to compare it to the height of other nodes.
12. 12. EP EP ST ST Node / Tree Height Tree height is easy, Here are three trees. 1 1 H=3 H=1 4 4 9 9 Height? H=2 12 12 17 17 1 1 7 7 6 6
13. 13. Node / Tree Heights for AVLs By convention, a null reference will return a height of zero (0). Therefore: The Balance Factor of is Ht of 17 17 = 1 – (0) = +1 12 12 0 12 12 minus (0) 17 17 0 0
14. 14. EP EP ST ST Balance Factor 2 2 Remember that balance is relative. relative So we define the Balance Factor (BF) at: BF = (right subtree height - left subtree height) 9 9 0 +1 12 12 4 4 0 17 17 1 1 +1 0 7 7 6 6 0 -1
15. 15. Balance Factor Each node has a balance factor. Remember, it’s based on (right ht - left ht). -1 9 9 4 4 1 7 1 7 6 6 +1 12 12 17 17 9 9 4 4 0 12 12 -1 1 7 1 7 6 6 +1 0 17 17 0 (We will note BF outside the node, to avoid confusion with the key values. Some texts don’t even give the keys, because values are irrelevant once you have a proper BST.)
16. 16. Quiz Yourself Calculate the balance factor for each node in each tree: 9 9 9 9 4 4 4 4 1 7 1 7 9 9 4 4 12 12 6 6 9 9 4 4 1 7 1 7 12 12 17 17 16 16
17. 17. Quiz Yourself- Ans Calculate the balance factor for each node in each tree: Here, the height is written inside the node, covering the key value it holds. Hopefully, this graphical clue helps. 2 2 -1 1 1 1 3 3 2 2 1 1 4 4 0 1 1 1 2 1 2 1 1 4 4 -3 -1 2 2 1 3 3 2 1 1 1 1 2 2 1 1 -1
18. 18. Using Balance Factors How do the Balance Factors help? Recall: Adelson-Velskii and Landis used a recursive definition of trees. Our BF measure +1 is likewise 4 4 recursive. -2 0 Not Balanced 0 9 9 4 4 0 1 7 1 7 0 -1 9 9 +1 12 12 -1 0 1 7 1 7 6 6 17 17 0 Balanced If any BF is > +1 or < -1, there’s imbalance
19. 19. AVL’s “Weak” Balancing • A fully balanced tree guarantees that, for nodes on each level, all left and right subtrees have the same height, and imposes this recursively. • AVL trees use a weaker balance definition which still maintains the logarithmic depth requirement: 12 12 – for any node in an AVL tree, the height of the left and right nodes differs by at most 1. • For example, this is a valid AVL tree, but not strictly balanced: 8 8 4 4 6 6 10 10 16 16 2 17 17
20. 20. Addressing Imbalance If we build a tree from scratch, and check nodes for imbalance after a new insertion, we can guard against imbalance. Q: So what do we do when the tree is unbalanced? A: Rotate the tree.
21. 21. Identifying Offending Scenarios The key to fixing an unbalanced tree is knowing what caused it to break. Let’s inductively look at what can cause a good tree to go bad. Given an AVL balanced tree: Two possible left insertions could break balance: #1 Insertion to left subtree of left child -2 -1 1 1 9 9 4 4 0 #2 +1 9 9 4 4 -1 0 -2 9 9 4 4 7 7 0 Insertion to right subtree of left child
22. 22. #1 Patterns of Problems -2 Insertion to 99 left subtree -1 44 of left child 11 #2 -2 99 +1 44 0 0 77 Insertion to right subtree of left child … and the mirror of these insertions #3 Insertion to left subtree of right child +2 9 9 -1 11 11 0 10 10 #4 +2 Insertion to right subtree of right child 9 9 +1 11 11 0 13 13
23. 23. #1 -2 Insertion to 99 left subtree -1 44 of left child 11 #2 Imbalance found regardless of height 0 9 9 matches -2 -2 +1 99 44 0 77 Insertion to right subtree of left child H-1 H H+1 #3 +2 99 Insertion to left subtree of right child 11 -1 11 10 10 0 4 4 (H-1) - (H+1) = -2 #4 +2 99 11 Insertion to 11 +1 right subtree of right child 13 0 13 Given our recursive definition of trees, we know these four scenarios are complete. The same insertion into a larger AVL balanced tree does not create a new category.
24. 24. #1 #2 -2 Insertion to 99 left subtree -1 44 of left child 11 0 #3 +2 99 Insertion to left subtree of right child 11 -1 11 10 10 0 Let’s look at two insertions that cause imbalance. Recall they are mirrors. They both deal with “outside” insertions on the tree’s exterior face: the left-left and right-right insertions. -2 +1 99 44 0 77 Insertion to right subtree of left child #4 +2 99 11 Insertion to 11 +1 right subtree of right child 13 0 13
25. 25. Single Rotation #1 Insertion to left subtree of left child -2 -1 1 1 9 9 4 4 These insertions can be cured by swapping parent and child . . . 0 #4 . . . provided you maintain search order. (An AVL tree is a BST) +2 Insertion to right subtree of right child 9 9 +1 11 11 0 13 13
26. 26. Single Rotation: Right #1 0 -2 -1 Insertion to left subtree of left child 9 9 4 4 1 1 0 X X 1 1 9 9 0 A single rotation round the unbalanced node restores AVL balance in all cases W W W W A 0 4 4 X X B C A B C
27. 27. 9 9 9 9 4 4 1 1 9 9 4 4 1 1 4 4 9 9 1 1 9 9 Think of rotations - Imagine the joints at the ‘4’ node starting to flex and articulate.
28. 28. #4 Single Rotation: Left +2 Insertion to right subtree of right child 0 9 9 +1 11 11 0 13 13 11 11 0 0 9 13 9 13 A similar rotation is used for right-right insertions that cause an imbalance X X W W W W X X A B C A B C
29. 29. Word of caution 9 9 X W A 3 3 6 6 4 4 7 7 5 5 C 11 11 14 14 The single rotations are simple, but you have to keep track of child references. B 1 1 9 9 W A 1 1 4 4 3 3 B 5 5 6 6 X 7 7 C 11 11 14 14
30. 30. #1 #2 -2 Insertion to 99 left subtree -1 44 of left child 11 0 #3 +2 99 Insertion to left subtree of right child 11 -1 11 10 10 0 Let’s look at the other two insertions causing problems. Recall they are mirrors. They both deal with “inside” insertions on the tree’s interior: right-left and left-right -2 +1 99 44 0 77 Insertion to right subtree of left child #4 +2 99 11 Insertion to 11 +1 right subtree of right child 13 0 13
31. 31. Double Rotation #2 These insertions are not solved with a single rotation. +1 -2 9 9 4 4 7 7 #3 Insertion to left subtree of right child +2 9 9 -1 11 11 0 10 10 0 Insertion to right subtree of left child Instead, two single rotations, left-right and right-left are performed.
32. 32. Why Doesn’t Single Rotation Work? X X Single Right W W A C B W W Rotation Attempted X X A B C Unbalanced insertions into the “interior” of the tree remain unbalanced with only a single rotation
33. 33. Double Rotation #3 +2 0 9 9 -1 Insertion to left subtree 11 11 of right child 0 10 10 10 10 0 0 9 11 9 11 (trivial case) Y Y X X X X W W A Y Y D B C A W W B C D
34. 34. Double Rotation #4 -2 Insertion to right subtree of left child +1 0 9 9 4 4 7 7 7 7 0 4 4 0 Y Y W W A X X W W X X D B 9 9 C 0 A Y Y B C D
35. 35. Double Rotation W A 99 Y 77 44 33 B 55 88 66 Magic 11 11 X D X W 14 14 Step? A 33 99 66 44 77 55 B 11 11 Y 88 14 14 D Solved: Move up X! W becomes X’s left child Y becomes X’s right child W’s right child becomes X’s left child X’s right child (if any) becomes Y’s left child
36. 36. Double Rotation Strategy On a left-right insert, we first rotate left 9 9 4 4 D 7 7 A 7 7 at 4 D 4 4 C B C A SRR 9 9 SLR B 7 7 4 4 at 9 A 9 9 B C D Double left rotate = single left rot + single right rot This places the tree in a position for which we already have a working solution. (Code reuse!)
37. 37. Double Rotation Strategy 9 9 11 11 A 10 10 D B C SRR at 11 A SLR 9 9 10 10 11 11 10 10 9 9 at 9 B A C 11 11 B C D Similarly, we perform a right and left rotation on insertions that went left-right. The first rotation reforms the tree into an exterior imbalance. We already have a way of fixing this: single left rotations D