CSE : AVL TREES
2 Balanced Binary Trees Recall: Worst case for find in a BST is : Worst case for find in a Balanced BST is:
3 Balanced Binary Trees Deterministic Balancing Change insert and delete operations to ensure that the tree always stays balanced TODAY Randomized Balancing Use random numbers to determine the values of the nodes, independent from the keys The resulting tree will probably be balanced (e.g., more like the average case), but not guaranteed LATER
Height of a node Mark the height of each node in the tree. 4 42 67 What is the height of node with key 53? A. One B. Two C. Three D. Zero 53 9 Height of a node = Depth of a node=
Local computation of height of a node 5 Height of a node= max{ Height of left child, Height of right child} + 42 67 53 9 Goal of AVL trees: For each node, maintain the difference between height of left and right children to within +/ Each node maintains then a balance factor: Balance factor= height of the right child height of left child
Which of the following is/are balanced trees? And thus can become AVL trees by adding the balance factors 6 A 42 67 B 42 67 C 42 67 53 9 53 9 53 9 87 87 D. A&C E. A&B&C Annotate the trees with balance factors (for those that are balanced)
Finding an element in a AVL Tree is worst case O(logN)! How would you prove this (we won t, but imagine we wanted to)? A. Come up with a formula that shows that the height of the tallest AVL tree with N nodes is never bigger than c*logn + k, for some c and k (assuming large N). B. Come up with a formula that shows that the number of nodes, N, in an AVL tree is never bigger than c*logn + k for some c and k (assuming large N) C. Come up with a formula that shows that the height of the tree never grows by more than log(n) when you insert a node. D. Run some empirical tests to show that for any size tree, the time to find an element is never more than some formula c*logn + k for some c and k (assuming large N) 7
8 An AVL Tree is worst case O(logN) to find an element! (We might prove this later) How would you prove this? A. Come up with a formula that shows that the height of the tallest AVL tree with N nodes is never bigger than c*logn + k, for some c and k (assuming large N). The key to this proof is showing that the height stays small, no matter how legally unbalanced the tree is. But how does the tree stay balanced as new nodes are inserted??
9 Inserting and rebalancing 3 5 7 2 6 85 5 5 65 8 9 4 55 Insert 3
Inserting and rebalancing 3 5 7 2 6 85 5 5 65 8 9 3 Propagate news up 4 55 Insert 3
Inserting and rebalancing 3 5 7 2 6 85 5 We re OK, Keep going up 5 65 8 9 3 4 55 Insert 3
2 Inserting and rebalancing 3 Uh oh! 5 7-2 2 6 85 5 5 65 8 9 3 4 55 Insert 3
3 Rotations X y y X A B C A B C
4 Inserting and rebalancing Rotate! Left or right? cut 5 7 3-2 2 6 85 5 5 65 8 9 3 4 55 Insert 3
5 Inserting and rebalancing 3 5 7-2 2 6 85 5 lift 5 65 8 9 3 4 55 Insert 3
6 Inserting and rebalancing Adjust and reattach 5 7 3 5 2 6 85-2 3 5 65 8 9 4 55 Insert 3
7 We just did a single rotation of 5 around A.k.a. a single right rotation Done! 5 7 3 5 2 6 85 3 5 65 8 9 4 55 Insert 3
8 Single rotation practice 3 5 7 2 6 What could you insert into this AVL tree that would result in a single right rotation? A. 7 B. C. 5 D. 66
9 Single rotation practice 3 5 7 2 6 Insert 5. Draw the resulting AVL tree. (Don t peek)
2 Single rotation practice 3-2 5 7 2 6 5 After insertion
2 Single rotation practice 3 5 6 2 5 7 After rotation
22 Single rotation is not enough 3 5 7 2 6 What happens if we insert 66?
23 Single rotation is not enough 3 cut -2 5 7 2 6 66
24 Is this a valid rotation? A. Yes B. No 3-2 5 7 lift 6 2 6 66 66 7
25 Invalid rotation! 3-2 5 6 2 66 7 UH OH!
26 Double rotation to the rescue 3-2 5 7 2 6 66 Single rotations only work when involved nodes are in a line So we will first rotate left around 6, then we can rotate right around 7.
27 Double rotation to the rescue -2 5 7 3 cut 2 6 66 Single rotations only work when involved nodes are in a line So we will first rotate left around 6, then we can rotate right around 7.
28 Double rotation to the rescue 3-2 5 7 2 6 lift 66 Single rotations only work when involved nodes are in a line So we will first rotate left around 6, then we can rotate right around 7.
29 Double rotation to the rescue 3-2 5 7 2 66 adjust 6 Single rotations only work when involved nodes are in a line So we will first rotate left around 6, then we can rotate right around 7.
3 Double rotation to the rescue 3-2 5 7 2 66 reattach 6 Single rotations only work when involved nodes are in a line So we will first rotate left around 6, then we can rotate right around 7.
3 Double rotation to the rescue 3 A -2 5 7 B 2 6 C 66 Single rotations only work when involved nodes are in a line So we will first rotate left around 6, then we can rotate right around 7. Where in the tree above should I cut to start this rotation?
32 Double rotation to the rescue 5 3 2 6 66 7 Single rotations only work when involved nodes are in a line So we will first rotate left around 6, then we can rotate right around 7.
33 It s sometimes even more complicated 3 5 66 6 7 Insert 64 do we need a double or a single rotation? A. Double B. Single C. No rotation needed
34 Where is the tree out of balance? 3 5 66 6 7 64 A. Node 3 B. Node 66 C. Node 6 D. Node 64
35 Rotate right around 66 to make a straight line 3 5 66 6 7 64
36 Rotate right around 66 to make a straight line 3 5 66 6 7 64
37 Rotate right around 66 to make a straight line 5 3 6 66 64 7 UH OH! Where do we put 64?? Are we stuck?
38 Rotate right around 66 to make a straight line 3 5 6 66 64 7 A. No, sometimes this doesn t work B. Yes, this will always work Will 64 always reattach there?
39 Finishing the rotation to balance the tree 6 3 66 5 64 7