New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf ·...

45
Lecture 7 Binary Search Trees and Red-Black Trees

Transcript of New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf ·...

Page 1: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Lecture7BinarySearchTreesandRed-BlackTrees

Page 2: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Announcements

• HW3released!(DueFriday)

Page 3: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Today:binarysearchtrees• Briefforayintodatastructures!• SeeCS166formore!

• Whatarebinarysearchtrees?• YoumayrememberthesefromCS106B• Whyaretheygood?• Whyaretheybad?

thiswillleadusto…

• Self-BalancingBinarySearchTrees• Red-Black trees.

Page 4: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Whyarewestudyingself-balancingBSTs?

1. Thepunchlineisimportant:• AdatastructurewithO(log(n))INSERT/DELETE/SEARCH

2. TheideabehindRed-Black Treesisclever• It’sgoodtobeexposedtocleverideas.• Alsoit’sjustaestheticallypleasing.

Page 5: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Motivationforbinarysearchtrees• We’vebeenassumingthatwehaveaccesstosomebasicdatastructures:• (Sorted)linkedlists

• (Sorted)arrays

42 871 3 5HEAD

42 871 3 5

Page 6: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Sortedlinkedlists

• O(1)insert/delete(assumingwehaveapointertothelocationoftheinsert/delete):

• O(n)search/select:

42 871 3 5

42 871 3 5HEAD

6

42 871 3 5HEAD

Page 7: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

SortedArrays

• O(n)insert/delete:

• O(log(n))search,O(1)select:

42 871 3 5

421 3

42 871 3 5Search:Binarysearchtoseeif3isinA.

8754.5

Select:ThirdsmallestisA[3].

Page 8: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Thebestofbothworlds

Sorted Arrays LinkedLists BinarySearchTrees*

Search O(log(n)) O(n) O(log(n))

Insert/Delete O(n) O(1) O(log(n))

Page 9: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Treeterminology

42 8

7

1

3

5Thisnodeistheroot

Thisisanode.Ithasakey (7).

Thesenodesareleaves.

Theleft childof is3 2

Therightchildof is3 4

Bothchildrenof areNIL1

Todayallkeysaredistinct.

Page 10: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

BinarySearchTrees

4

2

8 71

3 5

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 11: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

BinarySearchTrees

4

2

8 71

3 5

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 12: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

BinarySearchTrees

42

8

71

3 5

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 13: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

BinarySearchTrees

42 8

71

3

5

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 14: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

BinarySearchTrees

42 8

7

1

3

5

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 15: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Aside:thisshouldlookfamiliar

4

2

8 71

3 5

kinda likeQuickSort

Page 16: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

BinarySearchTrees

42 8

7

1

3

5

• It’sabinarytreesothat:• EveryLEFTdescendent ofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

42 8

7

1

3

5

BinarySearchTree NOT aBinarySearchTree

Page 17: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

SEARCHinaBinarySearchTreedefinitionbyexample

42 8

7

1

3

5EXAMPLE:Searchfor4.

EXAMPLE:Searchfor4.5• Itturnsoutitwillbeconvenient

toreturn4inthiscase• (thatis,return thelastnode

beforewewentoffthetree)

Ollietheover-achievingostrich

Writepseudocode(oractualcode)toimplementthis!

Page 18: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

INSERTinaBinarySearchTree

42 8

7

1

3

5EXAMPLE:Insert4.5

4.5

• INSERT(key):• x=SEARCH(key)• ifkey>x.key:

• Makeanewnodewiththecorrectkey,andputitastherightchildofx.

• ifkey<x.key:• Makeanewnodewiththe

correctkey,andputitastheleftchildofx.

• if x.key ==key:• return

x= 4

Page 19: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

DELETEinaBinarySearchTree

42 8

7

1

3

5EXAMPLE:Delete2

• DELETE(key):• x=SEARCH(key)• if x.key ==key:• ….deletex….

Thisisabitmorecomplicated…x= 2

Page 20: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

DELETEinaBinarySearchTreeseveralcases(byexample)saywewanttodelete3

Thistriangleisacartoonforasubtree

3Case1:if3isaleaf,justdeleteit. 2

3

Case2:if3hasjustonechild,movethatup.

5 5

2

55

Ollietheover-achievingostrich

Wewon’twritedownpseudocodeforthis– trytodoityourself!

Page 21: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

DELETEinaBinarySearchTreectd.

42

3

53.1

2

5

Case3:if3hastwochildren,replace3withit’simmediatesuccessor.(aka,nextbiggestthingafter3) • Howdowefindthe

immediatesuccessor?• SEARCH(3.right,3)

• Howdoweremoveitwhenwefindit?• RunDELETEforoneoftheprevioustwocases.

• Wait,whatifit’sTHIScase?(Case3).• It’snot.

4

3.1

Page 22: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Howlongdotheseoperationstake?• SEARCH isthebigone.• EverythingelsejustcallsSEARCH andthendoessomesmallO(1)-timeoperation.

42 8

73

5

6

Time=O(depthoftree)

TreeshavedepthO(log(n)).Done!

Luckythelackadaisicallemur.

Page 23: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Wait...

4

2

8

7

3

5

6

• Thisisavalidbinarysearchtree.

• Theversionwithnnodeshasdepthn,not O(log(n)).

Couldsuchatreeshowup?InwhatorderwouldIhaveto

insertthenodes?

Insertingintheorder2,3,4,5,6,7,8woulddoit.

Sothiscould happen.

Page 24: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Whattodo?

• Keeptrackofhowdeepthetreeisgetting.

• Ifitgetstootall,re-doeverythingfromscratch.• AtleastΩ(n)everysooften….

•Otherideas?

Ollietheover-achievingostrich

Howoftenis“everysooften”intheworstcase?It’sactuallypretty

often!

Page 25: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Idea1:Rotations• MaintainBinarySearchTree(BST)property,whilemovingstuffaround.

BA

CY

XYOINK!

CLAIM:thisstillhasBSTproperty.

NomatterwhatlivesunderneathA,B,C,thistakestimeO(1).(Why?)

BA

C

Y

X

B

A

C

Y

X

Page 26: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Thisseemshelpful

4

2

8

7

3

6

5

YOINK!

42 8

73

6

5

Page 27: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Doesthiswork?

• Wheneversomethingseemsunbalanced,dorotationsuntilit’sokayagain.

LuckytheLackadaisicalLemur

Evenformethisisprettyvague.Whatdowemeanby“seemsunbalanced”?What’s

“okay”?

Page 28: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Idea2:havesomeproxyforbalance

• Maintainingperfectbalanceistoohard.• Instead,comeupwithsomeproxyforbalance:• Ifthetreesatisfies[SOMEPROPERTY],thenit’sprettybalanced.• Wecanmaintain[SOMEPROPERTY] usingrotations.

Thereareactuallyseveralwaystodothis,buttodaywe’llsee…

Page 29: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

• A Binary Search Tree that balances itself!• No more time-consuming by-hand balancing!• Be the envy of your friends and neighbors

with the time-saving…

42 8

73

5

6Maintain balance by stipulating that

black nodes are balanced, and that there aren’t too many red nodes.

It’s just good sense!

Red-BlackTrees

Page 30: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Red-BlackTreestheserulesaretheproxyforbalance

• Everynodeiscoloredred orblack.• Therootnodeisablacknode.• NIL childrencountasblacknodes.• Childrenofarednodeareblacknodes.• Forallnodesx:• allpathsfromxtoNONE’shavethesamenumberofblacknodesonthem.

42 8

73

5

6

Page 31: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Examples(?) • Everynodeiscoloredred orblack.• Therootnodeisablacknode.• NONEchildrencountasblacknodes.• Childrenofarednodeareblacknodes.• Forallnodesx:

• allpathsfromxtoNONE’shavethesamenumberofblacknodesonthem.

Yes!No! No! No!

Page 32: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Why???????

• Thisisprettybalanced.• Theblacknodes arebalanced• Therednodesare“spreadout”sotheydon’tmessthingsuptoomuch.

• Wecanmaintainthispropertyasweinsert/deletenodes,byusingrotations.

42 8

73

5

6

9

Page 33: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Thisis“prettybalanced”• Toseewhy,intuitively,let’strytobuildaRed-BlackTreethat’sunbalanced.

Luckythelackadaisicallemur

Let’sbuildsomeintuition!

Onepathcouldbetwiceaslongasalltheothersifwepaditwithrednodes.

Conjecture:theheightofared-blacktreeisatmost2log(n)

Page 34: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Thatturnsoutthebebasicallyright.[proofsketch]

• Saythereareb(x)blacknodesinanypathfromxtoNONE.• (includingx).

• Claim:• Thenthereareatleast2b(x) – 1nodesinthesubtreeunderneathx.

• [Proofbyinduction– onboardiftime]Then:𝑛 ≥ 2$ %&&' − 1≥ 2

+,-.+/01 − 1

Rearranging:

𝑛 + 1 ≥ 234563'

71 ⇒ ℎ𝑒𝑖𝑔ℎ𝑡 ≤ 2log(𝑛 + 1)

x

y

usingtheClaim

b(root)>=height/2becauseofRBTree rules.

Page 35: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Okay,soit’sbalanced……butcanwemaintainit?

•Yes!• Fortherestoflecture:• sketchofhowwe’ddothis.

• SeeCLRSformoredetails.

Page 36: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

InsertingintoaRed-BlackTree

• Makeanewrednode.• Insertitasyouwouldnormally.

73

6Example:insert0

0

Whatifitlookslikethis?

73

6

Page 37: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

InsertingintoaRed-BlackTree

• Makeanewrednode.• Insertitasyouwouldnormally.• Fixthingsupifneeded.

73

6Example:insert0

0

No!

Whatifitlookslikethis?

73

6

Page 38: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

InsertingintoaRed-BlackTree

• Makeanewrednode.• Insertitasyouwouldnormally.• Fixthingsupifneeded.

73

6Example:insert0

Can’twejustinsert0asablacknode?

0No!

Whatifitlookslikethis?

73

6

Page 39: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

InsertingintoaRed-BlackTree

• Makeanewrednode.• Insertitasyouwouldnormally.• Fixthingsupifneeded.

73

6Example:insert0

0

• Insteadrecolorlikethis.• Needtoargue:

• RB-Treepropertiesstillhold.• Whatabouttheredroot?

• if6isactuallytheroot,coloritblack.• Else,recursivelyre-colorupthetree.

Whatifitlookslikethis?

73

6

ish

-1

6

-1Nowtheproblemlookslikethis,whereI’minserting 6

Page 40: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

InsertingintoaRed-BlackTree

• Makeanewrednode.• Insertitasyouwouldnormally.• Fixthingsupifneeded.

73

6Example:Insert0.

• Actually,thiscan’thappen?

• Itmighthappenthatwejustturned0redfromthepreviousstep.

• OritcouldhappenifisactuallyNIL.

0

Whatifitlookslikethis?

73

6

7

Page 41: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

RecallRotations• MaintainBinarySearchTree(BST)property,whilemovingstuffaround.

BA

CY

XYOINK!

CLAIM:thisstillhasBSTproperty.

BA

C

Y

X

B

A

C

Y

X

Page 42: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

InsertingintoaRed-BlackTree• Makeanewrednode.• Insertitasyouwouldnormally.• Fixthingsupifneeded.

73

6

0

Whatifitlookslikethis?

73

6

YOINK!3

60

7

NeedtoarguethatifRB-Treepropertyheldbefore,itstill

does.

Page 43: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Thepunchline:

• AfewthingsstilllefttocheckforINSERT!• Anythingelsethatmighthappenlooksbasicallylikewhatwejustdid.

• Formallydealingwiththerecursion.• Youcheckthese!(orseeCLRS)

• DELETE issimilar.

That’sbasicallyit

• Red-BlackTreesalwayshaveheightatmost2log(n+1).• AswithgeneralBinarySearchTrees,alloperationsareO(height)• SoalloperationsareO(log(n)).

Pluckythepedanticpenguin

Page 44: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Conclusion:Thebestofbothworlds

Sorted Arrays LinkedListsBalanced

BinarySearchTrees

Search O(log(n)) O(n) O(log(n))

Insert/Delete O(n) O(1) O(log(n))

Page 45: New Lecture 7 - Stanford Universityweb.stanford.edu/.../cs/cs161/cs161.1176/Slides/Lecture7.pdf · 2017. 4. 24. · 3 7 6 Example: insert 0 0 •Instead recolor like this. •Need

Recap

•Hashing!Nexttime

• Balancedbinarytreesarethebestofbothworlds!• Butweneedtokeepthembalanced.• Red-BlackTrees dothatforus.• WegetO(log(n))-timeINSERT/DELETE/SEARCH• Cleveridea:haveaproxyforbalance