BINOMIAL HEAP
BINOMIAL
EXPLANATION HEAP
⮚ Binomial Heap is an extension of Binary Heap
⮚ It provides faster union or merge operation together
with other operations provided by Binary Heap.
⮚ Binary heap is used to implement priority queue, as
seen in the previous sections
“A Binomial Heap is a collection of Binomial Trees”
BINOMIAL
EXPLANATION HEAP
⮚ A binomial Heap is a collection of Binomial
Trees.
⮚ A binomial tree Bk is an ordered tree
defined recursively.
⮚ A binomial Tree B0 is consists of a single
node.
⮚ A binomial tree Bk is consisting of k
binomial trees Bk-1 that are linked
together
⮚ The root of one is the left most child of
the root of the other
BINOMIAL
PROPERTIES HEAP
➢ A Binomial Heap is a set of Binomial Trees where
each Binomial Tree follows Min Heap property
➢ There can be at most one Binomial Tree of any
degree.
➢ Properties Each binomial tree in H is heap-ordered.
➢ So the key of a node is greater than or equal to the
key of its parent.
➢ There is at most one binomial tree in H, whose root
has a given degree.
BINOMIAL
PROPERTIES AND EXAMPLE HEAP
A Binomial Tree of order k has following
properties.
⮚ It has exactly 2^k nodes. It has depth as
k.
⮚ There are exactly nodes at depth i for i =
0, 1, . . . , k.
⮚ The root has degree k and children of root
are themselves Binomial Trees with order
k-1, k-2,.. 0 from left to right.
BINOMIAL
EXAMPLE HEAP
This binomial Heap H consists of binomial
trees B0, B2 and B3.
They have 1, 4 and 8 nodes respectively.In
total n = 13 nodes.
The root of binomial trees are linked by a
linked list in order of increasing degree
BINOMIAL
OPERATION HEAP
The main operation in Binomial Heap is union()
All other operations mainly use this operation.
The union() operation is to combine two Binomial
Heaps into one
BINOMIAL
OPERATION HEAP
insert(H, k): Inserts a key ‘k’ to Binomial Heap ‘H’.
This operation first creates a Binomial Heap with
single key ‘k’, then calls union on H and the new
Binomial heap.
getMin(H): A simple way to getMin() is to traverse
the list of root of Binomial Trees and return the
minimum key.
BINOMIAL
OPERATION HEAP
extractMin(H): We first call getMin() to find the
minimum key Binomial Tree, then we remove the node
and create a new Binomial Heap by connecting all
subtrees of the removed minimum node.
BINOMIAL
OPERATION HEAP
Finally, we call union() on H and the newly created
Binomial Heap. delete(H): Like Binary Heap, delete
operation first reduces the key to minus infinite, then
calls extractMin(). decreaseKey(H): decreaseKey() is
also similar to Binary Heap. We compare the decreases
key with it parent and if parent’s key is more, we swap
keys and recur for the parent. We stop when we either
reach a node whose parent has a smaller key or we hit
the root node.
BINOMIAL
OPERATION HEAP
Union Operation Given two Binomial Heaps H1 and H2,
union(H1, H2) creates a single Binomial Heap.
The first step is to simply merge the two Heaps in non-
decreasing order of degrees.After the simple merge, we
need to make sure that there is at most one Binomial
Tree of any order.
To do this, we need to combine Binomial Trees of the
same order. We traverse the list of merged roots, we
keep track of three-pointers, prev, x and next-x.
BINOMIAL
OPERATION HEAP
There can be following 4 cases when we traverse the list
of roots.
Orders of x and next-x are not same, we simply move
ahead.
In following 3 cases orders of x and next-x are same.
If the order of next-next-x is also same, move ahead.
If the key of x is smaller than or equal to the key of
next-x, then make next-x as a child of x by linking it
with x.
If the key of x is greater, then make x as the child of
BINOMIAL
RESULT HEAP
BINOMIAL
RESULT HEAP
BINOMIAL
import java.io.*; HEAP BinomialHeapNode ret;
class BinomialHeapNode { if (sibling != null)
int key, degree; ret =
BinomialHeapNode parent; sibling.reverse(this);
BinomialHeapNode sibling; else
BinomialHeapNode child; ret = this;
public BinomialHeapNode(int k) sibling = sibl;
{ return ret;
}
key = k; public BinomialHeapNode
degree = 0; findMinNode()
parent = null; {
sibling = null; BinomialHeapNode x =
child = null; this, y = this;
} int min = x.key;
public BinomialHeapNode while (x != null) {
reverse(BinomialHeapNode sibl)
{
BINOMIAL
if (x.key < min) { else {
y = x;
HEAP
min = x.key; node=temp.child.findANodeWithKey(valu
} e);
if (node == null)
x = x.sibling; temp = temp.sibling;
} else
return y; break;
} }
public BinomialHeapNode }
findANodeWithKey(int value) return node;
{ }
BinomialHeapNode temp = public int getSize()
this, node = null; {
while (temp != null) { return (1 + ((child == null)
if (temp.key == value) { ? 0 : child.getSize())+ ((sibling ==
node = temp; null) ? 0 : sibling.getSize()));
break; }
} }
if (temp.child == null) class BinomialHeap {
temp = temp.sibling; private BinomialHeapNode
BINOMIAL
public BinomialHeap() Nodes = temp;
{
HEAP size = 1;
Nodes = null; }
size = 0; else {
}
public boolean isEmpty() { return unionNodes(temp);
Nodes == null; } size++;
public int getSize() { return }
size; } }
public void makeEmpty() }
{ private void
Nodes = null; merge(BinomialHeapNode binHeap)
size = 0; {
} BinomialHeapNode
public void insert(int value) temp1 = Nodes, temp2 = binHeap;
{ while ((temp1 !=
null) && (temp2 != null)) {
if (value > 0) { if (temp1.degree
BinomialHeapNode temp= new == temp2.degree) {
BinomialHeapNode(value);
if (Nodes == null) {
BINOMIAL
HEAP
BinomialHeapNode tmp = temp2; else {
temp2 = temp2.sibling; BinomialHeapNode tmp = temp1;
tmp.sibling = temp1.sibling; temp1 = temp2;
temp1.sibling = tmp; temp2 = temp2.sibling;
temp1 = tmp.sibling; temp1.sibling = tmp;
} if (tmp == Nodes) {
else { Nodes = temp1;
if (temp1.degree < temp2.degree) }
{ else {
if ((temp1.sibling == null)|| }
(temp1.sibling.degree> temp2.degree)) { }
BinomialHeapNode tmp = temp2; }
temp2 = temp2.sibling; }
tmp.sibling = temp1.sibling; if (temp1 == null) {
temp1.sibling = tmp; temp1 = Nodes;
temp1 = tmp.sibling; while (temp1.sibling != null) {
} temp1 = temp1.sibling;
else { }
temp1 = temp1.sibling;
}
BINOMIAL
temp1.sibling = temp2; HEAP else {
} if (temp.key <= nextTemp.key) {
else { temp.sibling = nextTemp.sibling;
} nextTemp.parent = temp;
} nextTemp.sibling =
private void temp.child;
unionNodes(BinomialHeapNode binHeap) temp.child = nextTemp;
{ temp.degree++;
merge(binHeap); }
BinomialHeapNode prevTemp = null, else {
temp = Nodes, if (prevTemp == null) {
nextTemp = Nodes.sibling; Nodes =
while (nextTemp != null) { nextTemp;
if ((temp.degree != }
nextTemp.degree)|| ((nextTemp.sibling != else {
null)&& (nextTemp.sibling.degree== prevTemp.sibling =
temp.degree))) { nextTemp;
prevTemp = temp; }
temp = nextTemp; temp.parent = nextTemp;
}
BINOMIAL
temp.sibling = nextTemp.child; HEAP extractMin();
nextTemp.child = temp; }
nextTemp.degree++; }
temp = nextTemp; public void decreaseKeyValue(int
} old_value,
}
nextTemp = temp.sibling; int new_value)
} {
} BinomialHeapNode temp=
public int findMinimum() Nodes.findANodeWithKey(old_value);
{ if (temp == null)
return return;
Nodes.findMinNode().key; temp.key = new_value;
} BinomialHeapNode
public void delete(int value) tempParent = temp.parent;
{ while ((tempParent != null)
if ((Nodes != null)&& && (temp.key < tempParent.key))
(Nodes.findANodeWithKey(value) != null)) { {
decreaseKeyValue(value, findMinimum()-1); int z = temp.key;
temp.key = tempParent.key;
tempParent.key = z;
BINOMIAL
temp = tempParent; HEAP else {
tempParent = tempParent.parent; prevTemp.sibling = temp.sibling;
} }
} temp = temp.child;
public int extractMin() BinomialHeapNode fakeNode =
{ temp;
if (Nodes == null) while (temp != null) {
return -1; temp.parent = null;
BinomialHeapNode temp = Nodes, prevTemp temp = temp.sibling;
= null; }
BinomialHeapNode minNode = if ((Nodes == null) &&
Nodes.findMinNode(); (fakeNode == null)) {
while (temp.key != size = 0;
minNode.key) { }
prevTemp = temp; else {
temp = temp.sibling; if ((Nodes == null) && (fakeNode
} != null)) {
if (prevTemp == null) { Nodes = fakeNode.reverse(null);
Nodes = temp.sibling; size = Nodes.getSize();
} }
BINOMIAL
else { HEAP private void displayHeap(BinomialHeapNode
if ((Nodes != null) && (fakeNode == null)) r)
{ {
size = Nodes.getSize(); if (r != null) {
} displayHeap(r.child);
else { System.out.print(r.key + " ");
unionNodes(fakeNode.reverse(null)); displayHeap(r.sibling);
size = Nodes.getSize(); }
} }
} }
} public class main {
return minNode.key; public static void main(String[] args)
} {
public void displayHeap() BinomialHeap binHeap =
{ new BinomialHeap();
System.out.print("\nHeap : ");
displayHeap(Nodes);
System.out.println("\n");
}
BINOMIAL
HEAP
binHeap.insert(12); binHeap.delete(15);
binHeap.insert(8); binHeap.delete(8);
binHeap.insert(5); System.out.println("Size
binHeap.insert(15); of the binomial heap is "+
binHeap.insert(7); binHeap.getSize());
binHeap.insert(2); binHeap.displayHeap();
binHeap.insert(9); binHeap.makeEmpty();
System.out.println("Size
of the binomial heap is "+ System.out.println(binHeap.isEmpty());
binHeap.getSize()); }
binHeap.displayHeap(); }
INTERVIEW QUESTION
1.What is Binomial Heap?
Answer: A binomial heap is a type of data structure used to
maintain a collection of elements, typically with a focus on
efficient priority queue operations. It is a collection of
binomial trees where each tree follows the min-heap property.
INTERVIEW QUESTION
2. What are the key operations in a Binomial Heap?
Answer: The key operations in a binomial heap are:
insert(H, k): Inserts a key 'k' into the binomial heap
'H'.
getMin(H): Finds the minimum key in the binomial heap
'H'.
extractMin(H): Removes and returns the minimum key from
the binomial heap 'H'.
delete(H): Deletes a specific element from the binomial
heap.
decreaseKey(H): Decreases the key of a specific element
INTERVIEW QUESTION
3. How does the insert operation work in a Binomial Heap?
Answer: The insert operation in a binomial heap creates a new
binomial heap with a single key 'k' and then uses the union
operation to merge it with the existing binomial heap.
INTERVIEW QUESTION
4. What is the purpose of the union operation in
a Binomial Heap?
Answer: The union operation is used to combine two binomial
heaps into one. It involves merging the two heaps in non-
decreasing order of their degrees and then combining binomial
trees of the same degree to ensure that there is at most one
binomial tree of any order in the heap.
INTERVIEW QUESTION
5. How does the decreaseKey operation work in
a Binomial Heap?
Answer: The decreaseKey operation compares the decreased key
with its parent and swaps keys recursively until it reaches a
node whose parent has a smaller key or the root node.
INTERVIEW QUESTION
6. What is the Min Heap property, and how does it
apply to a Binomial Heap?
Answer: The Min Heap property states that the key of a node
is greater than or equal to the key of its parent. In a
binomial heap, each binomial tree follows the Min Heap
property, and the root of the entire heap is the minimum key
among all the trees.
INTERVIEW QUESTION
7. Explain the process of extracting the
minimum key in a Binomial Heap.
Answer: To extract the minimum key in a binomial heap, we
first call the getMin operation to find the minimum key
binomial tree. Then, we remove the node with the minimum key
and create a new binomial heap by connecting all the subtrees
of the removed node. Finally, we call the union operation on
the original heap and the newly created heap.
INTERVIEW QUESTION
8. What are the properties of a Binomial Heap?
Answer: The properties of a binomial heap are:
Each binomial tree in the heap is heap-ordered (follows the
Min Heap property).
There is at most one binomial tree with a given degree in the
heap.
INTERVIEW QUESTION
9. Can you compare a Binomial Heap to a Binary Heap?
Answer: Binomial heaps have certain advantages over binary
heaps, such as faster union and decrease key operations. They
can be used for priority queue operations efficiently.
However, binary heaps are easier to implement and have better
average time complexity for some operations.
https://learn.codemithra.com
THANK
YOU
95095