Binomial Heap (Data Structures)
Binomial Heap (Data Structures)
Home Data Structure C C++ C# Java SQL HTML CSS JavaScript Ajax
⇧ SCROLL TO TOP
https://www.javatpoint.com/binomial-heap 1/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Binomial Heap
In this article, we will discuss the binomial heap. Before start discussing the topic, we should first
understand some basic terms such as heap, min-heap, max-heap, and binomial tree.
What is a heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node can have utmost
two children. There are two types of heap that are defined as follows -
Min Heap: The value of the parent node should be less than or equal to either of its children.
Mathematically, the min-heap can be defined as -
A[Parent(i)] <= A[i]
Max Heap: The value of the parent node is greater than or equal to its children.
Mathematically, the max-heap can be defined as -
A[Parent(i)] >= A[i]
A Binomial tree Bk is an ordered tree defined recursively, where k is defined as the order of the
binomial tree.
If the binomial tree is represented as B0, then the tree consists of a single node.
In general terms, Bk consists of two binomial trees, i.e., Bk-1 and Bk-1 that are linked together
in which one tree becomes the left subtree of another binomial tree.
If B0, where k is 0, there would exist only one node in the tree.
https://www.javatpoint.com/binomial-heap 2/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
If B1, where k is 1. Therefore, there would be two binomial trees of B0 in which one B0 becomes the
left subtree of another B0.
If B2, where k is 2. Therefore, there would be two binomial trees of B1 in which one B1 becomes the
left subtree of another B1.
If B3, where k is 3. Therefore, there would be two binomial trees of B2 in which one B2 becomes the
left subtree of another B2.
https://www.javatpoint.com/binomial-heap 3/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
A binomial heap can be defined as the collection of binomial trees that satisfies the heap properties,
i.e., min-heap. The min-heap is a heap in which each node has a value lesser than the value of its
child nodes. Mainly, Binomial heap is used to implement a priority queue. It is an extension of binary
heap that gives faster merge or union operations along with other operations provided by binary
heap.
Every binomial tree in the heap must follow the min-heap property, i.e., the key of a node is
greater than or equal to the key of its parent.
For any non-negative integer k, there should be atleast one binomial tree in a heap where
root has degree k.
The first property of the heap ensures that the min-heap property is hold throughout the heap.
Whereas the second property listed above ensures that a binary tree with n nodes should have at
most 1 + log2 n binomial trees, here log2 is the binary logarithm.
https://www.javatpoint.com/binomial-heap 4/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
We can understand the properties listed above with the help of an example -
The above figure has three binomial trees, i.e., B0, B2, and B3. The above all three binomial trees
satisfy the min heap's property as all the nodes have a smaller value than the child nodes.
The above figure also satisfies the second property of the binomial heap. For example, if we consider
the value of k as 3, we can observe in the above figure that the binomial tree of degree 3 exists in a
heap.
Suppose we want to create the binomial heap of 'n' nodes that can be simply defined by the binary
number of 'n'. For example: if we want to create the binomial heap of 13 nodes; the binary form of
13 is 1101, so if we start the numbering from the rightmost digit, then we can observe that 1 is
available at the 0, 2, and 3 positions; therefore, the binomial heap with 13 nodes will have B0, B2, and
B3 binomial trees.
https://www.javatpoint.com/binomial-heap 5/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
We can use another example to understand it more clearly, suppose we have to create the binomial
heap with 9 nodes. The binary representation of 9 is 1001. So, in the binary representation of 9, digit
1 is occurring at 0 and 3 positions, therefore, the binomial heap will contain the binomial trees of 0
and 3 degrees.
Inserting a node
Decreasing a key
Deleting a node
When we create a new binomial heap, it simply takes O(1) time because creating a heap will create
the head of the heap in which no elements are attached.
https://www.javatpoint.com/binomial-heap 6/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
function merge(a,b)
if a.root.key ? b.root.key
return a.add(b)
else
return b.add(a)
To perform the union of two binomial heaps, we have to consider the below cases -
Case 1: If degree[x] is not equal to degree[next x], then move pointer ahead.
and key[x] < key[next x] then remove [next x] from root and attached to x.
and key[x] > key[next x] then remove x from root and attached to [next x].
Now, let's understand the merging or union of two binomial heaps with the help of an example.
Consider two binomial heaps -
https://www.javatpoint.com/binomial-heap 7/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
We can see that there are two binomial heaps, so, first, we have to combine both heaps. To combine
the heaps, first, we need to arrange their binomial trees in increasing order.
https://www.javatpoint.com/binomial-heap 8/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
In the above heap first, the pointer x points to the node 12 with degree B0, and the pointer next[x]
points the node 18 with degree B0. Node 7 with degree B1 is the sibling of 18, therefore, it is
represented as sibling[next[x]].
Now, first apply Case1 that says 'if degree[x] ≠ degree[next x] then move pointer ahead' but in
the above example, the degree[x] = degree[next[x]], so this case is not valid.
Now, apply Case2 that says 'if degree[x] = degree[next x] = degree[sibling(next x)] then Move
pointer ahead'. So, this case is also not applied in the above heap.
Now, apply Case3 that says ' If degree[x] = degree[next x] ≠ degree[sibling[next x]] and key[x]
< key[next x] then remove [next x] from root and attached to x'. We will apply this case because
the above heap follows the conditions of case 3 -
degree[x] = degree[next x] ≠ degree[sibling[next x]] {as, B0 = B0¬ ≠ B1} and key[x] < key[next x] {as
12 < 18}.
Now we will reapply the cases in the above binomial heap. First, we will apply case 1. Since x is
pointing to node 12 and next[x] is pointing to node 7, the degree of x is equal to the degree of next
x; therefore, case 1 is not valid.
Here, case 2 is valid as the degree of x, next[x], and sibling[next[x]] is equal. So, according to the
case, we have to move the pointer ahead.
https://www.javatpoint.com/binomial-heap 9/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Now, let's try to apply case 3, here, first condition of case3 is satisfied as degree[x] = degree[next[x]]
≠ degree[sibling[next[x]]], but second condition (key[x] < key[next x]) of case 3 is not satisfied.
Now, let's try to apply case 4. So, first condition of case4 is satisfied and second condition (key[x] >
key[next x]) is also satisfied. Therefore, remove x from the root and attach it to [next[x]].
Now, the pointer x points to node 3, next[x] points to node 15, and sibling[next[x]] points to the
node 6. Since, the degree of x is equal to the degree of next[x] but not equal to the
degree[sibling[next[x]]], and the key value of x is less than the key value of next[x], so we have to
remove next[x] and attach it to x as shown below -
Now, x represents to the node 3, and next[x] points to node 6. Since, the degree of x and next[x] is
not equal, so case1 is valid. Therefore, move the pointer ahead. Now, the pointer x points the node
6.
The B4 is the last binomial tree in a heap, so it leads to the termination of the loop. The above tree is
the final tree after the union of two binomial heaps.
https://www.javatpoint.com/binomial-heap 10/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
In the above heap, there are three binomial trees of degrees 0, 1, and 2 are given where B0 is
attached to the head of the heap.
First, we have to combine both of the heaps. As both node 12 and node 15 are of degree 0, so node
15 is attached to node 12 as shown below -
https://www.javatpoint.com/binomial-heap 11/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Now, assign x to B0 with value 12, next(x) to B0 with value 15, and assign sibling(next(x)) to B1 with
value 7. As the degree of x and next(x) is equal. The key value of x is smaller than the key value of
next(x), so next(x) is removed and attached to the x. It is shown in the below image -
Now, x points to node 12 with degree B1, next(x) to node 7 with degree B1, and sibling(next(x))
points to node 15 with degree B2. The degree of x is equal to the degree of next(x) but not equal to
the degree of sibling(next(x)). The key value of x is greater than the key value of next(x); therefore, x
is removed and attached to the next(x) as shown in the below image -
https://www.javatpoint.com/binomial-heap 12/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Now, x points to node 7, and next(x) points to node 15. The degree of both x and next(x) is B2, and
the key value of x is less than the key value of next(x), so next(x) will be removed and attached to x
as shown in the below image -
Now, the degree of the above heap is B3, and it is the final binomial heap after inserting node 15.
https://www.javatpoint.com/binomial-heap 13/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Now, compare the key values of the root node of the binomial trees in the above heap. So, 12, 7,
and 15 are the key values of the root node in the above heap in which 7 is minimum; therefore,
remove node 7 from the tree as shown in the below image -
Now, the degree of node 12 and node 25 is B0, and the degree of node 15 is B2. Pointer x points to
the node 12, next(x) points to the node 25, and sibling(next(x)) points to the node 15. Since the
degree of x is equal to the degree of next(x) but not equal to the degree of sibling(next(x)). Value of
pointer x is less than the pointer next(x), so node 25 will be removed and attached to node 12 as
shown in the below image -
https://www.javatpoint.com/binomial-heap 14/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Now, the degree of node 12 is changed to B1. The above heap is the final heap after extracting the
minimum key.
Decreasing a key
Now, let's move forward to another operation to be performed on binomial heap. Once the value of
the key is decreased, it might be smaller than its parent's key that results in the violation of min-
heap property. If such case occurs after decreasing the key, then exchange the element with its
parent, grandparent, and so on until the min-heap property is satisfied.
Let's understand the process of decreasing a key in a binomial heap using an example. Consider a
heap given below -
Decrease the key 45 by 7 of the above heap. After decreasing 45 by 7, the heap will be -
https://www.javatpoint.com/binomial-heap 15/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
After decreasing the key, the min-heap property of the above heap is violated. Now, compare 7 wits
its parent 30, as it is lesser than the parent, swap 7 with 30, and after swapping, the heap will be -
Again compare the element 7 with its parent 8, again it is lesser than the parent, so swap the
element 7 with its parent 8, after swapping the heap will be -
Now, the min-heap property of the above heap is satisfied. So, the above heap is the final heap after
decreasing a key.
https://www.javatpoint.com/binomial-heap 16/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
First, replace the node with negative infinity (or -∞) as shown below -
Now, swap the negative infinity with its root node in order to maintain the min-heap property.
https://www.javatpoint.com/binomial-heap 17/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Now, again swap the negative infinity with its root node.
The next step is to extract the minimum key from the heap. Since the minimum key in the above
heap is -infinity so we will extract this key, and the heap would be:
https://www.javatpoint.com/binomial-heap 18/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
The above is the final heap after deleting the node 41.
Time Complexity
https://www.javatpoint.com/binomial-heap 19/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
The time complexity of finding the minimum element from the heap can be reduced to O(1). It can
be done by using an additional pointer to the minimum element.
Space Complexity
Conclusion
So, that's all about the article. Here, we have discussed binomial heap along with its properties and
complexity. We have also discussed the operations performed on binomial heap with the help of an
example to make the topic easier to understand. Hope the article will be helpful and interesting to
you.
← Prev Next →
https://www.javatpoint.com/binomial-heap 20/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Feedback
https://www.javatpoint.com/binomial-heap 21/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
Preparation
Company
Interview
Questions
Company Questions
Trending Technologies
https://www.javatpoint.com/binomial-heap 22/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
B.Tech / MCA
https://www.javatpoint.com/binomial-heap 23/24
3/12/23, 4:17 PM Binomial Heap (Data Structures) - javatpoint
https://www.javatpoint.com/binomial-heap 24/24