SlideShare uma empresa Scribd logo
1 de 26
Binomial Heaps
Referred to “MIT Press: Introduction to Algorithms 2nd
Edition”
Binomial Trees
• The binomial tree B(k) is an ordered tree defined recursively. As
shown in the binomial tree B(0) consists of a single node. The binomial
tree B(k) consists of two binomial trees B(k-1) that are linked together:
the root of one is the leftmost child of the root of the other.
Properties of binomial trees
For the binomial tree B(k),
• 1. there are 2^k nodes,
• 2. the height of the tree is k,
• 3. there are exactly k!/(i!(k-i)!) nodes at depth i for i = 0, 1,
..., k, and
• 4. the root has degree k, which is greater than that of any
other node; moreover if i the children of the root are
numbered from left to right by k - 1, k - 2, ..., 0, child i is
the root of a subtree B(i).
Binomial Heaps
A binomial heap H is a set of binomial trees that satisfies the following
binomial-heap properties.
• 1. Each binomial tree in H obeys the min-heap property: the key of a
node is greater than or equal to the key of its parent. We say that each
such tree is min-heap-ordered.
• 2. For any nonnegative integer k, there is at most one binomial tree in
H whose root has degree k.
Representing binomial heaps
Creating a new binomial heap
• To make an empty binomial heap, the MAKE-BINOMIAL-HEAP
procedure simply allocates and returns an object H , where head[H ] =
NIL. The running time is Θ(1).
Finding the minimum key
• The procedure BINOMIAL-HEAP-MINIMUM returns a pointer to the
node with the minimum key in an n-node binomial heap H. This
implementation assumes that there are no keys with value ∞.
BINOMIAL-HEAP-MINIMUM(H)
1 y ← NIL
2 x ← head[H]
3 min ← ∞
4 while x ≠ NIL
5 do if key[x] < min
6 then min ← key[x]
7 y ← x
8 x ← sibling[x]
9 return y
Uniting two binomial heaps
• The BINOMIAL-HEAP-UNION procedure repeatedly
links binomial trees whose roots have the same degree.
The following procedure links the B(k-1) tree rooted at
node y to the B(k-1) tree rooted at node z; that is, it makes z
the parent of y. Node z thus becomes the root of a B(k)
tree.
BINOMIAL-LINK(y, z)
1 p[y] ← z
2 sibling[y] ← child[z]
3 child[z] ← y
4 degree[z] ← degree[z] + 1
The following procedure unites binomial heaps H1 and H2, returning the
resulting heap. It destroys the representations of H1 and H2 in the
process. Besides BINOMIAL-LINK, the procedure uses an auxiliary
procedure BINOMIAL-HEAP-MERGE that merges the root lists of
H1 and H2 into a single linked list that is sorted by degree into
monotonically increasing order.
BINOMIAL-HEAP-UNION(H1, H2)
1 H ← MAKE-BINOMIAL-HEAP()
2 head[H] ← BINOMIAL-HEAP-MERGE(H1, H2)
3 free the objects H1 and H2 but not the lists they
point to
4 if head[H] = NIL
5 then return H
6 prev-x ← NIL
7 x ← head[H]
8 next-x ← sibling[x]
9 while next-x ≠ NIL
10 do if (degree[x] ≠ degree[next-x]) or
(sibling[next-x] ≠ NIL and
degree[sibling[next-x]] =degree[x] )
11 then prev-x ← x ▹ Cases 1 and 2
12 x ← next-x ▹ Cases 1 and
2(see the figure 2, 5)
13 else if key[x] ≤ key[next-x]
14 then sibling[x] ← sibling[next-x]
▹ Case 3
15 BINOMIAL-
LINK(next-x, x) ▹ Case 3 (1, 4)
16 else if prev-x = NIL ▹ Case 4
17 then head[H] ←
next-x ▹ Case 4
18 else sibling[prev-
x] ← next-x ▹ Case 4
19 BINOMIAL-LINK(x,
next-x) ▹ Case 4
• back
• back
• back
• back
• back
Inserting a node
• The following procedure inserts node x into binomial heap H ,
assuming that x has already been allocated and key[x] has already been
filled in.
BINOMIAL-HEAP-INSERT(H, x)
1 H′ ← MAKE-BINOMIAL-HEAP()
2 p[x] ← NIL
3 child[x] ← NIL
4 sibling[x] ← NIL
5 degree[x] ← 0
6 head[H′] ← x
7 H ← BINOMIAL-HEAP-UNION(H, H′)
Extracting the node with the minimum key
• The following procedure extracts the node with the minimum key
from binomial heap H and returns a pointer to the extracted node.
BINOMIAL-HEAP-EXTRACT-MIN(H)
1 find the root x with the minimum key in
the root list of H,
and remove x from the root list of H
(see the figure)
2 H′ ← MAKE-BINOMIAL-HEAP()
3 reverse the order of the linked list of
x's children,
and set head[H′] to point to the head
of the resulting list (see the figure)
4 H ← BINOMIAL-HEAP-UNION(H, H′)
5 return x
• back
• back
Decreasing a key
• The following procedure decreases the key of a node x in a binomial
heap H to a new value k. It signals an error if k is greater than x's
current key.
BINOMIAL-HEAP-DECREASE-KEY(H, x, k)
1 if k > key[x]
2 then error "new key is greater than current
key"
3 key[x] ← k
4 y ← x
5 z ← p[y]
6 while z ≠ NIL and key[y] < key[z]
7 do exchange key[y] ↔ key[z]
8 ▸ If y and z have satellite fields, exchange
them, too.
9 y ← z
10 z ← p[y]
(see the figure)
• back
Deleting a key
• It is easy to delete a node x's key and satellite information from
binomial heap H in O(lg n) time. The following implementation
assumes that no node currently in the binomial heap has a key of -∞.
BINOMIAL-HEAP-DELETE(H, x)
1 BINOMIAL-HEAP-DECREASE-KEY(H, x, -∞)
2 BINOMIAL-HEAP-EXTRACT-MIN(H)
Exercise 1
Draw the result after inserting nodes with
integer keys from 1 through 15 into an
empty binomial heap in reverse order.
Exercise 2
Draw the result after deleting the node with
key 8 from the final binomial heap in
exercise 1.
Solution to exercise 1
Solution to exercise 2

Mais conteúdo relacionado

Mais procurados

Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
Graph Theory: Connectivity & Isomorphism
Graph Theory: Connectivity & Isomorphism Graph Theory: Connectivity & Isomorphism
Graph Theory: Connectivity & Isomorphism Ashikur Rahman
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphssana younas
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap treezia eagle
 
Yzm 2116 Bölüm 8 - İkili Arama Ağacı - Binary Search Tree
Yzm 2116   Bölüm 8 - İkili Arama Ağacı - Binary Search TreeYzm 2116   Bölüm 8 - İkili Arama Ağacı - Binary Search Tree
Yzm 2116 Bölüm 8 - İkili Arama Ağacı - Binary Search TreeDeniz KILINÇ
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertionKousalya M
 
Application Of Graph Data Structure
Application Of Graph Data StructureApplication Of Graph Data Structure
Application Of Graph Data StructureGaurang Dobariya
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureYaksh Jethva
 

Mais procurados (20)

Hash tables
Hash tablesHash tables
Hash tables
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Binary tree
Binary tree Binary tree
Binary tree
 
Binomial queues
Binomial queuesBinomial queues
Binomial queues
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
 
Graph Theory: Connectivity & Isomorphism
Graph Theory: Connectivity & Isomorphism Graph Theory: Connectivity & Isomorphism
Graph Theory: Connectivity & Isomorphism
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphs
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Yzm 2116 Bölüm 8 - İkili Arama Ağacı - Binary Search Tree
Yzm 2116   Bölüm 8 - İkili Arama Ağacı - Binary Search TreeYzm 2116   Bölüm 8 - İkili Arama Ağacı - Binary Search Tree
Yzm 2116 Bölüm 8 - İkili Arama Ağacı - Binary Search Tree
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Application Of Graph Data Structure
Application Of Graph Data StructureApplication Of Graph Data Structure
Application Of Graph Data Structure
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 
Red black trees
Red black treesRed black trees
Red black trees
 
Hash table
Hash tableHash table
Hash table
 
b+ tree
b+ treeb+ tree
b+ tree
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 

Semelhante a Binomial heaps

Semelhante a Binomial heaps (20)

Applications of datastructures
Applications of datastructuresApplications of datastructures
Applications of datastructures
 
Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Heaps 4up
Heaps 4upHeaps 4up
Heaps 4up
 
b-tree.ppt
b-tree.pptb-tree.ppt
b-tree.ppt
 
358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
 
Red black trees
Red black treesRed black trees
Red black trees
 
Recursion
RecursionRecursion
Recursion
 
Trees
TreesTrees
Trees
 
C12 bst
C12 bstC12 bst
C12 bst
 
C12 bst
C12 bstC12 bst
C12 bst
 
Fileprocessing lec-7
Fileprocessing lec-7Fileprocessing lec-7
Fileprocessing lec-7
 
Lecture3
Lecture3Lecture3
Lecture3
 
Binomial Heap DAA PPT (2).ppt
Binomial Heap DAA PPT (2).pptBinomial Heap DAA PPT (2).ppt
Binomial Heap DAA PPT (2).ppt
 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.ppt
 
lecture 13
lecture 13lecture 13
lecture 13
 

Último

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 

Último (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Binomial heaps

  • 1. Binomial Heaps Referred to “MIT Press: Introduction to Algorithms 2nd Edition”
  • 2. Binomial Trees • The binomial tree B(k) is an ordered tree defined recursively. As shown in the binomial tree B(0) consists of a single node. The binomial tree B(k) consists of two binomial trees B(k-1) that are linked together: the root of one is the leftmost child of the root of the other.
  • 3. Properties of binomial trees For the binomial tree B(k), • 1. there are 2^k nodes, • 2. the height of the tree is k, • 3. there are exactly k!/(i!(k-i)!) nodes at depth i for i = 0, 1, ..., k, and • 4. the root has degree k, which is greater than that of any other node; moreover if i the children of the root are numbered from left to right by k - 1, k - 2, ..., 0, child i is the root of a subtree B(i).
  • 4. Binomial Heaps A binomial heap H is a set of binomial trees that satisfies the following binomial-heap properties. • 1. Each binomial tree in H obeys the min-heap property: the key of a node is greater than or equal to the key of its parent. We say that each such tree is min-heap-ordered. • 2. For any nonnegative integer k, there is at most one binomial tree in H whose root has degree k.
  • 6. Creating a new binomial heap • To make an empty binomial heap, the MAKE-BINOMIAL-HEAP procedure simply allocates and returns an object H , where head[H ] = NIL. The running time is Θ(1).
  • 7. Finding the minimum key • The procedure BINOMIAL-HEAP-MINIMUM returns a pointer to the node with the minimum key in an n-node binomial heap H. This implementation assumes that there are no keys with value ∞. BINOMIAL-HEAP-MINIMUM(H) 1 y ← NIL 2 x ← head[H] 3 min ← ∞ 4 while x ≠ NIL 5 do if key[x] < min 6 then min ← key[x] 7 y ← x 8 x ← sibling[x] 9 return y
  • 8. Uniting two binomial heaps • The BINOMIAL-HEAP-UNION procedure repeatedly links binomial trees whose roots have the same degree. The following procedure links the B(k-1) tree rooted at node y to the B(k-1) tree rooted at node z; that is, it makes z the parent of y. Node z thus becomes the root of a B(k) tree. BINOMIAL-LINK(y, z) 1 p[y] ← z 2 sibling[y] ← child[z] 3 child[z] ← y 4 degree[z] ← degree[z] + 1
  • 9. The following procedure unites binomial heaps H1 and H2, returning the resulting heap. It destroys the representations of H1 and H2 in the process. Besides BINOMIAL-LINK, the procedure uses an auxiliary procedure BINOMIAL-HEAP-MERGE that merges the root lists of H1 and H2 into a single linked list that is sorted by degree into monotonically increasing order. BINOMIAL-HEAP-UNION(H1, H2) 1 H ← MAKE-BINOMIAL-HEAP() 2 head[H] ← BINOMIAL-HEAP-MERGE(H1, H2) 3 free the objects H1 and H2 but not the lists they point to 4 if head[H] = NIL 5 then return H 6 prev-x ← NIL 7 x ← head[H] 8 next-x ← sibling[x]
  • 10. 9 while next-x ≠ NIL 10 do if (degree[x] ≠ degree[next-x]) or (sibling[next-x] ≠ NIL and degree[sibling[next-x]] =degree[x] ) 11 then prev-x ← x ▹ Cases 1 and 2 12 x ← next-x ▹ Cases 1 and 2(see the figure 2, 5) 13 else if key[x] ≤ key[next-x] 14 then sibling[x] ← sibling[next-x] ▹ Case 3 15 BINOMIAL- LINK(next-x, x) ▹ Case 3 (1, 4) 16 else if prev-x = NIL ▹ Case 4 17 then head[H] ← next-x ▹ Case 4 18 else sibling[prev- x] ← next-x ▹ Case 4 19 BINOMIAL-LINK(x, next-x) ▹ Case 4
  • 16. Inserting a node • The following procedure inserts node x into binomial heap H , assuming that x has already been allocated and key[x] has already been filled in. BINOMIAL-HEAP-INSERT(H, x) 1 H′ ← MAKE-BINOMIAL-HEAP() 2 p[x] ← NIL 3 child[x] ← NIL 4 sibling[x] ← NIL 5 degree[x] ← 0 6 head[H′] ← x 7 H ← BINOMIAL-HEAP-UNION(H, H′)
  • 17. Extracting the node with the minimum key • The following procedure extracts the node with the minimum key from binomial heap H and returns a pointer to the extracted node. BINOMIAL-HEAP-EXTRACT-MIN(H) 1 find the root x with the minimum key in the root list of H, and remove x from the root list of H (see the figure) 2 H′ ← MAKE-BINOMIAL-HEAP() 3 reverse the order of the linked list of x's children, and set head[H′] to point to the head of the resulting list (see the figure) 4 H ← BINOMIAL-HEAP-UNION(H, H′) 5 return x
  • 20. Decreasing a key • The following procedure decreases the key of a node x in a binomial heap H to a new value k. It signals an error if k is greater than x's current key. BINOMIAL-HEAP-DECREASE-KEY(H, x, k) 1 if k > key[x] 2 then error "new key is greater than current key" 3 key[x] ← k 4 y ← x 5 z ← p[y] 6 while z ≠ NIL and key[y] < key[z] 7 do exchange key[y] ↔ key[z] 8 ▸ If y and z have satellite fields, exchange them, too. 9 y ← z 10 z ← p[y] (see the figure)
  • 22. Deleting a key • It is easy to delete a node x's key and satellite information from binomial heap H in O(lg n) time. The following implementation assumes that no node currently in the binomial heap has a key of -∞. BINOMIAL-HEAP-DELETE(H, x) 1 BINOMIAL-HEAP-DECREASE-KEY(H, x, -∞) 2 BINOMIAL-HEAP-EXTRACT-MIN(H)
  • 23. Exercise 1 Draw the result after inserting nodes with integer keys from 1 through 15 into an empty binomial heap in reverse order.
  • 24. Exercise 2 Draw the result after deleting the node with key 8 from the final binomial heap in exercise 1.