lecture 7 - stanford universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •idea 0:...
TRANSCRIPT
Lecture7BinarySearchTreesandRed-BlackTrees
Announcements
• HW3released!(DueFriday)
• Specialguestlecturer:SamKim!
Roadmap
Graphs!
Asymptotic
Analysis
Dynamic
ProgrammingGreedyAlgs
MIDTERM
The
Future!
Moredetailedscheduleonthewebsite!
Wearehere
Today
• Beginabriefforayintodatastructures!
• SeeCS166formore!
• Binarysearchtrees
• YoumayrememberthesefromCS106B
• Theyarebetterwhenthey’rebalanced.
thiswillleadusto…
• Self-BalancingBinarySearchTrees
• Red-Black trees.
Whyarewestudyingself-balancingBSTs?
1. Thepunchlineisimportant:
• AdatastructurewithO(log(n))INSERT/DELETE/SEARCH
2. TheideabehindRed-Black Treesisclever
• It’sgoodtobeexposedtocleverideas.
• Alsoit’sjustaestheticallypleasing.
Somedatastructuresforstoringobjectslike(aka,nodes withkeys)
• (Sorted)arrays:
• (Sorted)linkedlists:
• Somebasicoperations:
• INSERT,DELETE,SEARCH
42 871 3 5HEAD
42 871 3 5
5
SortedArrays
• O(n)INSERT/DELETE:
• O(log(n))SEARCH:
42 871 3 5
421 3
42 871 3 5
eg,Binarysearchtoseeif3isinA.
8754.5
Sortedlinkedlists
• O(1)INSERT/DELETE:• (assumingwehaveapointertothelocationoftheinsert/delete)
• O(n)SEARCH:
42 871 3 5
42 871 3 5HEAD
6
42 871 3 5HEAD
MotivationforBinarySearchTrees
Sorted Arrays LinkedListsBinarySearch
Trees*
Search O(log(n)) O(n) O(log(n))
Insert/Delete O(n) O(1) O(log(n))
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.
BinarySearchTrees
4
2
8 7
1
35
• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.
• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.
• Exampleofbuildingabinarysearchtree:
BinarySearchTrees
4
2
8 7
1
35
• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.
• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.
• Exampleofbuildingabinarysearchtree:
BinarySearchTrees
42
8
7
1
35
• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.
• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.
• Exampleofbuildingabinarysearchtree:
BinarySearchTrees
42 8
71
3
5
• It’sabinarytreesothat:• EveryLEFTdescendantofanodehaskeylessthanthatnode.
• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.
• Exampleofbuildingabinarysearchtree:
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.
Aside:thisshouldlookfamiliar
4
2
8 7
1
35
kinda likeQuickSort
BinarySearchTrees
42 8
7
1
3
5
• It’sabinarytreesothat:• EveryLEFTdescendant ofanodehaskeylessthanthatnode.
• EveryRIGHTdescendantofanodehaskeylargerthanthatnode.
42 8
7
1
3
5
BinarySearchTreeNOT aBinary
SearchTree
Whichof
theseisa
BST?
Rememberthegoal
FastSEARCH/INSERT/DELETECanwedothese?
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)
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.)
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
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
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!
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!
Howlongdotheseoperationstake?
• SEARCH isthebigone.
• EverythingelsejustcallsSEARCH andthendoessomesmallO(1)-timeoperation.
42 8
73
5
6
Time=
O(heightoftree)
Treeshavedepth
O(log(n)).Done!
Luckythelackadaisicallemur.
Howlongdoessearchtake?
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?
• 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!
Self-BalancingBinarySearchTrees
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.
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:
• allpathsfromxtoNIL’shavethesamenumberofblacknodesonthem.
42 8
73
5
6
NIL NIL NIL NIL NIL NIL NIL NIL
I’mnotgoingtodrawtheNIL
childreninthefuture,butthey
aretreatedasblacknodes.
Examples(?)• Everynodeiscoloredred orblack.
• Therootnodeisablacknode.
• NIL childrencountasblacknodes.
• Childrenofarednodeareblacknodes.
• Forallnodesx:
• allpathsfromxtoNIL’shavethesamenumberofblacknodesonthem.
Yes!
No! No! No!
Why???????
• Thisisprettybalanced.
• Theblacknodes arebalanced
• Therednodesare“spreadout”sotheydon’tmessthingsuptoomuch.
• Wecanmaintainthispropertyasweinsert/deletenodes,byusingrotations.
42 8
73
5
6
9
Thisisthereallycleveridea!
This Red-Blackstructureisaproxyforbalance.It’sjustasmidgeweakerthanperfectbalance,butwecanactuallymaintainit!
Thisis“prettybalanced”
• Toseewhy,intuitively,let’strytobuildaRed-BlackTreethat’sunbalanced.
Luckythe
lackadaisical
lemur
Let’sbuildsomeintuition!
Onepathcouldbetwiceas
longanotherifwepaditwith
rednodes.
Conjecture:theheightofa
red-blacktreeisatmost2log(n)
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
Okay,soit’sbalanced……butcanwemaintainit?
•Yes!
• Fortherestoflecture:• sketchofhowwe’ddothis.
• SeeCLRSformoredetails.
• (Youarenotresponsibleforthedetailsforthisclass– butyoushouldunderstandthemainideas).
Manycases
73
6
73
6
73
6
• Supposewewanttoinserthere.• eg,wanttoinsert0.
• Andthenthereare9morecasesforallofthevarious
symmetriesofthese3cases…
InsertingintoaRed-BlackTree
• Makeanewrednode.
• Insertitasyouwouldnormally.
73
6Example:insert0
0
Whatifitlookslikethis?
73
6
Manycases
73
6
73
6
73
6
• Supposewewanttoinserthere.• eg,wanttoinsert0.
• Andthenthereare9morecasesforallofthevarious
symmetriesofthese3cases…
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
Weneedabitmorecontext
73
6
Example:insert0
Whatifitlookslikethis?
73
6
-1
Weneedabitmorecontext
73
6
Example:insert0
0
Whatifitlookslikethis?
73
6
• Add0asarednode.
-1
Weneedabitmorecontext
73
6
Example:insert0
0
Whatifitlookslikethis?
73
6
Flip
colors!
• Add0asarednode.
• Claim: RB-Treepropertiesstillhold.
-1
Butwhatifthat wasred?
73
6
Example:insert0
0
Whatifitlookslikethis?
73
6
-1
Morecontext…
73
6
Example:insert0
0
Whatifitlookslikethis?
73
6
-1
-3
Morecontext…
6
Example:insert0
Whatifitlookslikethis?
73
6
-1
-3
Nowwe’re
basicallyinserting
6intosome
smallertree.
Recurse!
Manycases
73
6
73
6
73
6
• Supposewewanttoinserthere.• eg,wanttoinsert0.
• Andthenthereare9morecasesforallofthevarious
symmetriesofthese3cases…
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
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.
Manycases
73
6
73
6
73
6
• Supposewewanttoinserthere.• eg,wanttoinsert0.
• Andthenthereare9morecasesforallofthevarious
symmetriesofthese3cases…
DeletingfromaRed-Blacktree
Funexercise!
Ollietheover-achievingostrich
That’salotofcases
• Youarenotresponsible forthenitty-grittydetailsofRed-BlackTrees.(Forthisclass)
• Thoughimplementingthemisagreatexercise!
• Youshouldknow:
• WhatarethepropertiesofanRBtree?
• And(moreimportant)whydoesthatguaranteethattheyarebalanced?
Whatwasthepointagain?
• Red-BlackTreesalwayshaveheightatmost2log(n+1).
• AswithgeneralBinarySearchTrees,alloperationsareO(height)
• SoalloperationsareO(log(n)).
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))
Today
• Beginabriefforayintodatastructures!
• SeeCS166formore!
• Binarysearchtrees
• YoumayrememberthesefromCS106B
• Theyarebetterwhenthey’rebalanced.
thiswillleadusto…
• Self-BalancingBinarySearchTrees
• Red-Black trees.
Recap
Recap
• Balancedbinarytreesarethebestofbothworlds!
• Butweneedtokeepthembalanced.
• Red-BlackTrees dothatforus.
• WegetO(log(n))-timeINSERT/DELETE/SEARCH
• Cleveridea:haveaproxyforbalance
42 8
73
5
6
Before nexttime
• Pre-lectureexerciseforLecture8
• (More)funwithprobability!
Nexttime
• Hashing!