lecture 7 - stanford universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •idea 0:...

62
Lecture 7 Binary Search Trees and Red-Black Trees

Upload: others

Post on 19-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Lecture7BinarySearchTreesandRed-BlackTrees

Page 2: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Announcements

• HW3released!(DueFriday)

• Specialguestlecturer:SamKim!

Page 3: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Roadmap

Graphs!

Asymptotic

Analysis

Dynamic

ProgrammingGreedyAlgs

MIDTERM

The

Future!

Moredetailedscheduleonthewebsite!

Wearehere

Page 4: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Today

• Beginabriefforayintodatastructures!

• SeeCS166formore!

• Binarysearchtrees

• YoumayrememberthesefromCS106B

• Theyarebetterwhenthey’rebalanced.

thiswillleadusto…

• Self-BalancingBinarySearchTrees

• Red-Black trees.

Page 5: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Whyarewestudyingself-balancingBSTs?

1. Thepunchlineisimportant:

• AdatastructurewithO(log(n))INSERT/DELETE/SEARCH

2. TheideabehindRed-Black Treesisclever

• It’sgoodtobeexposedtocleverideas.

• Alsoit’sjustaestheticallypleasing.

Page 6: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Somedatastructuresforstoringobjectslike(aka,nodes withkeys)

• (Sorted)arrays:

• (Sorted)linkedlists:

• Somebasicoperations:

• INSERT,DELETE,SEARCH

42 871 3 5HEAD

42 871 3 5

5

Page 7: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

SortedArrays

• O(n)INSERT/DELETE:

• O(log(n))SEARCH:

42 871 3 5

421 3

42 871 3 5

eg,Binarysearchtoseeif3isinA.

8754.5

Page 8: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Sortedlinkedlists

• O(1)INSERT/DELETE:• (assumingwehaveapointertothelocationoftheinsert/delete)

• O(n)SEARCH:

42 871 3 5

42 871 3 5HEAD

6

42 871 3 5HEAD

Page 9: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

MotivationforBinarySearchTrees

Sorted Arrays LinkedListsBinarySearch

Trees*

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

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

Page 10: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Binarytreeterminology

42 8

7

1

3

5

Thisnodeis

theroot

Thisisanode.

Ithasakey (7).

Thesenodes

areleaves.

Theleft child

of is3 2

Therightchild

of is3 4

Bothchildren

of areNIL1

Fortodayallkeysaredistinct.

Eachnodehas

twochildren

isadescendant

of

2

5Eachnodehasa

pointertoitsleft

child,rightchild,

andparent.

Page 11: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

BinarySearchTrees

4

2

8 7

1

35

• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.

• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 12: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

BinarySearchTrees

4

2

8 7

1

35

• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.

• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 13: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

BinarySearchTrees

42

8

7

1

35

• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.

• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 14: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

BinarySearchTrees

42 8

71

3

5

• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.

• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Page 15: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

BinarySearchTrees

42 8

7

1

3

5

• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.

• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Q:Isthistheonly

binarysearchtreeI

couldpossiblybuild

withthesevalues?

A:No. Imade

choicesabout

whichnodesto

choosewhen.Any

choiceswould

havebeenfine.

Page 16: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Aside:thisshouldlookfamiliar

4

2

8 7

1

35

kinda likeQuickSort

Page 17: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

BinarySearchTrees

42 8

7

1

3

5

• It’sabinarytreesothat:• EveryLEFTdescendant ofanodehaskeylessthanthatnode.

• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.

42 8

7

1

3

5

BinarySearchTreeNOT aBinary

SearchTree

Whichof

theseisa

BST?

Page 18: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Rememberthegoal

FastSEARCH/INSERT/DELETECanwedothese?

Page 19: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

SEARCHinaBinarySearchTreedefinitionbyexample

42 8

7

1

3

5EXAMPLE:Searchfor4.

EXAMPLE:Searchfor4.5• Itturnsoutitwillbeconvenient

toreturn4inthiscase

• (thatis,return thelastnode

beforewewentoffthetree)

Ollietheover-achievingostrich

Writepseudocode

(oractualcode)to

implementthis!Howlongdoesthistake?

O(lengthoflongestpath)= O(height)

Page 20: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

INSERTinaBinarySearchTree

42 8

7

1

3

5EXAMPLE:Insert4.5

4.5

• INSERT(key):• x=SEARCH(key)

• Insertanewnodewith

desiredkeyatx…

x= 4

Youthoughtaboutthison

yourpre-lectureexercise!

(Seehiddenslidefor

pseudocode.)

Page 21: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

INSERTinaBinarySearchTree

42 8

7

1

3

5EXAMPLE:Insert4.5

4.5

• INSERT(key):• x=SEARCH(key)

• ifkey>x.key:

• Makeanewnodewiththe

correctkey,andputitasthe

rightchildofx.

• ifkey<x.key:

• Makeanewnodewiththe

correctkey,andputitasthe

leftchildofx.

• if x.key ==key:

• return

x= 4

Thisslide

skippedin

class– here

forreference

Page 22: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

DELETEinaBinarySearchTree

42 8

7

1

3

5

EXAMPLE:Delete2

• DELETE(key):• x=SEARCH(key)

• if x.key ==key:

• ….deletex….

Youthoughtaboutthisinyourpre-

lectureexercisetoo!

Thisisabitmorecomplicated…see

thehiddenslidesforsomepictures

ofthedifferentcases.

x= 2

Page 23: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

DELETEinaBinarySearchTreeseveralcases(byexample)saywewanttodelete3

Thistriangle

isacartoon

forasubtree

3

Case1:if3isaleaf,

justdeleteit.2

3

Case2:if3hasjustonechild,

movethatup.

5 5

2

55

Siggi theStudiousStork

Writepseudocodeforallof

these!(OrseeIPython

NotebookforLecture7)

Thisslideskipped

inclass– herefor

reference!

Page 24: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

DELETEinaBinarySearchTreectd.

42

3

5

3.1

2

5

Case3:if3hastwochildren,

replace3withit’simmediatesuccessor.(aka,nextbiggestthingafter3)

• DoesthismaintaintheBST

property?• Yes.

• Howdowefindthe

immediatesuccessor?• SEARCHfor3inthesubtree

under3.right

• Howdoweremoveitwhen

wefindit?• If[3.1]has0or1children,

dooneofthepreviouscases.

• Whatif[3.1]hastwo

children?• Itdoesn’t.

4

3.1

Thisslideskipped

inclass– herefor

reference!

Page 25: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Howlongdotheseoperationstake?

• SEARCH isthebigone.

• EverythingelsejustcallsSEARCH andthendoessomesmallO(1)-timeoperation.

42 8

73

5

6

Time=

O(heightoftree)

Treeshavedepth

O(log(n)).Done!

Luckythelackadaisicallemur.

Howlongdoessearchtake?

Page 26: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Wait...

4

2

8

7

3

5

6

• Thisisavalidbinarysearchtree.

• Theversionwithnnodeshas

depthn,not O(log(n)).

Couldsuchatreeshowup?

InwhatorderwouldIhaveto

insertthenodes?

Insertingintheorder

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

Sothiscould happen.

Page 27: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Whattodo?

• Goal:FastSEARCH/INSERT/DELETE

• AllthesethingstaketimeO(height)

• Andtheheightmightbebig!!!L

• Idea0:

• Keeptrackofhowdeepthetreeisgetting.

• Ifitgetstootall,re-doeverythingfromscratch.

• AtleastΩ(n)everysooften….

• Turnsoutthat’snotagreatidea.Insteadweturnto…

Ollietheover-achievingostrich

Howoftenis“everyso

often”intheworstcase?

It’sactuallyprettyoften!

Page 28: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Self-BalancingBinarySearchTrees

Page 29: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

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

Bfell

down.

Note:A

,B,C,X,Yareva

riablenames,n

otth

e

contentso

fthenodes.

Page 30: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Thisseemshelpful

4

2

8

7

3

6

5

YOINK!

42 8

73

6

5

Page 31: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Doesthiswork?

• Wheneversomethingseemsunbalanced,dorotationsuntilit’sokayagain.

LuckytheLackadaisicalLemur

Evenformethisis

prettyvague.Whatdo

wemeanby“seems

unbalanced”?What’s

“okay”?

Page 32: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Idea2:havesomeproxyforbalance

• Maintainingperfectbalanceistoohard.

• Instead,comeupwithsomeproxyforbalance:

• Ifthetreesatisfies[SOMEPROPERTY],thenit’sprettybalanced.

• Wecanmaintain[SOMEPROPERTY] usingrotations.

Thereareactuallyseveral

waystodothis,buttoday

we’llsee…

Page 33: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

• 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 34: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Red-BlackTreestheserulesaretheproxyforbalance

• Everynodeiscoloredred orblack.

• Therootnodeisablacknode.

• NIL childrencountasblacknodes.

• Childrenofarednodeareblacknodes.

• Forallnodesx:

• allpathsfromxtoNIL’shavethesamenumberofblacknodesonthem.

42 8

73

5

6

NIL NIL NIL NIL NIL NIL NIL NIL

I’mnotgoingtodrawtheNIL

childreninthefuture,butthey

aretreatedasblacknodes.

Page 35: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Examples(?)• Everynodeiscoloredred orblack.

• Therootnodeisablacknode.

• NIL childrencountasblacknodes.

• Childrenofarednodeareblacknodes.

• Forallnodesx:

• allpathsfromxtoNIL’shavethesamenumberofblacknodesonthem.

Yes!

No! No! No!

Page 36: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Why???????

• Thisisprettybalanced.

• Theblacknodes arebalanced

• Therednodesare“spreadout”sotheydon’tmessthingsuptoomuch.

• Wecanmaintainthispropertyasweinsert/deletenodes,byusingrotations.

42 8

73

5

6

9

Thisisthereallycleveridea!

This Red-Blackstructureisaproxyforbalance.It’sjustasmidgeweakerthanperfectbalance,butwecanactuallymaintainit!

Page 37: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Thisis“prettybalanced”

• Toseewhy,intuitively,let’strytobuildaRed-BlackTreethat’sunbalanced.

Luckythe

lackadaisical

lemur

Let’sbuildsomeintuition!

Onepathcouldbetwiceas

longanotherifwepaditwith

rednodes.

Conjecture:theheightofa

red-blacktreeisatmost2log(n)

Page 38: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Thatturnsouttobebasicallyright.[proofsketch]

• Saythereareb(x)blacknodesinanypathfromxtoNIL.

• (excludingx,includingNIL).

• Claim:

• Thenthereareatleast2b(x) – 1non-NILnodesinthesubtreeunderneathx.(Includingx).

• [Proofbyinduction– onboardiftime]

Then:

𝑛 ≥ 2$ %&&' − 1

≥ 2+,-.+/

01 − 1Rearranging:

𝑛 + 1 ≥ 234563'

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

x

y

usingtheClaim

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

z

NIL

Page 39: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Okay,soit’sbalanced……butcanwemaintainit?

•Yes!

• Fortherestoflecture:• sketchofhowwe’ddothis.

• SeeCLRSformoredetails.

• (Youarenotresponsibleforthedetailsforthisclass– butyoushouldunderstandthemainideas).

Page 40: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Manycases

73

6

73

6

73

6

• Supposewewanttoinserthere.• eg,wanttoinsert0.

• Andthenthereare9morecasesforallofthevarious

symmetriesofthese3cases…

Page 41: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

73

6Example:insert0

0

Whatifitlookslikethis?

73

6

Page 42: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Manycases

73

6

73

6

73

6

• Supposewewanttoinserthere.• eg,wanttoinsert0.

• Andthenthereare9morecasesforallofthevarious

symmetriesofthese3cases…

Page 43: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6Example:insert0

0

No!

Whatifitlookslikethis?

73

6

Page 44: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6Example:insert0

Can’twejustinsert0as

ablacknode?

0No!

Whatifitlookslikethis?

73

6

Page 45: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Weneedabitmorecontext

73

6

Example:insert0

Whatifitlookslikethis?

73

6

-1

Page 46: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Weneedabitmorecontext

73

6

Example:insert0

0

Whatifitlookslikethis?

73

6

• Add0asarednode.

-1

Page 47: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Weneedabitmorecontext

73

6

Example:insert0

0

Whatifitlookslikethis?

73

6

Flip

colors!

• Add0asarednode.

• Claim: RB-Treepropertiesstillhold.

-1

Page 48: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Butwhatifthat wasred?

73

6

Example:insert0

0

Whatifitlookslikethis?

73

6

-1

Page 49: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Morecontext…

73

6

Example:insert0

0

Whatifitlookslikethis?

73

6

-1

-3

Page 50: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Morecontext…

6

Example:insert0

Whatifitlookslikethis?

73

6

-1

-3

Nowwe’re

basicallyinserting

6intosome

smallertree.

Recurse!

Page 51: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Manycases

73

6

73

6

73

6

• Supposewewanttoinserthere.• eg,wanttoinsert0.

• Andthenthereare9morecasesforallofthevarious

symmetriesofthese3cases…

Page 52: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6

Example:Insert0.

• Actually,thiscan’t

happen?• 6-3 pathhasoneblacknode

• 6-7-… hasatleasttwo

• Itmighthappenthatwe

justturned0redfrom

thepreviousstep.

• Oritcouldhappenif

isactuallyNIL.

0

Whatifitlookslikethis?

73

6

7

Page 53: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

RecallRotations

• MaintainBinarySearchTree(BST)property,whilemovingstuffaround.

BA

CY

XYOINK!

CLAIM:

thisstillhasBSTproperty.

BA

C

Y

X

B

A

C

Y

X

Page 54: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6

0

Whatifitlookslikethis?

73

6

YOINK!

3

60

7

Needtoarguethat

ifRB-Treeproperty

heldbefore,itstill

does.

Page 55: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Manycases

73

6

73

6

73

6

• Supposewewanttoinserthere.• eg,wanttoinsert0.

• Andthenthereare9morecasesforallofthevarious

symmetriesofthese3cases…

Page 56: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

DeletingfromaRed-Blacktree

Funexercise!

Ollietheover-achievingostrich

Page 57: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

That’salotofcases

• Youarenotresponsible forthenitty-grittydetailsofRed-BlackTrees.(Forthisclass)

• Thoughimplementingthemisagreatexercise!

• Youshouldknow:

• WhatarethepropertiesofanRBtree?

• And(moreimportant)whydoesthatguaranteethattheyarebalanced?

Page 58: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Whatwasthepointagain?

• Red-BlackTreesalwayshaveheightatmost2log(n+1).

• AswithgeneralBinarySearchTrees,alloperationsareO(height)

• SoalloperationsareO(log(n)).

Page 59: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Conclusion:Thebestofbothworlds

Sorted Arrays LinkedLists

Balanced

BinarySearch

Trees

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

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

Page 60: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Today

• Beginabriefforayintodatastructures!

• SeeCS166formore!

• Binarysearchtrees

• YoumayrememberthesefromCS106B

• Theyarebetterwhenthey’rebalanced.

thiswillleadusto…

• Self-BalancingBinarySearchTrees

• Red-Black trees.

Recap

Page 61: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Recap

• Balancedbinarytreesarethebestofbothworlds!

• Butweneedtokeepthembalanced.

• Red-BlackTrees dothatforus.

• WegetO(log(n))-timeINSERT/DELETE/SEARCH

• Cleveridea:haveaproxyforbalance

42 8

73

5

6

Page 62: Lecture 7 - Stanford Universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •Idea 0: •Keep track of how deep the tree is getting. •If it gets too tall, re-do everything

Before nexttime

• Pre-lectureexerciseforLecture8

• (More)funwithprobability!

Nexttime

• Hashing!