cse 326 data structures avl trees
play

CSE 326: Data Structures AVL Trees Richard Anderson, Steve Seitz - PowerPoint PPT Presentation

CSE 326: Data Structures AVL Trees Richard Anderson, Steve Seitz Winter 2014 1 Announcements HW 2 due now HW 3 out today 2 Balanced BST Complexity of operations depend on tree height For a BST with n nodes Want height to be ~


  1. CSE 326: Data Structures AVL Trees Richard Anderson, Steve Seitz Winter 2014 1

  2. Announcements • HW 2 due now • HW 3 out today 2

  3. Balanced BST Complexity of operations depend on tree height For a BST with n nodes • Want height to be ~ log n • “Balanced” But balancing cost must be low 28

  4. How about complete trees? This worked for heaps • balance maintained via percolate up/down • Let’s try with BST 10 5 15 3 7 13 17 1 4 6 (add 14 in rightmost leaf, percolate up) 4

  5. Balancing Trees • Many algorithms exist for keeping trees balanced – Adelson-Velskii and Landis (AVL) trees – Splay trees and other self-adjusting trees – B-trees and other multiway search trees (for very large trees) • Today we will talk about AVL trees … 5

  6. The AVL Tree Data Structure Ordering property – Same as for BST 8 Structural properties 5 11 1. Binary tree property (0,1, or 2 children) 2. Heights of left and right 2 6 10 12 subtrees of every node differ by at most 1 4 7 9 13 14 15 Result: worst case height: O(log n ) 6

  7. Recursive Height Calculation Recall : height is max number A of edges from root to a leaf h left h right What is the height at A? Define: height(null) = -1 7

  8. AVL trees or not? 6 4 8 1 11 7 12 10 6 4 8 1 5 7 11 3 2 8

  9. Goal h ∈ O(log n) •we will do this by showing: n + 1 > φ h •What’s φ? φ is the golden ratio, (1+ √ 5)/2 The golden section: –Since the Renaissance, many artists and architects have proportioned their work (e.g., length:height) to approximate the golden ratio φ 9

  10. Minimum Size of an AVL Tree • n > m(h) = minimum # of nodes in an AVL tree of height h . • Base cases: h – m (0) = m (1) = • Inductive case: h -1 h -1 – m (h) = • Can prove: h – m ( h ) > φ h - 1 h -2 h -1 11

  11. Proof that m ( h ) > φ h -1 •Base cases h =0,1: m (0) = 1 > φ 0 -1 = 0 m (1) = 2 > φ 1 -1 ≈ 0.62 •Assume true for h -2 and h -1: m ( h -2) > φ h -2 – 1 m ( h -1) > φ h -1 – 1 •Induction step: m ( h ) = m ( h -1) + m ( h -2) + 1 > ( φ h -1 - 1) + ( φ h -2 - 1) + 1 ( φ h -1 - 1) + ( φ h -2 - 1) + 1 = φ h -2 ( φ +1) – 1 = φ h -2 ( φ 2 ) – 1 = φ h - 1 m ( h ) > φ h - 1 � 12

  12. Maximum Height of an AVL Tree Suppose we have n nodes in an AVL tree of height h . We can now say: m ( h ) > φ h – 1 What does this say about n ? What does this say about the complexity of h ? 13

  13. Testing the Balance Property We need to be able to: 10 1. Track Balance 5 15 2. Detect Imbalance 2 9 20 3. Restore Balance 7 Is this AVL tree balanced? How about after insert(30)? 14

  14. An AVL Tree data 10 3 height 10 3 children 2 1 5 20 1 0 0 0 15 30 2 9 0 7 15

  15. AVL trees: find, insert • AVL find : – same as BST find. • AVL insert : – same as BST insert, except may need to “fix” the AVL tree after inserting new value. We will consider the 4 fundamental insertion cases… 16

  16. Case #1: left-left insertion (zig) h+2 a h+1 h b h h Z Y X Insert on left child’s left a h b h Z X Y 17

  17. Case #1: repair with single rotation h+3 a h+2 h b h+1 h Z Y X single rotation X < b < Y < a < Z b a 18 Height of tree before/after? Effect on Ancestors? Cost?

  18. Single rotation example 15 8 22 24 4 10 19 17 20 6 3 16 15 8 4 10 6 3 19

  19. Case #2: left-right insertion h+2 a h+1 h b h h Z Y X Insert on left child’s right a h b h Z X Y 20

  20. Case #2: repair with single rotation? h+3 a h+2 h b h h+1 Z X Y X < b < Y < a < Z Single rotation b h a h+1 h X Z Y 21 Are we better off now?

  21. Case #2: trying again h+2 Let’s break subtree Y a h+1 into pieces: h b h h c h-1 h-1 Z X U V Insert on left child’s right (at U or V) a h b h c h-1 Z X U V 22

  22. Case #2: trying again h+3 Let’s break subtree Y a h+2 into pieces: h b h+1 h c h-1 h Z X U V Insert on left child’s right (at U or V) c 23

  23. Can also do this in two rotations h+3 a h+2 h b h+1 h c h-1 h Z X U V First rotation X < b < U < c < V < a < Z a h c h-1 b h Z h V U 24 X

  24. second rotation h+3 a h+2 h c h+1 h-1 b h Z h V U X Second rotation c h+1 b a h-1 h h h U V X Z 25

  25. Double rotation example 15 19 8 4 10 17 22 6 16 3 20 24 5 15 19 17 22 16 20 24 26

  26. Double rotation, step 1 15 19 8 4 10 17 22 6 16 3 20 24 5 15 19 8 10 17 22 16 20 24 27

  27. Double rotation, step 2 15 19 8 6 17 10 22 4 16 20 24 3 5 15 19 17 22 16 20 24 28

  28. Case #3: right-left insertion h+3 a h+2 h b h+1 h c h h-1 X U V Z Double rotation h+2 c h+1 h+1 a b h h h-1 h V U 29 Z X

  29. Case #4: right-right insertion h+3 a h+2 h b h+1 h X Y Z Single rotation h+2 b h+1 h+1 a h h Z X Y 30

  30. AVL tree case summary Let a be the node where an imbalance occurs. Four cases to consider. The insertion below a is in the left child’s left subtree . (zig) 1. left child’s right subtree . (zig-zag) 2. right child’s left subtree . (zig-zag) 3. right child’s right subtree . (zig) 4. Cases 1 & 4 are solved by a single rotation: 1. Rotate between a and child Cases 2 & 3 are solved by a double rotation: 1. Rotate between a ’s child and grandchild 2. Rotate between a and a ’s new child 31

  31. Single and Double Rotations: Consider inserting one of {1, 4, 6, 8, 10, 12, 14} Which values require: 1. single rotation? 9 5 11 2 7 13 0 3 2. double rotation? 3. no rotation? 32

  32. Insertion procedure 1. Find spot for new key 2. Hang new node there with this key 3. Search back up the path for imbalance 4. If there is an imbalance: cases #1,#4: Perform single rotation and exit cases #2,#3: Perform double rotation and exit Both rotations restore subtree height to value before insert. Hence only type of rotation is sufficient per insert! 33

  33. More insert examples 3 Insert(33) 10 1 2 5 15 0 0 0 1 2 9 12 20 0 0 17 30 Unbalanced? How to fix? 34

  34. Single Rotation 4 3 10 10 1 3 1 5 15 5 0 0 0 0 2 0 2 9 12 20 2 9 0 1 17 30 0 33 35

  35. More insert examples 3 Suppose we didn’t 10 do that last insert. 1 2 5 15 Now do: Insert(18) 0 0 0 1 2 9 12 20 0 0 17 30 Unbalanced? How to fix? 36

  36. Single Rotation (oops!) 4 4 10 10 1 3 1 5 15 5 0 0 0 0 2 0 2 9 12 20 2 9 1 0 17 30 0 18 37

  37. Double Rotation 4 4 10 10 1 3 1 5 15 5 0 0 0 0 2 0 2 9 12 20 2 9 1 0 17 30 0 18 38

  38. More insert examples Insert(3) 3 10 1 2 5 17 0 0 1 1 2 9 15 20 0 0 0 12 18 30 Unbalanced? How to fix? 39

  39. Insert into an AVL tree: 5, 8, 9, 4, 2, 7, 3, 1 40

  40. AVL complexity What is the worst case complexity of a find? What is the worst case complexity of an insert? What is the worst case complexity of buildTree? 41

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend