unit 7 avl trees
play

Unit #7: AVL Trees CPSC 221: Algorithms and Data Structures Lars - PowerPoint PPT Presentation

Unit #7: AVL Trees CPSC 221: Algorithms and Data Structures Lars Kotthoff 1 larsko@cs.ubc.ca 1 With material from Will Evans, Steve Wolfman, Alan Hu, Ed Knorr, and Kim Voll. Unit Outline Binary search trees Balance implies shallow


  1. Unit #7: AVL Trees CPSC 221: Algorithms and Data Structures Lars Kotthoff 1 larsko@cs.ubc.ca 1 With material from Will Evans, Steve Wolfman, Alan Hu, Ed Knorr, and Kim Voll.

  2. Unit Outline ▷ Binary search trees ▷ Balance implies shallow (shallow is good) ▷ How to achieve balance ▷ Single and double rotations ▷ AVL tree implementation

  3. Learning Goals ▷ Compare and contrast balanced/unbalanced trees. ▷ Describe and apply rotation to a BST to achieve a balanced tree. ▷ Recognize balanced binary search trees (among other tree types you recognize, e.g., heaps, general binary trees, general BSTs).

  4. Dictionary ADT Implementations Worst Case Runtime: insert find delete (after find) Linked list Unsorted array Sorted array Hash table

  5. Binary Search in a Sorted List 2 4 5 7 8 9 12 14 17 20 21 25 2 4 5 7 8 12 14 17 20 21 25 2 4 7 8 12 14 20 21 25 4 8 14 20 25 int bSearch( int A[], int key, int i, int j) { if (j < i) return -1; int m = (i + j) / 2; if (key < A[m]) return bSearch(A, key, i, m-1); else if (key > A[m]) return bSearch(A, key, m+1, j); else return m; }

  6. Binary Search Tree as Dictionary Data Structure 9 5 17 Binary tree property 2 8 14 20 ▷ each node has ≤ 2 children 4 7 12 25 Search tree property 21 ▷ all keys in left subtree smaller than node’s key ▷ all keys in right subtree larger than node’s key Result: easy to find any given key

  7. In-, Pre-, Post-Order Traversal 10 5 15 2 9 20 7 17 30 In-order: 2, 5, 7, 9, 10, 15, 17, 20, 30 Pre-order: Post-order:

  8. Beauty is Only O (log n ) Deep Binary Search Trees are fast if they’re shallow. Know any shallow trees? ▷ perfectly complete ▷ almost complete (except the last level, like a heap) ▷ anything else?

  9. Beauty is Only O (log n ) Deep Binary Search Trees are fast if they’re shallow. Know any shallow trees? ▷ perfectly complete ▷ almost complete (except the last level, like a heap) ▷ anything else? What matters here? Siblings should have about the same height.

  10. Balance x 5 7 balance( x ) = height( x .left) − height( x .right) height( NULL ) = − 1 . If for all nodes x , ▷ balance( x ) = 0 then perfectly balanced. ▷ | balance( x ) | is small then balanced enough. ▷ − 1 ≤ balance( x ) ≤ 1 then tree height ≤ c lg n where c < 2 .

  11. AVL (Adelson-Velsky and Landis) Tree 9 5 17 Binary tree property 2 8 14 20 ▷ each node has ≤ 2 children 4 7 12 18 25 Search tree property 21 ▷ all keys in left subtree smaller than node’s key ▷ all keys in right subtree larger than node’s key Balance property ▷ For all nodes x , − 1 ≤ balance( x ) ≤ 1 Result: height is Θ(log n ) .

  12. Is this an AVL tree? 9 4 17 2 8 20 7 18 25

  13. An AVL Tree key 9 height 3 3 children 9 2 2 4 20 0 1 1 0 2 8 17 25 0 0 7 18

  14. How Do We Stay Balanced? Suppose we start with a balanced search tree (an AVL tree). 3 4 9 9 insert 5 2 2 3 2 4 20 4 20 0 1 1 0 0 2 1 0 2 8 17 25 2 8 17 25 0 0 1 0 7 18 7 18 0 5 It’s no longer an AVL tree. What can we do?

  15. How Do We Stay Balanced? Suppose we start with a balanced search tree (an AVL tree). 3 4 9 9 insert 5 2 2 3 2 4 20 4 20 0 1 1 0 0 2 1 0 2 8 17 25 2 8 17 25 0 0 1 0 7 18 7 18 0 5 It’s no longer an AVL tree. What can we do? ROTATE!

  16. Rotation B A z y x

  17. Rotation B A z y x

  18. Rotation A B z y x

  19. Rotation A B z y x

  20. Rotation A B x y z

  21. Time Complexity of Rotation ▷ O( 1 ) ▷ O( lg n ) ▷ O( n ) ▷ O( n lg n ) ▷ O( n 2 ) ▷ none of the above

  22. Single Rotation 4 3 9 9 3 2 2 2 4 20 4 20 0 2 1 0 0 1 1 0 2 8 17 25 2 7 17 25 1 0 0 0 0 7 18 5 8 18 0 5

  23. Single Rotation rotateRight is shown. There’s also a symmetric rotateLeft . h + 2 h + 1 B A h + 1 h h − 1 h A B z h h − 1 h − 1 h − 1 x y y z x insert occurred here After rotation, subtree’s height is the same as before insert. So heights of ancestors don’t change.

  24. Double Rotation Start with 3 4 9 9 insert 6 2 2 3 2 4 20 4 20 0 1 1 0 0 2 1 0 2 7 17 25 2 7 17 25 0 0 0 1 0 0 5 8 18 5 8 18 0 6 A single rotation won’t fix this.

  25. Double Rotation Start with 3 4 9 9 insert 6 2 2 3 2 4 20 4 20 0 1 1 0 0 2 1 0 2 7 17 25 2 7 17 25 0 0 0 1 0 0 5 8 18 5 8 18 0 6 A single rotation won’t fix this. DOUBLE ROTATE!

  26. Double Rotation 4 3 9 9 3 2 2 2 4 20 5 20 0 2 1 0 1 1 1 0 2 7 17 25 4 7 17 25 1 0 0 4 0 0 0 0 5 8 18 9 2 6 8 18 0 3 2 6 4 20 0 2 1 0 2 5 17 25 rotateLeft rotateRight 1 0 7 18 0 0 6 8

  27. Double Rotation doubleRotateLeft is shown. There’s also a symmetric doubleRotateRight . h + 2 h + 1 A B h + 1 h − 1 h h C A C w h − 1 h − 1 h − 1 h − 1 h B y z w x z h − 1 y x insert occurred here or here Either x or y increased to height h − 1 after insert. After rotation, subtree’s height is the same as before insert. So height of ancestors doesn’t change.

  28. Insert Algorithm 1. Find location for new key. 2. Add new leaf node with new key. 3. Go up tree from new leaf searching for imbalance. 4. At lowest unbalanced ancestor: Case LL: rotateRight Case RR: rotateLeft Case LR: doubleRotateRight Case RL: doubleRotateLeft The case names are the first two steps on the path from the unbalanced ancestor to the new leaf.

  29. Insert: No Imbalance Insert(3) 3 10 1 2 5 15 0 0 0 1 2 9 12 20 0 0 17 30

  30. Insert: No Imbalance 3 10 2 2 5 15 1 0 0 1 2 9 12 20 0 0 0 3 17 30

  31. Insert: Imbalance Case RR Insert(33) 3 10 2 2 5 15 1 0 0 1 2 9 12 20 0 0 0 3 17 30

  32. Case RR: rotateLeft ? 3 10 10 2 3 2 2 5 15 5 20 1 0 0 2 1 0 1 1 2 9 12 20 2 9 15 30 0 0 1 0 0 0 0 0 3 17 30 3 12 17 33 0 33

  33. Single Rotation Code a a A B b B A x z y y x z void rotateLeft(Node *&a) { Node* b = a->right; a->right = b->left; b->left = a; updateHeight(a); updateHeight(b); a = b; }

  34. Insert: Imbalance Case RL Insert(18) 3 10 2 2 5 15 1 0 0 1 2 9 12 20 0 0 0 3 17 30

  35. Case RL: doubleRotateLeft 3 ? 10 10 2 3 2 2 5 15 5 17 1 0 0 2 1 0 1 1 2 9 12 20 2 9 15 20 0 1 0 0 0 0 0 3 17 30 3 12 18 30 0 18 ? 10 rotateRight rotateLeft 2 3 5 15 1 0 0 2 2 9 12 17 0 1 3 20 0 0 18 30

  36. Double Rotation Code a a A B C A C w a B A y z w x z B y x w C x y z void doubleRotateLeft(Node *&a) { rotateRight(a->right); rotateLeft(a); }

  37. Delete 1. Delete as for general binary search tree. (This way we reduce the problem to deleting a node with 0 or 1 child.) 2. Go up tree from deleted node searching for imbalance (and fixing heights). 3. Fix all unbalanced ancestors (bottom-up). 6 13 4 5 5 2 3 2 8 0 1 1 2 1 3 6 10 0 4 7 9 11 12

  38. Thinking about AVL trees Observations ▷ Binary search trees that allow only slight imbalance. ▷ Worst-case O (log n ) time for find, insert, and delete. ▷ Elements (even siblings) may be scattered in memory. Realities ▷ For large data sets, disk accesses dominate runtime. Could we have perfect balance if we relax binary tree restriction?

Recommend


More recommend