Today’s&Goals& • Mergeable&Heaps& – Review&Binary&Heaps& – Linked2lists& – Binomial&Trees& – Binomial&Heaps& • Red2Black& h

Binary&&representaPons& ExtractMax&

16&&&14&&&10&&&8&&&&&7&&&&&&9&&&&&3&&&&&2&&&&&4&&&&&1& Return&and&remove&the&largest&element&in&the&&

1&&&2&&&&3&&&&4&&&&5&&&&6&&&&7&&&&8&&&&9&&&&10& 16&

16& 14& 10&

14& 10& 8& 7& 9& 3&

8& 7& 9& 3& 2& 4& 1&

2& 4& 1& ExtractMax& ExtractMax& Return&and&remove&the&largest&element&in&the&set& Return&and&remove&the&largest&element&in&the&set&

?& 1& Heapify-

14& 10& 14& 10&

8& 7& 9& 3& 8& 7& 9& 3&

2& 4& 1& 2& 4&

ExtractMax& ExtractMax& Return&and&remove&the&largest&element&in&the&set& Return&and&remove&the&largest&element&in&the&set&

14& Heapify-

8& 10&

4& 7& 9& 3&

2& 1& Mergeable&Heap& Binary&heaps&

A&mergeable&heap&is&any&data&structure&that&supports&the& following&five&operaPons,&in&which&each&element&has&a&key:& • MakeHeap()&2&creates&and&returns&a&new&heap&containing&no&elements.& • Insert(H,x)&2&inserts&element&x,&whose&key&has&already&been&filled&in,&into& heap&H.& • Minimum(H)&2&returns&a&pointer&to&the&element&in&heap&H&whose&key&is& minimum.& • Extract:Min(H)&2&deletes&the&element&from&heap&H&whose&key&is&minimum,& returning&a&pointer&to&the&element.&

• Union(H1,-H2)-2&creates&and&returns&a&new&heap&that&contains&all&the& elements&of&heaps&H1&and&H2.&Heaps&H1&and&H2&are&“destroyed”&by&this& operaPon.& • ALSO:&Increase2Element,&Delete&

Mergeable&heaps& Union&for&binary&heaps&

2 Mergeable&heaps&support& the&union&operaPon&

2 Allows&us&to&combine&two& concatenate the arrays and heaps&to&get&a&single&heap& then call Build-Heap

2 Union&runPme&for&binary& heaps?& & Adapted from: Worksheet:& Kevin Wayne Binomial&& (Doubly)&linked2list&heap& B0- Bk-

Bk: a binomial tree Bk-1 with the Linked-list addition of a left child with Bk:1- another binomial tree B k-1 Bk:1-

B0- B1- B2- B3- B4-

Binomial&Tree& Binomial&Tree&

Bk- Bk- Number of nodes with Height? B B 1- B0- 1- B0- respect to k? B2- B2- Bk:1- Bk:1-

B0- B1- B2- B3- B4- B0- B1- B2- B3- B4- Binomial&Tree& Binomial&Tree&

Bk- Bk- Degree of root node? What are the children of B B 1- B0- 1- B0- B2- the root? B2- Bk:1- Bk:1-

B0- B1- B2- B3- B4- B0- B1- B2- B3- B4-

Binomial&Heap& Binomial&Tree& •Binomial&heap&& Vuillemin,&1978.& Sequence&of&binomial&trees&that&saPsfy&binomial&heap&property:& Why is it called a binomial tree? • each&tree&is&min2heap&ordered& • top&level:&full&or&empty&binomial&tree&of&order&k& • which&are&empty&or&full&is&based&on&the&number&of&elements&

6- 3 18 depth-0-

depth-1- 8 29- 10- 44- 37-

depth-2- 30- 23- 22- 48- 31- 17- depth-3 - 45- 32- 24- 50-

depth-4 - B4- 55- B4- B1- B0- Binomial&Heap& Binomial&Heap:&&ProperPes& Represented&as&a&series&of&arrays&(each&represenPng&a&min2heap)& How many heaps? A0: [18] A1: [3, 7] A2: empty A3: empty A3: [6, 8, 29, 10, 44, 30, 23, 22, 48, 31, 17, 45, 32, 24, 55] 6- 3 18 6- 3 18

8 29- 10- 44- 37- 8 29- 10- 44- 37-

30- 23- 22- 48- 31- 17- N-=-19- 30- 23- 22- 48- 31- 17- N-=-19- #-trees-=-3- #-trees-=-3- height-=-4- height-=-4- 45- 32- 24- 50- 45- 32- 24- 50- binary-=-10011 binary-=-10011 55- 55- B4- B1- B0- B4- B1- B0-

Binomial&Heap:&&ProperPes& Binomial&Heap:&&ProperPes& Where is the max/min? Runtime of max/min?

6- 3 18 6- 3 18

8 29- 10- 44- 37- 8 29- 10- 44- 37-

30- 23- 22- 48- 31- 17- N-=-19- 30- 23- 22- 48- 31- 17- N-=-19- #-trees-=-3- #-trees-=-3- height-=-4- height-=-4- 45- 32- 24- 50- 45- 32- 24- 50- binary-=-10011 binary-=-10011 55- 55- B4- B1- B0- B4- B1- B0- Binomial&Heap:&&ProperPes& Binomial&Heap:&&Union& Height? •How&can&we&merge&two&binomial&tree&heaps&of&the&same&size&(2 k)?& • connect&roots&of&H'&and&H''& • choose&smaller&key&to&be&root&of&H&& - largest tree = Blog n Runtime? - height of that tree is log n 6- 3 18 6-

8 29- 10- 44- 37- 8 29- 10- 44-

30- 23- 22- 48- 31- 17- N-=-19- 30- 23- 22- 48- 31- 17- #-trees-=-3- height-=-4- 45- 32- 24- 50- 45- 32- 24- 50- binary-=-10011 55- 55- B4- B1- B0- H'- H''-

Binomial&Heap:&&Union& Binomial&Heap:&&Union& Go through each tree size starting at 0 and merge as we go

6- 3 18 6- 3 18

8 29- 10- 44- 37- 8 29- 10- 44- 37-

30- 23- 22- 48- 31- 17- 30- 23- 22- 48- 31- 17- 15- 7 12 15- 7 12 45- 32- 24- 50- 45- 32- 24- 50- 28- 33- 25- 28- 33- 25- 55- 55- +- 41-

How can we combine/merge binomial 1- 1- 1- 1 0 0 1 1 heaps (i.e. a combination of binomial - - - + 0 0- 1- 1- 1 19-+-7-=-26- tree heaps)? 1 1- 0- 1- 0 3- 3- 12

15- 7- 37- 7- 37- 18 Binomial&Heap:&&Union& 28- 33- 25- 25- Analogous&to&binary&addiPon& 41- 6- 3 18

8 29- 10- 44- 37- •Running&Pme?&

30- 23- 22- 48- 31- 17- – 15- 7 12 ProporPonal&to&number&of&trees&in&root&lists&2&O(log2&N)& 45- 32- 24- 50- – 28- 33- 25- 55- +- 41- 6- 3- 12-

1- 1- 1- 8 29- 10- 44- 15- 7- 37- 18- 1 0- 0- 1- 1 30- 23- 22- 48- 31- 17- 28- 33- 25- + 0 0- 1- 1- 1 19-+-7-=-26- 45- 32- 24- 50- 41- 1 1- 0- 1- 0 55-

Binomial&Heap:&&Delete&Min/Max& Binomial&Heap:&&Delete&Min& We can find the min/max in O(log n). •Delete&node&with&minimum&key&in&binomial&heap&H.& How can we extract it? – Find&root&x&with&min&key&in&root&list&of&H,&and&delete& – H'&←&&broken&binomial&trees& Hint: Bk consists of binomial trees: – H&&←&&Union(H',&H)&

Bk-1, Bk-2, …, B0 3- 6 18 3- 6 18

8 29- 10- 44- 37- 8 29- 10- 44- 37-

30- 23- 22- 48- 31- 17- 30- 23- 22- 48- 31- 17- H- H-

45- 32- 24- 50- 45- 32- 24- 50-

55- 55- Binomial&Heap:&&Delete&Min& Binomial&Heap:&&Decrease&Key& •Delete&node&with&minimum&key&in&binomial&heap&H.& – Find&root&x&with&min&key&in&root&list&of&H,&and&delete& •Just&call&Decrease2Key/Increase2Key&of&Heap& – H'&←&&broken&binomial&trees& – Suppose&x&is&in&binomial&tree&Bk& – H&&←&&Union(H',&H)& – Bubble&node&x&up&the&tree&if&x&is&too&small& •Running&Pme:&&

•Running&Pme?&& & 3- 6 18 – ProporPonal&to&depth&of&node&x& 3- 6 18

depth-=-3 8 29- 10- 44- 37- 8 29- 10- 44- 37-

30- 23- 22- 48- 31- 17- 30- 23- 22- 48- 31- 17- H- H-

45- 32- 24- 50- x- 32- 24- 50-

55- 55-

Binomial&Heap:&&Delete& Binomial&Heap:&&Insert&

•Delete&node&x&in&binomial&heap&H& •Insert&a&new&node&x&into&binomial&heap&H& – Decrease&key&of&x&to&2∞& – H'&←&&MakeHeap(x)& – H&&←&&Union(H',&H)& – Delete&min&

•Running&Pme.&& 3- 6 18 x •Running&Pme:&& 8 29- 10- 44- 37-

30- 23- 22- 48- 31- 17- H- H'-

45- 32- 24- 50-

55- Build2Heap& Build2Heap& •Call&insert&n&Pmes& •Call&insert&n&Pmes& RunQme?- Consider-inserQng-n-numbers- times cost

Can-we-get-a-Qghter-bound?-  how-many-Qmes-will-B0-be-empty? --

 how-many-Qmes-will-we-need-to-merge-with-B0?-

 how-many-Qmes-will-we-need-to-merge-with-B1?-

 how-many-Qmes-will-we-need-to-merge-with-B2?-

 …-

 how-many-Qmes-will-we-need-to-merge-with-Blog-n?-

Heaps& BST&refresher&

• Data&structures&that&support&many&dynamic2set&operaPons& • Can&be&used&as&a&both&a&dicPonary&and&as&a&priority&queue& • Basic&operaPons&take&Pme&proporPonal&to&the&height&of&the& tree& – Complete&binary&tree&with&n&nodes:&worst&case&Θ(log&n)& – For&linear&chain&of&n&nodes:&worst&case&Θ(n)& • Red2black&trees& – VariaPon&of&binary&search&tree& – Balanced:&height&is&O(log&n)& – OperaPons&take&O(log&n)&in&the&worst&case& Red2Black&Tree& Red2black&tree&

• A&variaPon&of&binary&search&trees& • A&red:black&tree&is&a&binary&search&tree&+1&bit&per& • Balanced:&height&is&O(log&n)& node:&a&color&a

Red2Black&Tree&ProperPes& Red2Black&Tree&Example&

1. Every&node&is&either&red&or&black.& 26- 2. The&root&is&black.& 3. Every&(null)&leaf&is&black.& 17- 4. If&a&node&is&red,&then&both&its&children&are&black.& 30- 47- 5. For&each&node,&all&paths&from&the&node&to& descendant&leaves&contain&the&same&number&of& black&nodes.& 38- 50-

NIL- Height&of&a&Red2Black&Tree& Red2Black&Tree&Example&

h=4,bh=2, • Height-of-a-node:-is&the&number&of&edges&in&a& 26-

longest&path&to&a&leaf& h=1,bh=1, h=3,bh=2, • Black:height-of-a-node-x:&bh(x)-is&the&number& 17- of&black&nodes&(including&NIL)&on&the&path& h=2,bh=1, h=2,bh=1, from&x&to&a&leaf,¬&including&x.& 30- 47- – Well2defined&due&to&property&5& h=1,bh=1, h=1,bh=1, 38- 50-

NIL-

Worksheet:&Prove& Worksheet:&Prove&

• Claim-1:&Any&node&with&height&h&has&black2 • Claim-2:&The&subtree&rooted&at&node&x& height&≥&h/2.& contains&≥&2bh(x)&21& & The&key&to&success& OperaPons&on&Red2black&trees&

• Lemma:&A&red&black&tree&with&n&internal&nodes& • Non2modifying&operaPons&run&as&before& has&height&≤&2log(n+1).& – Minimum,&Maximum,&Successor,&Predecessor,&Search& – O(height)&!&O(log&n), h=4,bh=2, • Proof:&Let&h,and&b&be&the&height&and&black2 26- height&of&the&root.&Then,& h=1,b h=1, h=3,bh=2, 17- n ≥ 2b −1≥ 2h/2 −1 h=2,bh=1, h=2,bh=1, → log(n +1) ≥ h / 2 30- 47- h=1,bh=1, h=1,bh=1, → h ≤ 2log(n +1). 38- 50-

InserPng& DelePng&

• When&inserPng&a&new&key,&e.g.&40,&should&it&be:& • When&delePng&an&exisPng&key,&what&happens&if&it’s:& h=4,bh=2, h=4,bh=2, 26- 26-

h=3,bh=1, h=3,bh=2, h=3,bh=1, h=3,bh=2, 17- 17-

h=2,bh=1, h=2,bh=1, h=2,bh=1, h=2,bh=1, 30- 47- – Red?-OK& 30- 47- – Red?-Could&violate&property&4.& h=1,bh=1, – Black?-Could&lead&to&violaPon&of:& h=1,bh=1, – Black?-Could&violate&property&5.& 38- 50- &&&&&properPes&2,4,&and/or&5& 38- 50- h=1,bh=1, h=1,bh=1, RotaPons& Rotate&Analysis&

• The&basic&tree2restructuring&operaPon& • Needed&to&maintain&red2black&trees&as&balanced&binary& • Time:&O(1)& search&trees& • Used&in&AVL&and&splay&trees& • Changes&only&the&local&pointer&structure.& • Can&also&talk&about&rotaPng&on& • Does¬&impact&binary&tree&property& edge,&rather&than&node& • Both&lep&and&right&rotaPons&(inverse&of&each&other)& • Takes&as&input:&red2black&tree&and&a&node&within&the& tree& &

InserPon& Insert2Fixup:&Case&1&–&y&is&red&

• Idea:&& C- C- – Run&inserPon&as&before& y& A- – Set&z&to&red& A- D- α- z& δ& ε& – Fix&possible&violaPons:& α- z& δ& ε& B- 1. OK& B- β& γ& 2. If&z&is&root& β& γ& 3. OK& • • 4. If&z.p&is&red& z.p.p&must&be&black& Make&z.p.p&red& 5. OK& • Make&z.p&and&y&black& • Set&z.p.p&as&the&new&z, Insert2Fixup:&Case&2&–&y&is&black,&z&is&right&child& Insert2Fixup:&Case&3&–&y&is&black,&z&is&lep&child&

C- C- C- y C- y δ& δ& δ& A- δ& B- B- A- z& α- z& z& γ& A- γ& A- B- α- z& B- α- β& α- β& β& γ& β& γ& • Make&z.p&black&and&z.p.p& • No&longer&have&2&reds&in&a& • Rotate&z&and&z.p, red& row& • Rotate&on&z.p.p, • z.p&is&now&black&!&no& more&iteraPons,

RB2Insert&Analysis& DelePon&

• O(log&n)&Pme&for&original&insert& • Idea:&& – Run&delePon&(mostly)&as&before& • Within&RB2Insert2Fixup& – If&y’s&original&color&black,&fix& possible&violaPons:& – Each&iteraPon&takes&O(1)&Pme& 1. OK& – Each&iteraPon&either&fixes&the&tree&or&moves&z&up& 2. If&y&is&the&root&and&x&is&red& two&levels& 3. OK& 4. If&x.p&and&x&are&both&red& – There&are&at&most&2&rotaPons&overall& 5. Any&simple&path&containing&y&has& – O(log&n)&levels&!&O(log&n)&Pme& 1&fewer&black&nodes& Delete2Fixup:&Case&1&–&w&is&red& Delete2Fixup:&Case&2&–&w&is&black&&&both&children&are&black&

new&x& B- B- B- w& w& x& B- E- x& D- A- D- A- x& A- new&w& C- E- A- C- C- E- C- E-

• W&must&have&black&children&• New&sibiling&of&x&must&now& • Take&1&black&off&x,and&w& as&new&x, • Make&w&black&and&x.p&red& be&black& • Move&black&to&x.p, • If&new,x&had&been&red,& • Lep&rotate&on&x.p& • Goto&case&2,3,&or&4& • Do&next&iteraPon&with&x.p& break&and&new,x&to&black,

Delete2Fixup:&Case&3&–&w&is&black&& Delete2Fixup:&Case&4&–&w&is&black&& &&w’s&lep&child&is&red,&right&child&is&black& &&w’s&right&child&is&red&

new&x& D- B- B- B- w& w& x& x& E- D- C- D- B- A- A- A- C- E- D- C- E- A- C-

E-

• Make&w&red&and&w’s& • Right&rotate&on&w, • Make&w&be&x.p’s&color& • Lep&rotate&on&w, • • lep&child&black& Make&x.p&black&and&w’s& Remove&extra&black&on&x& • !&case&4& right&child&black& • Set&x&to&root& RB2Delete&Analysis& • O(log&n)&Pme&to&delete& • RB2Delete2Fixup& – Only&Case&2&iterates&& • (all&others&progress&to&next&case&or&halt)& • X&moves&up&one&level& – Each&of&cases&1,&3,&and&4&has&1&rotaPon&!&≤&3& rotaPons&in&all& – Hence&O(log&n)&overall&