Lecture 7 - Stanford University

45
Lecture 7 Binary Search Trees and Red-Black Trees

Transcript of Lecture 7 - Stanford University

Lecture7BinarySearchTreesandRed-BlackTrees

Announcements

• HW3released!(DueFriday)

Today:binarysearchtrees

• Briefforayintodatastructures!

• SeeCS166formore!

• Whatarebinarysearchtrees?

• YoumayrememberthesefromCS106B

• Whyaretheygood?

• Whyaretheybad?

thiswillleadusto…

• Self-BalancingBinarySearchTrees

• Red-Black trees.

Whyarewestudyingself-balancingBSTs?

1. Thepunchlineisimportant:

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

2. TheideabehindRed-Black Treesisclever

• It’sgoodtobeexposedtocleverideas.

• Alsoit’sjustaestheticallypleasing.

Motivationforbinarysearchtrees

• We’vebeenassumingthatwehaveaccesstosomebasicdatastructures:

• (Sorted)linkedlists

• (Sorted)arrays

42 871 3 5HEAD

42 871 3 5

Sortedlinkedlists

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

• O(n)search/select:

42 871 3 5

42 871 3 5HEAD

6

42 871 3 5HEAD

SortedArrays

• O(n)insert/delete:

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

42 871 3 5

421 3

42 871 3 5

Search:Binarysearchtoseeif3isinA.

8754.5

Select:ThirdsmallestisA[3].

Thebestofbothworlds

Sorted Arrays LinkedListsBinarySearch

Trees*

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

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

Treeterminology

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

Todayall

keysare

distinct.

BinarySearchTrees

4

2

8 7

1

35

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.

• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

BinarySearchTrees

4

2

8 7

1

35

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.

• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

BinarySearchTrees

42

8

7

1

35

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.

• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

BinarySearchTrees

42 8

71

3

5

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.

• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

BinarySearchTrees

42 8

7

1

3

5

• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.

• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

• Exampleofbuildingabinarysearchtree:

Aside:thisshouldlookfamiliar

4

2

8 7

1

35

kinda likeQuickSort

BinarySearchTrees

42 8

7

1

3

5

• It’sabinarytreesothat:• EveryLEFTdescendent ofanodehaskeylessthanthatnode.

• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.

42 8

7

1

3

5

BinarySearchTreeNOT aBinary

SearchTree

SEARCHinaBinarySearchTreedefinitionbyexample

42 8

7

1

3

5EXAMPLE:Searchfor4.

EXAMPLE:Searchfor4.5• Itturnsoutitwillbeconvenient

toreturn4inthiscase

• (thatis,return thelastnode

beforewewentoffthetree)

Ollietheover-achievingostrich

Writepseudocode

(oractualcode)to

implementthis!

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

DELETEinaBinarySearchTree

42 8

7

1

3

5

EXAMPLE:Delete2

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

• if x.key ==key:

• ….deletex….

Thisisabitmore

complicated…x= 2

DELETEinaBinarySearchTreeseveralcases(byexample)saywewanttodelete3

Thistriangle

isacartoon

forasubtree

3

Case1:if3isaleaf,

justdeleteit.2

3

Case2:if3hasjustonechild,

movethatup.

5 5

2

55

Ollietheover-achievingostrich

Wewon’twrite

downpseudocode

forthis– trytodoit

yourself!

DELETEinaBinarySearchTreectd.

42

3

5

3.1

2

5

Case3:if3hastwochildren,

replace3withit’simmediatesuccessor.(aka,nextbiggestthingafter3)

• Howdowefindthe

immediatesuccessor?• SEARCH(3.right,3)

• Howdoweremoveit

whenwefindit?• RunDELETEforoneof

theprevioustwocases.

• Wait,whatifit’sTHIS

case?(Case3).• It’snot.

4

3.1

Howlongdotheseoperationstake?

• SEARCH isthebigone.

• EverythingelsejustcallsSEARCH andthendoessomesmallO(1)-timeoperation.

42 8

73

5

6

Time=

O(depthoftree)

Treeshavedepth

O(log(n)).Done!

Luckythelackadaisicallemur.

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.

Whattodo?

• Keeptrackofhowdeepthetreeisgetting.

• Ifitgetstootall,re-doeverythingfromscratch.

• AtleastΩ(n)everysooften….

•Otherideas?

Ollietheover-achievingostrich

Howoftenis“every

sooften”inthe

worstcase?It’s

actuallypretty

often!

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

Thisseemshelpful

4

2

8

7

3

6

5

YOINK!

42 8

73

6

5

Doesthiswork?

• Wheneversomethingseemsunbalanced,dorotationsuntilit’sokayagain.

LuckytheLackadaisicalLemur

Evenformethisis

prettyvague.Whatdo

wemeanby“seems

unbalanced”?What’s

“okay”?

Idea2:havesomeproxyforbalance

• Maintainingperfectbalanceistoohard.

• Instead,comeupwithsomeproxyforbalance:

• Ifthetreesatisfies[SOMEPROPERTY],thenit’sprettybalanced.

• Wecanmaintain[SOMEPROPERTY] usingrotations.

Thereareactuallyseveral

waystodothis,buttoday

we’llsee…

• 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

Red-BlackTreestheserulesaretheproxyforbalance

• Everynodeiscoloredred orblack.

• Therootnodeisablacknode.

• NIL childrencountasblacknodes.

• Childrenofarednodeareblacknodes.

• Forallnodesx:

• allpathsfromxtoNONE’shavethesamenumberofblacknodesonthem.

42 8

73

5

6

Examples(?)• Everynodeiscoloredred orblack.

• Therootnodeisablacknode.

• NONEchildrencountasblacknodes.

• Childrenofarednodeareblacknodes.

• Forallnodesx:

• allpathsfromxtoNONE’shavethesamenumberofblacknodesonthem.

Yes!

No! No! No!

Why???????

• Thisisprettybalanced.

• Theblacknodes arebalanced

• Therednodesare“spreadout”sotheydon’tmessthingsuptoomuch.

• Wecanmaintainthispropertyasweinsert/deletenodes,byusingrotations.

42 8

73

5

6

9

Thisis“prettybalanced”

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

Luckythe

lackadaisical

lemur

Let’sbuildsomeintuition!

Onepathcouldbetwiceas

longasalltheothersifwepad

itwithrednodes.

Conjecture:theheightofa

red-blacktreeisatmost2log(n)

Thatturnsoutthebebasicallyright.[proofsketch]

• Saythereareb(x)blacknodesinanypathfromxtoNONE.

• (includingx).

• Claim:

• Thenthereareatleast2b(x) – 1nodesinthesubtreeunderneathx.

• [Proofbyinduction– onboardiftime]

Then:

� ≥ 2$ %&&' − 1

≥ 2+,-.+/

01 − 1Rearranging:

� + 1 ≥ 234563'

71 ⇒ ℎ���ℎ� ≤ 2log(� + 1)

x

y

usingtheClaim

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

Okay,soit’sbalanced……butcanwemaintainit?

•Yes!

• Fortherestoflecture:

• sketchofhowwe’ddothis.

• SeeCLRSformoredetails.

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

73

6Example:insert0

0

Whatifitlookslikethis?

73

6

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6Example:insert0

0

No!

Whatifitlookslikethis?

73

6

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6Example:insert0

Can’twejustinsert0as

ablacknode?

0No!

Whatifitlookslikethis?

73

6

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6

Example:insert0

0

• Insteadrecolorlikethis.

• Needtoargue:• RB-Treepropertiesstillhold.

• Whatabouttheredroot?• if6isactuallytheroot,coloritblack.

• Else,recursivelyre-colorupthetree.

Whatifitlookslikethis?

73

6

ish

-1

6

-1Nowtheproblemlooks

likethis,whereI’m

inserting 6

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6

Example:Insert0.

• Actually,thiscan’t

happen?

• Itmighthappenthatwe

justturned0redfrom

thepreviousstep.

• Oritcouldhappenif

isactuallyNIL.

0

Whatifitlookslikethis?

73

6

7

RecallRotations

• MaintainBinarySearchTree(BST)property,whilemovingstuffaround.

BA

CY

XYOINK!

CLAIM:

thisstillhasBSTproperty.

BA

C

Y

X

B

A

C

Y

X

InsertingintoaRed-BlackTree

• Makeanewrednode.

• Insertitasyouwouldnormally.

• Fixthingsupifneeded.

73

6

0

Whatifitlookslikethis?

73

6

YOINK!

3

60

7

Needtoarguethat

ifRB-Treeproperty

heldbefore,itstill

does.

Thepunchline:

• AfewthingsstilllefttocheckforINSERT!• Anythingelsethatmighthappenlooksbasicallylikewhatwejustdid.

• Formallydealingwiththerecursion.

• Youcheckthese!(orseeCLRS)

• DELETE issimilar.

That’sbasicallyit

• Red-BlackTreesalwayshaveheightatmost2log(n+1).

• AswithgeneralBinarySearchTrees,alloperationsareO(height)

• SoalloperationsareO(log(n)).

Pluckythepedanticpenguin

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

Recap

•Hashing!

Nexttime

• Balancedbinarytreesarethebestofbothworlds!

• Butweneedtokeepthembalanced.

• Red-BlackTrees dothatforus.

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

• Cleveridea:haveaproxyforbalance