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.
Case 2: if degree[x] = degree[next x] = degree[sibling(next x)] then,Move the pointer ahead.
Case 3: If degree[x] = degree[next x] but not equal to degree[sibling[next x]]
and key[x] < key[next x] then remove [next x] from root and attached to x.
Case 4: If degree[x] = degree[next x] but not equal to degree[sibling[next x]]
and key[x] > key[next x] then remove x from root and attached to [next x].
Example. Consider two binomial heaps -
Insert an element in the heap
The following procedure inserts node x into binomial heap H, assuming of course that node 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')
The procedure simply makes a one-node binomial heap H' in O(1) time and unites it with the n-node binomial heap H in O(1g n) time. The call to BINOMIAL-HEAP-UNION takes care of freeing the temporary binomial heap H'.
Inserting an element in the heap can be done by simply creating a new heap only with the element to be inserted, and then merging it with the original heap. Due to the merging, the single insertion in a heap takes O(logn) time. Now, let's understand the process of inserting a new node in a heap using an example.
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.
Suppose we have to insert node 15 in the above 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 -
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 -
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.
Extracting 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
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
4 H = BINOMIAL-HEAP-UNION(H,H')
5 return x
It means that we have to remove an element with the minimum key value. As we know, in min-heap, the root element contains the minimum key value. So, we have to compare the key value of the root node of all the binomial trees. Let's see an example of extracting the minimum key from the heap.
Suppose the heap is -
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 -
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
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]
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 -
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.
Deleting a node from the heap
To delete a node from the heap, first, we have to decrease its key to negative infinity (or -∞) and then delete the minimum in the heap. Now we will see how to delete a node with the help of an example. Consider the below heap, and suppose we have to delete the node 41 from the heap -
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.
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:
The above is the final heap after deleting the node 41.
Complexity of binomial heap
Now, let's see the time complexity of the operations performed on binomial heap.
Time Complexity
Operations
|
Time complexity
|
Finding the minimum key
|
O(log n)
|
Inserting a node
|
O(log n)
|
Extracting minimum key
|
O(log n)
|
Decreasing a key
|
O(log n)
|
Union or merging
|
O(log n)
|
Deleting a node
|
O(log n)
|
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
The space complexity of a binomial heap with 'n' elements is O(n).
0 Comments