Binomial Heap (Data Structures) - javatpoint
Binomial Heap (Data Structures) - javatpoint
Advertisement
DS Tutorial
Advertisement
DS Tutorial
DS Introduction
DS Algorithm
Asymptotic Analysis
DS Pointer
DS Structure
DS Array
DS Array
2D Array
DS Linked List
Linked List
Skip list in DS
https://www.tpointtech.com/binomial-heap 1/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
DS Stack
Advertisement
DS Stack
Array Implementation
← prev next →
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.
https://www.tpointtech.com/binomial-heap 2/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
If the binomial tree is represented as B0, then the tree consists of a single
node. Advertisement
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.
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.
https://www.tpointtech.com/binomial-heap 3/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
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.tpointtech.com/binomial-heap 4/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
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.
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.
https://www.tpointtech.com/binomial-heap 5/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
The above figure also satisfies the second property of the binomial heap. For example, if
we consider theAdvertisement
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.
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
https://www.tpointtech.com/binomial-heap 6/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
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.
Advertisement
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
Advertisement
example. Consider two binomial heaps -
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.tpointtech.com/binomial-heap 8/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
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
Advertisement
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.
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 -
https://www.tpointtech.com/binomial-heap 10/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
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.
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.
https://www.tpointtech.com/binomial-heap 11/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
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 -
Advertisement
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.tpointtech.com/binomial-heap 12/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
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.tpointtech.com/binomial-heap 13/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
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 -
https://www.tpointtech.com/binomial-heap 14/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
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
Advertisement
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 -
Advertisement
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.
https://www.tpointtech.com/binomial-heap 15/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Let's understand the process of decreasing a key in a binomial heap using an example.
Consider a heapAdvertisement
given below -
Advertisement
Decrease the key 45 by 7 of the above heap. After decreasing 45 by 7, the heap will be -
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 -
https://www.tpointtech.com/binomial-heap 16/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
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 -
Advertisement
Advertisement
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.tpointtech.com/binomial-heap 17/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
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.
Now, again swap the negative infinity with its root node.
https://www.tpointtech.com/binomial-heap 18/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
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.tpointtech.com/binomial-heap 19/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
The above is the final heap after deleting the node 41.
Time Complexity
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
https://www.tpointtech.com/binomial-heap 20/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
So, that's all about the article. Here, we have discussed binomial heap along with its
properties andAdvertisement
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.
Advertisement
← prev next →
https://www.tpointtech.com/binomial-heap 21/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
Related Posts
https://www.tpointtech.com/binomial-heap 22/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
5 min read
Advertisement
4 min read
Advertisement
Reversing a Queue
Reversing a queue is the process of making the first element as last and the last
element as first. By reversing a queue, we alter the sequence in which its elements will
be processed or accessed. This can be done by storing the temporary components in...
5 min read
4 min read
https://www.tpointtech.com/binomial-heap 23/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Maximum XOR
Given an array of positive integers, find 2 elements such that their xor: a ^ b is maximum.
Let us take an example to know what to be implemented. If the array elements are: 12,
15, 9. We need to find out the maximum xor value possible from these...
3 min read
Python Java
https://www.tpointtech.com/binomial-heap 24/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Javascript HTML
Database PHP
Advertisement
C++ React
B.Tech / MCA
Data
DBMS
Structures
Operating
DAA
System
https://www.tpointtech.com/binomial-heap 25/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Computer Compiler
Network
Advertisement Design
Computer Discrete
Organization Mathematics
Advertisement
Ethical Computer
Hacking Graphics
Web Software
Technology Engineering
Cyber
Automata
Security
C
C++
Programming
https://www.tpointtech.com/binomial-heap 26/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Java .Net
Advertisement
Python Programs
Advertisement
Control Data
System Warehouse
Preparation
Aptitude Reasoning
Verbal Interview
Ability Questions
Company
Questions
https://www.tpointtech.com/binomial-heap 27/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 28/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 29/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 30/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 31/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 32/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 33/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 34/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 35/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 36/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 37/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 38/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 39/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 40/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 41/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 42/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 43/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 44/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 45/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 46/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 47/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 48/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 49/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 50/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 51/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 52/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
Advertisement
https://www.tpointtech.com/binomial-heap 53/54
2/14/25, 11:31 AM Binomial Heap (Data Structures) - javatpoint
Advertisement
https://www.tpointtech.com/binomial-heap 54/54