CS261 Data Structures AVL Trees Goals Pros/Cons of - - PowerPoint PPT Presentation
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
Goals ¡ ¡
- Pros/Cons ¡of ¡a ¡BST ¡
- AVL ¡Solu;on ¡
– Height-‑Balanced ¡Trees ¡
Binary ¡Search ¡Tree: ¡Balance ¡
50 25 75 35 60 85 30 45 65 80 20
Binary ¡Search ¡Tree: ¡Balance ¡
50 75 85 90 92
¡Complete ¡Binary ¡ ¡Trees ¡
- Very ¡costly ¡to ¡maintain ¡a ¡
¡complete ¡binary ¡tree ¡
Alex ¡ Abner ¡ Angela ¡ Adela ¡ Alice ¡ Adam ¡ Abigail ¡
Add ¡Adam ¡ to ¡tree ¡
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 ¡
- Null ¡child ¡height ¡= ¡-‑1 ¡
3(3) ¡ 9(0) ¡ 1(0) ¡ 8(2) ¡ 2(1) ¡ 4(0) ¡ 5(1) ¡ 6(0) ¡
Height-‑Balanced ¡Tree ¡
Quiz ¡
- Are ¡these ¡trees ¡height ¡balanced? ¡ ¡If ¡not, ¡which ¡
node ¡is ¡out ¡of ¡balance? ¡
3 ¡ 2 ¡ 32 ¡ 50 ¡ 40 ¡ 16 ¡ 50 ¡ 70 ¡ 60 ¡ 16 ¡ 55 ¡
Quiz ¡
- Are ¡these ¡trees ¡height ¡balanced? ¡ ¡If ¡not, ¡which ¡
node ¡is ¡out ¡of ¡balance? ¡
100 ¡ 160 ¡ 150 ¡ 50 ¡ 155 ¡ 100 ¡ 130 ¡ 120 ¡ 50 ¡ 115 ¡ 110 ¡
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?? ¡
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 ¡
¡
- When ¡unbalanced, ¡performs ¡a ¡“rota;on” ¡to ¡balance ¡the ¡
tree ¡ ¡
1(2) ¡
Node ¡data ¡ Height ¡field ¡
3(0) ¡ 2(1) ¡ 1(2) ¡ 3(0) ¡ 1(0) ¡ 2(1) ¡
Rotate ¡ leK ¡ Unbalanced ¡ node ¡
¡Two ¡Cases ¡to ¡Remember ¡(Mirror ¡for ¡Right) ¡
1 ¡ 2 ¡ 1 ¡ 2 ¡
AKer ¡opera;on ¡
1 ¡ 2 ¡
AKer ¡opera;on ¡
1 ¡ 2 ¡
Heavy ¡leK ¡ child ¡ becomes ¡ heavy ¡itself ¡
- n ¡the ¡leK! ¡
(Single ¡Rot) ¡ unbalanced ¡ Heavy ¡leK ¡ child ¡ becomes ¡ heavy ¡itself ¡
- n ¡the ¡
right ¡! ¡ (Double ¡Rot) ¡
Fix ¡with ¡Rota;ons… ¡
3(0) ¡ 2(1) ¡ 1(2) ¡
Case ¡1: ¡Single ¡Rota;on ¡
¡
¡
3(0) ¡ 2(1) ¡ 1(2) ¡ 3(0) ¡ 1(0) ¡ 2(1) ¡
Rotate ¡ leK ¡ Unbalanced ¡ “top” ¡node ¡
Case ¡1: ¡Single ¡Rota;on ¡Example ¡2 ¡
1(0) ¡ 2(3) ¡ 6(0) ¡ 3(0) ¡ 4(2) ¡ 5(1) ¡ 1(0) ¡ 2(1) ¡ 6(0) ¡ 3(0) ¡ 4(2) ¡ 5(1) ¡
Rotate ¡ leK ¡ Unbalanced ¡ “top” ¡node ¡ New ¡“top” ¡node ¡
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 ¡
1(0) ¡ 2(3) ¡ 6(0) ¡ 3(0) ¡ 4(2) ¡ 5(1) ¡ 1(0) ¡ 2(1) ¡ 6(0) ¡ 3(0) ¡ 4(2) ¡ 5(1) ¡
Rotate ¡ leK ¡ “Current” ¡ top ¡node ¡ “New” ¡top ¡node ¡
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 ¡
2 ¡ 1 ¡
Unbalanced ¡ node ¡
3 ¡
“Heavy” ¡ right ¡child ¡
2(0) ¡ 3(2) ¡ 1(1) ¡
Rotate ¡ leK ¡
Doesn’t ¡ work!!! ¡
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) ¡
2(0) ¡ 1(2) ¡
Unbalanced ¡ ¡node ¡
3(1) ¡
“Heavy” ¡ right ¡child ¡ Rotate ¡heavy ¡ child ¡right ¡
3(0) ¡ 2(1) ¡ 1(2) ¡ 3(0) ¡ 1(0) ¡ 2(1) ¡
Rotate ¡ ¡ unbalanced ¡ node ¡leK ¡
Case ¡1: ¡ ¡Single ¡Rota;on ¡(another ¡view) ¡
1 ¡ 2 ¡ 1 ¡ 2 ¡
- Have ¡a ¡node ¡(2) ¡that ¡is ¡heavy ¡on ¡the ¡leK ¡(1) ¡
- Opera;on ¡makes ¡(2) ¡unbalanced ¡and ¡that ¡heavy ¡
child ¡heavy ¡on ¡leK ¡
AKer ¡Inser;on ¡
Case ¡1: ¡ ¡Single ¡Rota;on ¡
1 ¡ 2 ¡ 2 ¡ 1 ¡
- Single ¡rota;on ¡fixes ¡the ¡problem ¡
Single ¡Rota;on ¡
Case ¡2: ¡ ¡Double ¡Rota;on ¡
1 ¡ 3 ¡ 1 ¡ 3 ¡
- Have ¡a ¡node ¡(3) ¡with ¡a ¡heavy ¡leK ¡child ¡ ¡(1) ¡
- Opera;on ¡makes ¡(3) ¡unbalanced ¡and ¡(1) ¡is ¡heavy ¡
- n ¡the ¡right ¡
AKer ¡Dele;on ¡
2 ¡ 2 ¡
Case ¡2: ¡ ¡Double ¡Rota;on ¡
1 ¡ 3 ¡
- First, ¡rotate ¡heavy ¡child ¡(1) ¡to ¡the ¡leK ¡
AKer ¡Rota;on ¡1 ¡
2 ¡ 1 ¡ 3 ¡ 2 ¡
Case ¡2: ¡ ¡Double ¡Rota;on ¡
- Next, ¡rotate ¡unbalanced ¡node ¡(3) ¡to ¡the ¡right ¡
AKer ¡Rota;on ¡2 ¡
1 ¡ 3 ¡ 2 ¡ 1 ¡ 3 ¡ 2 ¡
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 ¡
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 ¡
3(3) ¡ 9(0) ¡ 1(0) ¡ 8(2) ¡ 2(1) ¡ 4(0) ¡ 5(1) ¡ 6(0) ¡
More ¡Examples… ¡
Add ¡data: ¡7 ¡
Balanced ¡Tree ¡
“Heavy” ¡leK ¡ child ¡
Unbalanced ¡Tree ¡
3(4) ¡ 9(0) ¡ 1(0) ¡ 8(3) ¡ 2(1) ¡ 4(0) ¡ 5(2) ¡ 7(0) ¡ 6(1) ¡
Added ¡to ¡right ¡side ¡
- f ¡heavy ¡leK ¡child ¡
Unbalanced ¡ “top”node ¡
Tree ¡S;ll ¡Unbalanced ¡
3(4) ¡ 9(0) ¡ 1(0) ¡ 8(3) ¡ 2(1) ¡ 4(0) ¡ 5(2) ¡ 7(0) ¡ 6(1) ¡
Single ¡rota;on ¡
AVL ¡Trees: ¡Double ¡Rota;on ¡Example ¡
Unbalanced ¡Tree ¡
3(4) ¡ 1(0) ¡ 2(1) ¡ 7(0) ¡ 6(1) ¡ 9(0) ¡ 8(2) ¡ 5(3) ¡ 4(0) ¡
Unbalanced “top” node (still)
Tree ¡S;ll ¡Unbalanced, ¡but ¡… ¡
Rotate ¡heavy ¡child ¡
AVL ¡Trees: ¡Double ¡Rota;on ¡Example ¡
“Heavy” ¡leK ¡ child ¡
3(4) ¡ 9(0) ¡ 1(0) ¡ 8(3) ¡ 2(1) ¡ 4(0) ¡ 5(1) ¡ 7(0) ¡ 6(2) ¡ 3(4) ¡ 9(0) ¡ 1(0) ¡ 8(3) ¡ 2(1) ¡ 4(0) ¡ 5(2) ¡ 7(0) ¡ 6(1) ¡
Unbalanced ¡Tree ¡
Tree ¡Now ¡Balanced ¡
Rotate ¡top ¡node ¡
AVL ¡Trees: ¡Double ¡Rota;on ¡Example ¡
Unbalanced ¡Tree ¡ (aKer ¡1st ¡rota;on) ¡
3(3) ¡ 1(0) ¡ 2(1) ¡ 7(0) ¡ 3(4) ¡ 9(0) ¡ 1(0) ¡ 8(3) ¡ 2(1) ¡ 4(0) ¡ 5(1) ¡ 7(0) ¡ 6(2) ¡
Unbalanced ¡ “top” ¡node ¡
4(0) ¡ 5(1) ¡ 6(2) ¡ 9(0) ¡ 8(1) ¡
Your ¡Turn ¡
- Worksheet: ¡AVL ¡Prac;ce ¡