cs261 data structures
play

CS261 Data Structures AVL Trees Goals Pros/Cons of - PowerPoint PPT Presentation

CS261 Data Structures AVL Trees Goals Pros/Cons of a BST AVL Solu;on Height-Balanced Trees Binary Search Tree: Balance 50 25 75 20 35 60


  1. CS261 ¡Data ¡Structures ¡ AVL ¡Trees ¡ ¡

  2. Goals ¡ ¡ • Pros/Cons ¡of ¡a ¡BST ¡ • AVL ¡Solu;on ¡ – 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 90 92

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

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

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

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

  9. Height-­‑Balanced ¡Trees ¡ • Mathema;cally, ¡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 ¡;me ¡propor;onal ¡to ¡the ¡path ¡length ¡ are ¡s;ll ¡O(log ¡ n ) ¡ • So.....How ¡do ¡we ¡maintain ¡height ¡balance?? ¡

  10. AVL ¡Trees ¡ • Named ¡aKer ¡the ¡inventors ’ ¡ini;als: ¡ ¡G.M. ¡ Adelson-­‑Velskii, ¡E.M. ¡Landis ¡ • Maintain ¡the ¡height ¡balanced ¡property ¡of ¡a ¡BST ¡ through ¡a ¡series ¡of ¡rota;ons ¡ 1 (2) ¡ Node ¡data ¡ Height ¡field ¡ ¡ • When ¡unbalanced, ¡performs ¡a ¡ “ rota;on ” ¡to ¡balance ¡the ¡ tree ¡ 1 (2) ¡ 2 (1) ¡ Unbalanced ¡ node ¡ 2 (1) ¡ Rotate ¡ 1 (0) ¡ 3 (0) ¡ leK ¡ ¡ 3 (0) ¡

  11. ¡Two ¡Cases ¡to ¡Remember ¡(Mirror ¡for ¡Right) ¡ 2 ¡ 2 ¡ unbalanced ¡ AKer ¡opera;on ¡ 1 ¡ 1 ¡ Heavy ¡leK ¡ child ¡ becomes ¡ heavy ¡itself ¡ on ¡the ¡leK! ¡ (Single ¡Rot) ¡ 2 ¡ 2 ¡ Heavy ¡leK ¡ 1 ¡ 1 ¡ child ¡ AKer ¡opera;on ¡ becomes ¡ heavy ¡itself ¡ on ¡the ¡ right ¡! ¡ (Double ¡Rot) ¡

  12. Fix ¡with ¡Rota;ons… ¡ 1 (2) ¡ 2 (1) ¡ 3 (0) ¡

  13. Case ¡1: ¡Single ¡Rota;on ¡ ¡ 1 (2) ¡ 2 (1) ¡ Unbalanced ¡ ¡ “top” ¡node ¡ 2 (1) ¡ Rotate ¡ 1 (0) ¡ 3 (0) ¡ leK ¡ 3 (0) ¡

  14. Case ¡1: ¡Single ¡Rota;on ¡Example ¡2 ¡ New ¡ “ top ” ¡node ¡ 2 (3) ¡ 4 (2) ¡ Unbalanced ¡ “ top ” ¡node ¡ 1 (0) ¡ 2 (1) ¡ 4 (2) ¡ 5 (1) ¡ Rotate ¡ leK ¡ 1 (0) ¡ 3 (0) ¡ 3 (0) ¡ 5 (1) ¡ 6 (0) ¡ 6 (0) ¡

  15. AVL ¡Trees: ¡ Rota;on ¡Pseudocode ¡ Pseudocode ¡to ¡rotate ¡current ¡( “ top ” ) ¡node ¡ le$ : ¡ 1. New ¡top ¡node ¡is ¡the ¡current ¡top ¡node’s ¡right ¡child ¡ 2. Current ¡node ’ s ¡new ¡right ¡child ¡is ¡the ¡new ¡top ¡node ’ s ¡leK ¡child ¡ 3. New ¡top ’ s ¡leK ¡child ¡is ¡the ¡current ¡node ¡ 4. Set ¡height ¡of ¡current ¡node ¡ ¡ 5. Set ¡height ¡of ¡new ¡top ¡node ¡ 6. Return ¡new ¡top ¡node ¡ “New” ¡top ¡node ¡ 2 (3) ¡ 4 (2) ¡ “Current” ¡ top ¡node ¡ 1 (0) ¡ 4 (2) ¡ 2 (1) ¡ 5 (1) ¡ Rotate ¡ leK ¡ 5 (1) ¡ 1 (0) ¡ 3 (0) ¡ 6 (0) ¡ 3 (0) ¡ 6 (0) ¡

  16. Case ¡2: ¡ Double ¡Rota;on ¡ • Some;mes ¡a ¡single ¡rota;on ¡will ¡not ¡fix ¡the ¡ problem : ¡ – Can ¡happen ¡when ¡an ¡inser;on ¡is ¡made ¡on ¡the ¡leK ¡ (or ¡ right) ¡side ¡of ¡a ¡node ¡that ¡is ¡itself ¡a ¡heavy ¡right ¡ (or ¡leK) ¡ child ¡ Unbalanced ¡ 1 ¡ 3 (2) ¡ node ¡ Doesn ’ t ¡ 1 (1) ¡ 3 ¡ work!!! ¡ “ Heavy ” ¡ Rotate ¡ right ¡child ¡ leK ¡ 2 ¡ 2 (0) ¡

  17. Case ¡2: ¡ Double ¡Rota;on ¡ • Fortunately, ¡this ¡case ¡is ¡easily ¡handled ¡by ¡ rota)ng ¡the ¡child ¡before ¡the ¡regular ¡rota;on: ¡ 1. First ¡rotate ¡the ¡heavy ¡right ¡ (or ¡leK) ¡child ¡to ¡the ¡right ¡ (or ¡ leK) ¡ 2. Rotate ¡the ¡unbalanced ¡node ¡to ¡the ¡leK ¡ (or ¡right) ¡ Unbalanced ¡ 1 (2) ¡ 1 (2) ¡ ¡node ¡ 2 (1) ¡ 3 (1) ¡ 2 (1) ¡ “ Heavy ” ¡ Rotate ¡heavy ¡ Rotate ¡ ¡ 1 (0) ¡ 3 (0) ¡ right ¡child ¡ child ¡right ¡ unbalanced ¡ 2 (0) ¡ 3 (0) ¡ node ¡leK ¡

  18. Case ¡1: ¡ ¡Single ¡Rota;on ¡(another ¡view) ¡ • Have ¡a ¡node ¡(2) ¡that ¡is ¡heavy ¡on ¡the ¡leK ¡(1) ¡ • Opera;on ¡makes ¡(2) ¡unbalanced ¡and ¡that ¡heavy ¡ child ¡heavy ¡on ¡leK ¡ 2 ¡ 2 ¡ AKer ¡Inser;on ¡ 1 ¡ 1 ¡

  19. Case ¡1: ¡ ¡Single ¡Rota;on ¡ • Single ¡rota;on ¡fixes ¡the ¡problem ¡ 1 ¡ 2 ¡ Single ¡Rota;on ¡ 1 ¡ 2 ¡

  20. Case ¡2: ¡ ¡Double ¡Rota;on ¡ • Have ¡a ¡node ¡(3) ¡with ¡a ¡heavy ¡leK ¡child ¡ ¡(1) ¡ • Opera;on ¡makes ¡(3) ¡unbalanced ¡and ¡(1) ¡is ¡heavy ¡ on ¡the ¡right ¡ 3 ¡ 3 ¡ AKer ¡Dele;on ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡

  21. Case ¡2: ¡ ¡Double ¡Rota;on ¡ • First, ¡rotate ¡heavy ¡child ¡(1) ¡to ¡the ¡leK ¡ 3 ¡ 3 ¡ 1 ¡ AKer ¡Rota;on ¡1 ¡ 2 ¡ 2 ¡ 1 ¡

  22. Case ¡2: ¡ ¡Double ¡Rota;on ¡ • Next, ¡rotate ¡unbalanced ¡node ¡(3) ¡to ¡the ¡right ¡ 3 ¡ 2 ¡ 2 ¡ AKer ¡Rota;on ¡2 ¡ 1 ¡ 3 ¡ 1 ¡

  23. AVL ¡Trees: ¡ Balacing ¡Pseudocode ¡ Balancing ¡pseudocode ¡ (to ¡rebalance ¡an ¡unbalanced ¡node) : ¡ If ¡leK ¡child ¡is ¡tallest ¡(by ¡more ¡than ¡1): ¡ ¡If ¡leK ¡child ¡is ¡heavy ¡on ¡the ¡right ¡side: ¡ ¡ ¡ ¡ // ¡Double ¡rota;on ¡needed. ¡ ¡ ¡Rotate ¡the ¡leK ¡child ¡to ¡the ¡leK ¡ ¡Rotate ¡unbalanced ¡“top” ¡node ¡to ¡the ¡right ¡ Else ¡if ¡right ¡child ¡is ¡tallest(by ¡more ¡than ¡1) ¡ ¡ ¡If ¡right ¡child ¡is ¡heavy ¡on ¡the ¡leK ¡side: ¡ ¡ ¡ ¡ // ¡Double ¡rota;on ¡needed. ¡ ¡ ¡Rotate ¡the ¡right ¡child ¡to ¡the ¡right ¡ ¡Rotate ¡unbalanced ¡“top” ¡node ¡to ¡the ¡leK ¡ Return ¡new ¡“ ¡top” ¡node ¡

  24. Rota;ons ¡ • Can ¡be ¡the ¡result ¡of ¡addi;ons ¡or ¡removals ¡ • All ¡cases ¡hold ¡for ¡the ¡“right” ¡side ¡as ¡well, ¡just ¡ replace ¡all ¡leKs ¡with ¡rights ¡and ¡all ¡rights ¡with ¡ leKs ¡

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

  26. AVL ¡Trees: ¡ Double ¡Rota;on ¡Example ¡ Unbalanced ¡Tree ¡ Tree ¡S;ll ¡Unbalanced ¡ Unbalanced 3 (4) ¡ 3 (4) ¡ “ top ” node Single ¡rota;on ¡ (still) 2 (1) ¡ 8 (3) ¡ 2 (1) ¡ 5 (3) ¡ 1 (0) ¡ 5 (2) ¡ 9 (0) ¡ 1 (0) ¡ 4 (0) ¡ 8 (2) ¡ 4 (0) ¡ 6 (1) ¡ 6 (1) ¡ 9 (0) ¡ 7 (0) ¡ 7 (0) ¡

  27. AVL ¡Trees: ¡ Double ¡Rota;on ¡Example ¡ Unbalanced ¡Tree ¡ Tree ¡S;ll ¡Unbalanced, ¡but ¡… ¡ 3 (4) ¡ 3 (4) ¡ Rotate ¡heavy ¡child ¡ 2 (1) ¡ 8 (3) ¡ 2 (1) ¡ 8 (3) ¡ 1 (0) ¡ 5 (2) ¡ 9 (0) ¡ 1 (0) ¡ 6 (2) ¡ 9 (0) ¡ “ Heavy ” ¡leK ¡ 4 (0) ¡ 6 (1) ¡ 5 (1) ¡ 7 (0) ¡ child ¡ 7 (0) ¡ 4 (0) ¡

  28. AVL ¡Trees: ¡ Double ¡Rota;on ¡Example ¡ Unbalanced ¡Tree ¡ Tree ¡Now ¡Balanced ¡ (aKer ¡1 st ¡rota;on) ¡ 3 (4) ¡ 3 (3) ¡ Rotate ¡top ¡node ¡ 2 (1) ¡ 8 (3) ¡ 2 (1) ¡ 6 (2) ¡ Unbalanced ¡ 1 (0) ¡ 6 (2) ¡ 9 (0) ¡ 1 (0) ¡ 5 (1) ¡ 8 (1) ¡ “ top ” ¡node ¡ 5 (1) ¡ 7 (0) ¡ 4 (0) ¡ 7 (0) ¡ 9 (0) ¡ 4 (0) ¡

  29. Your ¡Turn ¡ • Worksheet: ¡AVL ¡Prac;ce ¡

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