Post on 17-Oct-2020
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 5Search:Binarysearchtoseeif3isinA.
8754.5
Select:ThirdsmallestisA[3].
Thebestofbothworlds
Sorted Arrays LinkedLists BinarySearchTrees*
Search O(log(n)) O(n) O(log(n))
Insert/Delete O(n) O(1) O(log(n))
Treeterminology
42 8
7
1
3
5Thisnodeistheroot
Thisisanode.Ithasakey (7).
Thesenodesareleaves.
Theleft childof is3 2
Therightchildof is3 4
Bothchildrenof areNIL1
Todayallkeysaredistinct.
BinarySearchTrees
4
2
8 71
3 5
• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.
• Exampleofbuildingabinarysearchtree:
BinarySearchTrees
4
2
8 71
3 5
• It’sabinarytreesothat:• EveryLEFTdescendentofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.
• Exampleofbuildingabinarysearchtree:
BinarySearchTrees
42
8
71
3 5
• 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 71
3 5
kinda likeQuickSort
BinarySearchTrees
42 8
7
1
3
5
• It’sabinarytreesothat:• EveryLEFTdescendent ofanodehaskeylessthanthatnode.• EveryRIGHTdescendentofanodehaskeylargerthanthatnode.
42 8
7
1
3
5
BinarySearchTree NOT aBinarySearchTree
SEARCHinaBinarySearchTreedefinitionbyexample
42 8
7
1
3
5EXAMPLE:Searchfor4.
EXAMPLE:Searchfor4.5• Itturnsoutitwillbeconvenient
toreturn4inthiscase• (thatis,return thelastnode
beforewewentoffthetree)
Ollietheover-achievingostrich
Writepseudocode(oractualcode)toimplementthis!
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
DELETEinaBinarySearchTree
42 8
7
1
3
5EXAMPLE:Delete2
• DELETE(key):• x=SEARCH(key)• if x.key ==key:• ….deletex….
Thisisabitmorecomplicated…x= 2
DELETEinaBinarySearchTreeseveralcases(byexample)saywewanttodelete3
Thistriangleisacartoonforasubtree
3Case1:if3isaleaf,justdeleteit. 2
3
Case2:if3hasjustonechild,movethatup.
5 5
2
55
Ollietheover-achievingostrich
Wewon’twritedownpseudocodeforthis– trytodoityourself!
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
Howlongdotheseoperationstake?• SEARCH isthebigone.• EverythingelsejustcallsSEARCH andthendoessomesmallO(1)-timeoperation.
42 8
73
5
6
Time=O(depthoftree)
TreeshavedepthO(log(n)).Done!
Luckythelackadaisicallemur.
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.
Whattodo?
• Keeptrackofhowdeepthetreeisgetting.
• Ifitgetstootall,re-doeverythingfromscratch.• AtleastΩ(n)everysooften….
•Otherideas?
Ollietheover-achievingostrich
Howoftenis“everysooften”intheworstcase?It’sactuallypretty
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
Evenformethisisprettyvague.Whatdowemeanby“seemsunbalanced”?What’s
“okay”?
Idea2:havesomeproxyforbalance
• Maintainingperfectbalanceistoohard.• Instead,comeupwithsomeproxyforbalance:• Ifthetreesatisfies[SOMEPROPERTY],thenit’sprettybalanced.• Wecanmaintain[SOMEPROPERTY] usingrotations.
Thereareactuallyseveralwaystodothis,buttodaywe’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.
Luckythelackadaisicallemur
Let’sbuildsomeintuition!
Onepathcouldbetwiceaslongasalltheothersifwepaditwithrednodes.
Conjecture:theheightofared-blacktreeisatmost2log(n)
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.
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’twejustinsert0asablacknode?
0No!
Whatifitlookslikethis?
73
6
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
InsertingintoaRed-BlackTree
• Makeanewrednode.• Insertitasyouwouldnormally.• Fixthingsupifneeded.
73
6Example:Insert0.
• Actually,thiscan’thappen?
• Itmighthappenthatwejustturned0redfromthepreviousstep.
• OritcouldhappenifisactuallyNIL.
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
NeedtoarguethatifRB-Treepropertyheldbefore,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 LinkedListsBalanced
BinarySearchTrees
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