Binomial HeapAs we have already discussed about the heap data structure which is of two types, i.e., min heap and max heap. A binomial heap can be defined as the collection of binomial tree 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. To understand the binomial heap, we first understand about the binomial tree. What is Binomial tree?A Binomial tree is a tree in which B_{k} is an ordered tree defined recursively, where k is defined as the order of the binomial tree.
Let's understand through examples. If B_{0}, where k is 0, means that there would exist only one node in the tree shown as below: If B_{1}, where k is 1, means k1 equal to 0. Therefore, there would be two binomial trees of B_{0} in which one B_{0} becomes the left subtree of another B_{0}. If B_{2}, where k is 2, means k1 equal to 1. Therefore, there would be two binomial trees of B_{1 }in which one B_{1} becomes the left subtree of another B_{1}. If B_{3 }, where k is 3, means k1 equal to 2. Therefore, there would be two binomial trees of B_{3} in which one B_{3 }becomes the left subtree of another B_{3}.] Properties of Binomial tree
Binomial Heap A binomial heap is a collection of binomial trees that satisfies the properties of a min heap. The following are the two properties of the binomial heap:
Let's understand the above two properties through an example. The above figure has three binomial trees, i.e., B_{0}, B_{2}, B_{3}. 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, then we can observe in the above figure that the binomial tree of degree 3 exists in the heap. Representation of Binomial heap node The above figure shows the representation of the binomial heap node in the memory. The first block in the memory contains the pointer that stores the address of the parent of the node. The second block stores the key value of the node. The third block contains the degree of the node. The fourth block is divided into two parts, i.e., left child and sibling. The left child contains the address of the left child of a node, whereas the sibling contains the address of the sibling. Important point
Let's consider another example. If we want to create the binomial heap of 9 nodes. The binary form of 9 is 1001. As we can observe in the binary number that 1 digit is available at 0 and 3 position; therefore, the binomial heap will contain the binomial trees of 0 and 3 degree. Operations on Binomial Heap
Union of two Binomial HeapTo perform the union to two binomial heap, we will use the following 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 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]. Let's understand the union of two binomial heaps through an example. As we can observe in the above figure that there are two binomial heaps. First, we combine these two binomial heaps. To combine these two binomial heaps, we need to arrange them in the increasing order of binomial trees shown as below:
Now we will apply case 2. The case 2 says that 'if degree[x] = degree[next x] = degree[sibling(next x)] then Move pointer ahead'. In the above example, the degree of x is same as the degree of the next x but not equal to the degree of a sibling of next x; therefore, this case is not valid. Now we will apply case 3. The case 3 says that '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'. In the above example, the degree of x is equal to the degree of next x but equal to the degree of a sibling of next x, and the key value of x, i.e., 12, is less than the value of next x, i.e., 18; therefore, this case is valid. So, we have to remove the next x, i.e., 18, from the root and attach it to the x, i.e., 12, shown as below: As we can observe in the above binomial heap that node 18 is attached to the node 12.
Now we will apply case 2. Since the degree of x is equal to the degree of next x and also equal to the degree of sibling of next x; therefore, this case is valid. We will move the pointer ahead shown as below: As we can observe in the above figure that 'x' points to the binomial tree having root node 7, next(x) points to the binomial tree having root node 3 while prev(x) points to the binomial tree having root node 12. The sibling(next(x)) points to the binomial tree having root node 15.
As we can observe in the above figure that x becomes the left child of next(x). The pointer also gets updated. Now, x will point to the binomial tree having root node 3, and degree is also changed to 2. The next(x) points to the binomial tree having root node as 15, and the sibling(next(x)) will point to the binomial tree having root node as 6.
As we can observe in the above figure that next(x) becomes the left child of x, and the degree of x also gets changed to B_{3. }The pointer next(x) also gets updated, and it now points to the binomial tree having root node 6. The degree of x is 3, and the degree of next(x) is 4. Since the degrees of x and next(x) are not equal, so case 1 is valid. We will move the pointer ahead, and now x points to node 6. The B_{4} 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. Inserting a new node in a Binomial heap Now we will see how to insert a new node in a heap. Consider the below heap, and we want to insert a node 15 in a heap. In the above heap, there are three binomial trees of degree B_{0}, B_{1}, and B_{2}, where B_{0 }is attached to the head of the heap. Let's assume that node 15 is attached to the head of the heap shown as below: First, we will combine these two heaps. As the degree of node 12 and node 15 is B_{0} so node 15 is attached to the node 12 shown in the above figure. Now we assign 'x' to the B_{0} having key value 12, next(x) to the B_{0} having key value 15, and sibling(next(x)) to the B_{1} having key value 7. Since the degree of x is equal to the degree of next(x) but not equal to the degree of sibling of next(x) so either case 3 or case 4 will be applied to the heap. The key value of x is greater than the key value of next(x); therefore, next(x) would be removed and attached it to the x. Now, we will reapply the cases in the above heap. Now, x points to the node 12 having degree B_{1}, next(x) points to the node 7 having degree B_{1 }and sibling(next(x)) points to the node 15 having degree B_{2}. Since the degree of x is same as the degree of next(x) but not equal to the degree of sibling of next(x), so either case 3 or case 4 is applied. The key value of x is greater than the key value of next(x); therefore, the x is removed and attached to the x shown as below: As we can observe in the above figure that when 'x' is attached to the next(x) then the degree of node 7 is changed to B_{2}. The pointers are also get updated. Now 'x' points to the node 7 and next(x) points to the node 15. Now we will reapply the cases in the above heap. Since the degree of x is equal to the degree of next(x) and the key value of x is less than the key value of next(x); therefore, next(x) would be removed from the heap and attached it to the x shown as below: As we can observe in the above heap that the degree of x gets changed to 3, and this is the final binomial heap after inserting the node 15. Extracting a minimum key Here extracting a minimum key means that we need to remove the element which has minimum key value. We already know that in min heap, the root element contains the minimum key value. Therefore, we have to compare the key value of root node of all the binomial trees. Consider the binomial heap which is given below: In the above heap, the key values of root node of all the binomial trees are 12, 7 and 15. The key value 7 is the minimum value so we will remove the node 7 from the tree shown as below: As we can observe in the above binomial heap that the degree of node 12 is B_{0}, degree of node 25 is B_{0}, and degree of node 15 is B_{2}. The 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)); therefore, either case 3 or case 4 will be applied to the above heap. The key value of x is less than the key value of next(x), so node 25 will be removed and attached to the node 12 shown as below: The degree of node 12 is also changed to 1. Decreasing a key Now we will see how to decrease a key with the help of an example. Consider the below heap, and we will decrease the key 45 by 7: After decreasing the key by 7, the heap would look like: Since the above heap does not satisfy the minheap property, element 7 will be compared with an element 30; since the element 7 is less than the element 30 so 7 will be swapped with 30 element shown as below: Now we will again compare element 7 with its root element, i.e., 8. Since element 7 is less than element 8 so element 7 will be swapped with an element 8 shown as below: Now, the above heap satisfies the property of the minheap. Deleting a node Now we will see how to delete a node with the help of an example. Consider the below heap, and we want to delete node 41: First, we replace node 41 with the smallest value, and the smallest value is infinity, shown as below: Now we will swap the infinity with the root node of the tree shown as below: The next step is to extract the minimum key. Since the minimum key in a heap is infinity so we will extract this key, and the heap would be:
Next TopicPostorder Traversal
