lecture 7 - stanford universityweb.stanford.edu/class/archive/cs/cs161/cs161.1182/... · •idea 0:...
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/29.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
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/33.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 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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/38.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/39.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/40.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/42.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/43.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/44.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/45.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/46.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/47.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/48.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/49.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/50.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/51.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/52.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/53.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/54.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/55.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/56.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/57.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/58.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/59.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/60.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/61.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071007/5fc46e6a243fb14791154cb8/html5/thumbnails/62.jpg)
Before nexttime
• Pre-lectureexerciseforLecture8
• (More)funwithprobability!
Nexttime
• Hashing!