cs 2230 cs ii: data structures

21
CS 2230 CS II: Data structures Meeting 25: AVL trees, in detail Brandon Myers University of Iowa

Upload: others

Post on 01-Dec-2021

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 2230 CS II: Data structures

CS2230CSII:Datastructures

Meeting25:AVLtrees,indetailBrandonMyers

UniversityofIowa

Page 2: CS 2230 CS II: Data structures

Today’sbigideas

• TheAVLtreeisbalancedbinarytreeandrebalancesitselfusing“tri-noderestructuring”

• Tri-noderestructuringlooksatthreerootsinvolvedinthenewimbalanceandre-arrangesthemtorestorethebalanceproperty

Page 3: CS 2230 CS II: Data structures

b=y

a=z

c=xT0

T1

T2 T3h-1h-1

h

h

h

h+1

h+2

Insertmessesuptheheights

b=y

a=z

c=xT0

T1

T2 T3?h-1

h

h

?

?

?

d

insert(d)

Page 4: CS 2230 CS II: Data structures

b=y

a=z

c=xT0

T1

T2 T3h-1h-1

h

h

h

h+1

h+2

Peerinstruction

b=y

a=z

c=xT0

T1

T2 T3?h-1

h

h

?

?

?

d

insert(d)

Aftertheinsert,whichnodenowdoesn’tsatisfytheAVLbalanceproperty?a)ab)bc) cd) dd) multiplenodes

https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+

Page 5: CS 2230 CS II: Data structures

b=y

a=z

c=xT0

T1

T2 T3h-1h-1

h

h

h

h+1

h+2

Togettheanswer

b=y

a=z

c=xT0

T1

T2 T3hh-1

h

h

h+1

h+2

h+3

d

insert(d)

Page 6: CS 2230 CS II: Data structures

b=y

a=z

c=xT0

T1

T2 T3h-1h-1

h

h

h

h+1

h+2

Tri-noderestructuring

b=y

a=z

c=xT0

T1

T2 T3hh-1

h

h

h+1

h+2

h+3

Goaloftri-noderestructuring:rearrange3rootstorebalancethetree

d

insert(d)

Page 7: CS 2230 CS II: Data structures

Tri-noderestructuring

b=y

a=z

c=xT0

T1

T2 T3hh-1

h

h

h+1

h+2

h+3

Intuition:wewanttomakethemiddlenode (here,itisb)thenewrootofthesubtree

Howdowepickthe3nodesintherestructuring?Rootisthenodethatnowhasunbalancedchildren(here,a),thenincludetwonodesbelowitonthepathtod(here,bandc)

d

b=y

a=z c=x

T0 T1 T2 T3

restructure

d

hh-1

h+1h+1

h h

h+2

Page 8: CS 2230 CS II: Data structures

Anotherexample

c=y

b=x

a=z

T0

T1 T2

T3

h-1 h-1

hh

h+1h

h+2

c=y

b=x

a=z

T0

T1 T2

T3

h-1 ?

?h

?h

h+3

d

insert(d)

Page 9: CS 2230 CS II: Data structures

Peerinstruction

c=y

b=x

a=z

T0

T1 T2

T3

h-1 h-1

hh

h+1h

h+2

c=y

b=x

a=z

T0

T1 T2

T3

h-1 ?

?h

?h

?

d

insert(d)

Wemightneedtorebalancethetree.Whatwillbetheresult?a) nochangeb) aremainstherootbutleft=bc) bisthenewrootwithleft=a,right=cd) cisthenewrootwithleft=a,right=be) disthenewrootwithleft=b,right=c

https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+

Page 10: CS 2230 CS II: Data structures

Answer,part1

c=y

b=x

a=z

T0

T1 T2

T3

h-1 h-1

hh

h+1h

h+2

c=y

b=x

a=z

T0

T1 T2

T3

h-1 h

h+1h

h+2h

h+3

d

insert(d)

Wemightneedtorebalancethetree.Whatwillbetheresult?a) nochangeb) aremainstherootbutleft=bc) bisthenewrootwithleft=a,right=cd) cisthenewrootwithleft=a,right=be) disthenewrootwithleft=b,right=c

Page 11: CS 2230 CS II: Data structures

Answer,part2

c=y

b=x

a=z

T0

T1 T2

T3

h-1 h

h+1h

h+2

h+3

d

Wemightneedtorebalancethetree.Whatwillbetheresult?a) nochangeb) aremainstherootbutleft=bc) bisthenewrootwithleft=a,right=cd) cisthenewrootwithleft=a,right=be) disthenewrootwithleft=b,right=c

b=x

c=ya=z

T0 T1 T2 T3

d

h

Page 12: CS 2230 CS II: Data structures

Implementingtri-noderestructuring

• Step1:traverseupfromtheinsertednode,checkingtheAVLbalancepropertyateachnode.Onceyoufindonce,youhaveyour3nodesinvolvedinthetri-node

c=y

b=x

a=z

T0

T1 T2

T3

h-1 h

h+1h

h+2

h+3

d

checkd,checkitsparents...checkb,checkc,checka(aha!imbalanced)

h

Page 13: CS 2230 CS II: Data structures

Peerinstruction

Node54wasjustinserted,whatthreenodesshouldbeincludedinthetri-noderestructuring?

44

17 78

32 50 88

48 62

54

https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+

Page 14: CS 2230 CS II: Data structures

Answer

44

17 78

32 50 88

48 62

54

Page 15: CS 2230 CS II: Data structures

Implementingtri-noderestructuring

• Step1:traverseupfromtheinsertednode,checkingtheAVLbalancepropertyateachnode.Onceyoufindonce,youhaveyour3nodesinvolvedinthetri-node

• Step2:doeither1rotationor2rotationsdependingonwhichoffourcases...

Page 16: CS 2230 CS II: Data structures

Case1:right,right

Case2:left,left

T0T1

T2T3

c = xb = y

a = z

T0 T1 T2T3

c = xb = y

a = zsingle rotation

T3T2

T1T0

a = xb = y

c = z

T0T1T2T3

a = xb = y

c = zsingle rotation

h

Implementingtri-noderestructuringinsertoccurredinshadedsubtreeSinglerotations

T0T1 T2 T3

Page 17: CS 2230 CS II: Data structures

Doublerotations• Case3:right,left

• Case4:left,right

double rotationa = z

b = xc = y

T0T2

T1T3 T0

T2T3T1

a = zb = x

c = y

double rotationc = z

b = xa = y

T0T2

T1T3 T0

T2T3 T1

c = zb = x

a = y

Implementingtri-noderestructuringinsertoccurredinshadedsubtree

Page 18: CS 2230 CS II: Data structures

Peerinstruction

88

44

17 78

32 50

48 62

2

5

1

1

3

4

2

1

54

1

T0 T2

T3

x

y

z

2

3

4

5

67

1

Whichcaseisneededtoperformeither1or2rotations?a)case1:right,rightb)case2:left,leftc) case3:right,leftd)case4:left,right

Page 19: CS 2230 CS II: Data structures

Answer

Page 20: CS 2230 CS II: Data structures

Questionfornexttime...

afteraninsertimbalancesaBinarySearchTree ofheightH,howmanytri-noderestructuringsdowehavetoperformtobalancethewholetree?

(giveanswerinbig-oh)

Page 21: CS 2230 CS II: Data structures

Today’sbigideas

• TheAVLtreeisbalancedbinarytreeandrebalancesitselfusing“tri-noderestructuring”

• Tri-noderestructuringlooksatthreerootsinvolvedinthenewimbalanceandre-arrangesthemtorestorethebalanceproperty