new lecture 7 - stanford universityweb.stanford.edu/.../cs/cs161/cs161.1176/slides/lecture7.pdf ·...
TRANSCRIPT
![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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/1.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/2.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/5.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/6.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/7.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/8.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/16.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/21.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/23.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/24.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/25.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/26.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/27.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/28.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/29.jpg)
• 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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/30.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/31.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/32.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/33.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/34.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/35.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/36.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/37.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/38.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/39.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/40.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/41.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/42.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/43.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/44.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022081408/60584e93f296730f3a594d26/html5/thumbnails/45.jpg)
Recap
•Hashing!Nexttime
• Balancedbinarytreesarethebestofbothworlds!• Butweneedtokeepthembalanced.• Red-BlackTrees dothatforus.• WegetO(log(n))-timeINSERT/DELETE/SEARCH• Cleveridea:haveaproxyforbalance