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

cs261 data structures
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS261 ¡Data ¡Structures ¡

AVL ¡Trees ¡ ¡

slide-2
SLIDE 2

Goals ¡ ¡

  • Pros/Cons ¡of ¡a ¡BST ¡
  • AVL ¡Solu;on ¡

– Height-­‑Balanced ¡Trees ¡

slide-3
SLIDE 3

Binary ¡Search ¡Tree: ¡Balance ¡

50 25 75 35 60 85 30 45 65 80 20

slide-4
SLIDE 4

Binary ¡Search ¡Tree: ¡Balance ¡

50 75 85 90 92

slide-5
SLIDE 5

¡Complete ¡Binary ¡ ¡Trees ¡

  • Very ¡costly ¡to ¡maintain ¡a ¡

¡complete ¡binary ¡tree ¡

Alex ¡ Abner ¡ Angela ¡ Adela ¡ Alice ¡ Adam ¡ Abigail ¡

Add ¡Adam ¡ to ¡tree ¡

slide-6
SLIDE 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 ¡

  • Null ¡child ¡height ¡= ¡-­‑1 ¡

3(3) ¡ 9(0) ¡ 1(0) ¡ 8(2) ¡ 2(1) ¡ 4(0) ¡ 5(1) ¡ 6(0) ¡

Height-­‑Balanced ¡Tree ¡

slide-7
SLIDE 7

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 ¡

slide-8
SLIDE 8

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 ¡

slide-9
SLIDE 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?? ¡
slide-10
SLIDE 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 ¡

¡

  • 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 ¡

slide-11
SLIDE 11

¡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) ¡

slide-12
SLIDE 12

Fix ¡with ¡Rota;ons… ¡

3(0) ¡ 2(1) ¡ 1(2) ¡

slide-13
SLIDE 13

Case ¡1: ¡Single ¡Rota;on ¡

¡

¡

3(0) ¡ 2(1) ¡ 1(2) ¡ 3(0) ¡ 1(0) ¡ 2(1) ¡

Rotate ¡ leK ¡ Unbalanced ¡ “top” ¡node ¡

slide-14
SLIDE 14

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 ¡

slide-15
SLIDE 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 ¡

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 ¡

slide-16
SLIDE 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 ¡

2 ¡ 1 ¡

Unbalanced ¡ node ¡

3 ¡

“Heavy” ¡ right ¡child ¡

2(0) ¡ 3(2) ¡ 1(1) ¡

Rotate ¡ leK ¡

Doesn’t ¡ work!!! ¡

slide-17
SLIDE 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) ¡

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 ¡

slide-18
SLIDE 18

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 ¡

slide-19
SLIDE 19

Case ¡1: ¡ ¡Single ¡Rota;on ¡

1 ¡ 2 ¡ 2 ¡ 1 ¡

  • Single ¡rota;on ¡fixes ¡the ¡problem ¡

Single ¡Rota;on ¡

slide-20
SLIDE 20

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 ¡

slide-21
SLIDE 21

Case ¡2: ¡ ¡Double ¡Rota;on ¡

1 ¡ 3 ¡

  • First, ¡rotate ¡heavy ¡child ¡(1) ¡to ¡the ¡leK ¡

AKer ¡Rota;on ¡1 ¡

2 ¡ 1 ¡ 3 ¡ 2 ¡

slide-22
SLIDE 22

Case ¡2: ¡ ¡Double ¡Rota;on ¡

  • Next, ¡rotate ¡unbalanced ¡node ¡(3) ¡to ¡the ¡right ¡

AKer ¡Rota;on ¡2 ¡

1 ¡ 3 ¡ 2 ¡ 1 ¡ 3 ¡ 2 ¡

slide-23
SLIDE 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 ¡

slide-24
SLIDE 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 ¡

slide-25
SLIDE 25

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 ¡

slide-26
SLIDE 26

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)

slide-27
SLIDE 27

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 ¡

slide-28
SLIDE 28

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) ¡

slide-29
SLIDE 29

Your ¡Turn ¡

  • Worksheet: ¡AVL ¡Prac;ce ¡