Binomial Heap

Distribute Education like Computer Geek

Binomial Heap is a group of binomial trees

.

Binomial Tree

A binomial tree is an ordered tree, and it is denoted by Bk where k is the degree of the root.

It has some properties which is –

  1. Bk tree has 2k nodes.
  2. Degree of root is k. it means the root have k children. All the other nodes who are present in the tree have a degree less than k.
  3. Height of the tree is also k.
  4. There are exactly kCi nodes at depth i, where kCi = (k!) / (k-i)! i!
  5. The binomial tree Bk has two Bk-1 trees. Alternatively, we can say that 2k-1 + 2k-1 trees will form a 2k tree.
binomial tree merging
Merging binomial tree

The binomial tree examples are

B0

Binomial Tree with 0 depth

B1

Binomial Tree with 1 depth

B2

Binomial Tree with 2 depth

B3

Binomial Tree with 3 depth

 

Binomial Heap

A binomial heap is constructed as a recursively defined set of binomial trees. It was introduced by Jean Vuillemin in 1978.

The binomial trees must satisfy the certain properties.

The properties are –

  1. The min heap rule is maintained by each binomial tree.
  2. All binomial trees are connected through by a linked list.

binomial Heap

In the above diagram, there is a binomial heap. All the binomial trees are connected through a linked list, and every heap obeys the min-heap property.

We can quickly identify the binomial trees that will be present in a heap if we know how many nodes it contains by looking at the binary representation.

Number of nodes = 1 in B0 + 2 in B1 + 8 in B3 = 11

The binary representation of 11 is 1011

So, the binomial heap contains the binomial trees B0, B1 and B3.

So, 1*23 + 0*22 + 1*21 + 1*20 = 11

 

Operations in Binomial Heap

  1. Make a heap
  2. Insertion
  3. Merging two heap
  4. Find the minimum key
  5. Extracting the minimum key
  6. Decreasing a key
  7. Deleting a key
  1. Make a heap

Place a head and make an arrangement of linked lists so that every node after inserting will follow the links.

head

Because there is no node in the binomial tree, Head[link] = NIL.

This is a very simple process with a time complexity of ÆŸ(1).

.

  1. Insertion

If we wish to insert a node to create a binomial heap, we must follow several rules that are specific to binomial heaps.

Rule 1 – There is a head variable at the start, and all the binomial trees are connected by the root through a linked list.

Rule 2 – Each inserted node will form a separate binomial tree, and we will consider it the root of the tree that is connected to the linked list.

Rule 3 – No two binomial trees can contain the same k. They have to follow the merge operation that is discussed later.

binomial Heap insertion

Suppose we want to insert 12 in the binomial heap. Then we have to insert 12 as a separate binomial tree, B0.

insert 12 in binomial heap

Time Complexity of inserting is O(log n).

.

  1. Merging two Heaps

By doing some operation (inserting or deleting) in the binomial heap, if the result is that there are two binomial trees that have the same degree k, then a merge operation is applied to the two trees.

insert 30

In the above diagram, there are two binomial trees whose degree is the same, 30 and 12.

Choose the smaller value as the new root and the larger value as the smaller value’s child.

modification in heap

There are now two binomial trees with the same degree.

Merge them with the smaller value of the root to create a new root, and the larger value of the root becomes a child of the smaller value.

Heap modification

Time complexity of merging two binomial heaps is O(log n).

.

  1. Find the minimum key

Every binomial heap has a min-heap property. As a result, the root has the smallest element. To discover the minimum key, we must search all of the roots. 6 has the least value of the root nodes 17, 6, and 8.

find minimum in heap

Time complexity of finding the minimum key is O(log n).

.

  1. Extracting the minimum key

It means removing the minimum key of the binomial heap. In the diagram for finding the minimum key, node 6 is identified as the minimum key.

After extracting or removing node 6, the orphaned subtrees that arise are added back to the heap as independent trees.

Extracting in heap

This is the result of removing node 6. Node 12 and node 20 become root.

This binomial heap is not perfect, as node 17 and node 20 belong to the same degree. So, a merging operation is performed to get it in perfect shape. After that, there are two trees of the same degree 1. Therefore, a second merging operation is required, and the resultant binomial heap is

binomial tree

Time complexity of extracting the minimum key is O(log n).

.

  1. Decreasing a key

The min-heap property may be lost if an element’s key decreases to the point where it is smaller than the value of its parent. Change the element’s parent, possibly its grandparent, and so on until the min-heap property is no longer violated.

decreasing in binomial heap

If 28 is decreased by 9,

decrease 28 by 9 in binomial heap

Then node 9 is replaced by its grandparent node 10 to not violate the min-heap property. Its parent node 21 replaces node 9, and grandparent node 10 replaces node 21.

binomial tree

Time complexity of decreasing a key is O(log n).

.

  1. Deleting a key

In the other data structures, deleting a key replaces it with its predecessor or successor, but in the binomial heap, it is not so simple. It deletes the node by setting its value to (-∞) negative infinity.

Suppose if we want to delete node 20, first replace it with (-∞).

delete 20 in binomial heap

To satisfy the min-heap property, (-∞) is taken to the root.

Delete 20 in binomial tree

Remove (-∞). The orphaned subtrees that arise are added back to the heap as independent trees.

binomial tree

Time complexity of deleting a key or node is O(log n).

Space complexity is O(n).

.

Application

  1. A data structure that functions as a priority queue is the binomial heap.
  2. Memory management systems make use of heaps to effectively create and deallocate memory blocks of various sizes.
  3. Binomial heaps can be used in parallel computing applications, where numerous threads or processes must access and alter shared data structures simultaneously.

Test Yourself

Q1 – Explain the properties of a binomial tree.

Binomial tree is an ordered tree and it is denoted by Bk where k is the degree of root.

It has some properties which is –

  1. Bk tree has 2k nodes.
  2. Degree of root is k. it means the root have k children. All the other nodes who are present in the tree have a degree less than k.
  3. Height of the tree is also k.
  4. There are exactly kCi nodes at depth i, where                                                      kCi = (k!) / (k-i)! i!
  1. The binomial tree Bk has two Bk-1 trees. Alternatively, we can say that 2k-1 + 2k-1 trees will form a 2k tree.
Q2 – How is the binary representation of the number of nodes used to predict the binomial trees in a binomial heap?

The binary representation of the number of nodes in a binomial heap helps to identify the binomial trees it contains. For each ‘1’ in the binary representation, a corresponding binomial tree of that degree is present in the heap. For example, if the binary representation of the number of nodes is 1011, it means the binomial heap contains binomial trees B0, B1, and B3.

Q3 – Describe the process of inserting a new node in a binomial heap.

When inserting a new node in a binomial heap, the node is first considered as a separate binomial tree and connected to the linked list of trees. If there are two binomial trees with the same degree after the insertion, a merging operation is performed to maintain the heap’s properties. The insertion process has a time complexity of O(log n).

Q4 – How is the minimum key found in a binomial heap, and what is the time complexity of this operation?

The minimum key in a binomial heap is always located at the root of the tree. To find the minimum key, one needs to search all the roots of the binomial trees. 

The time complexity of finding the minimum key is O(log n).

Q5 – Explain the process of decreasing a key in a binomial heap and the steps taken to maintain the heap’s properties.

When a key is decreased in a binomial heap, it may violate the min-heap property. To restore this property, the element is replaced with its parent, and this process continues with its grandparent and so on until the min-heap property is satisfied. The time complexity of decreasing a key is O(log n).

Q6 – Describe how nodes are merged in a binomial heap and the purpose of this operation.

Nodes in a binomial heap are merged when they have the same degree, i.e., the same number of children. The merging operation is performed to maintain the heap’s properties. The node with the smaller value becomes the new root, and the node with the larger value becomes a child of the smaller node. This process ensures that no two binomial trees in the heap have the same degree.

binomial tree merging
Merging binomial tree
Q7 – What is the purpose of deleting a key in a binomial heap, and how is it achieved?

The purpose of deleting a key in a binomial heap is to remove the node with the specified key from the heap. To achieve this, the value of the node is replaced with (-∞) negative infinity, and the heap’s min-heap property is restored through the appropriate merging and restructuring of nodes.

Q8 – Explain the application of a binomial heap as a data structure.

One of the main applications of a binomial heap is as a priority queue. It provides efficient operations for insertion, deletion, finding the minimum key, and extracting the minimum key. Its properties make it suitable for various applications where maintaining the minimum element is crucial.

Q9 – How does a binomial heap ensure the min-heap property?

Each binomial tree in a binomial heap maintains the min-heap property, where the value of each node is less than or equal to the value of its parent. This ensures that the root of each binomial tree contains the smallest element in that tree, allowing the entire heap to maintain the min-heap property.

Q10 – What is the space complexity of a binomial heap, and why is it advantageous for certain applications?

The space complexity of a binomial heap is O(n), where n is the number of nodes in the heap. This space complexity is advantageous for certain applications, such as priority queues and efficient management of large datasets, where the heap can handle a large number of elements with relatively low memory overhead.

Q11 – How many nodes does a binomial tree B3 have?
  1. 3
  2. 6
  3. 8
  4. 15

Ans – (3)

Explanation – A binomial tree B3 has 2^3 = 8 nodes.

Q12 – What is the primary application of a binomial heap?
  1. Sorting elements in ascending order
  2. Priority queue
  3. Storing elements in a linked list
  4. Binary search tree

Ans – (2)

Explanation – The primary application of a binomial heap is as a priority queue.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Â