It contains a collection of binomial trees. Before diving further into the binomial heap, let us first try to understand what a binomial tree is. The recursive definition is given below. The binomial tree of order 0 i. Figure 1 illustrates the recursive definition.
|Published (Last):||10 December 2008|
|PDF File Size:||14.50 Mb|
|ePub File Size:||6.98 Mb|
|Price:||Free* [*Free Regsitration Required]|
It contains a collection of binomial trees. Before diving further into the binomial heap, let us first try to understand what a binomial tree is. The recursive definition is given below. The binomial tree of order 0 i.
Figure 1 illustrates the recursive definition. The binomial tree of order 1 is two binomial of trees of order 0 linked together. Similarly, the binomial tree of order 2 is two binomial trees of order 1 linked together and so on. Figure 2 shows the binomial trees of order 0, 1, 2, and 3.
Figure 3 shows a binomial tree of order 4. Properties of Binomial tree Following are the properties of a binomial tree. For the binomial tree of degree k i. The height of the tree is k.
Min-heap-ordered or Max-heap ordered binomial tree A min-heap-ordered binomial tree is a binomial tree that obeys the min-heap property i. Similarly, a max-heap-ordered binomial tree is a binomial tree that obeys the max-heap property i. Figure 4 shows an example of these binomial trees.
Each binomial tree in H must be min-heap-ordered or max-heap-ordered binomial tree. The first property tells us that the root of a min-heap-ordered tree contains the smallest key in the tree. Figure 5 shows an example of a binomial heap consisting of three binomial trees of degree 0, 1 and 3. Question 2: Find all the binomial trees in a binomial heap of 10 nodes. Solution: Do yourself.
Each node in a binomial heap has 5 fields as follows. In case of a root node, the sibling points to the root of another tree in the right. Figure 6 shows a representation of a node in a binomial heap. Figure 7 shows the representation of a binomial tree given in Figure 5.
Operations In this section, all the operations in a binomial heap are described in detail. Creating a new heap This is the easiest operation.
We simply point the head of the tree to null. The head always points to the leftmost tree of the heap. Finding the minimum The root of a min-heap-ordered binomial tree contains the node with minimum data in it. In figure 8, the binomial heap has 3 binomial trees. The minimum value of the binomial heap is the smallest root among these three trees, which is 5. Merging two binomial heaps We repeatedly merge two binomial trees of the same degree until all the binomial trees have a unique degree.
To merge two binomial trees of the same degree, we do the following. Compare the roots of two trees x and y. Find the smallest root. Let x is the tree with the smallest root. Figure 9 illustrates this process. This operation clearly takes constant time. Once we know how to merge two binomial trees with the same degree, we can merge two binomial heaps using the following steps.
Merge two binomial heaps without worrying about trees with the same degree. That means put the trees in the increasing order of degree. Starting from the head, repeatedly merge trees with the same degree until all the trees in the heap have a unique degree. Let me clarify these points with the help of examples.
Consider two binomial heaps given in figure In the first step, we simply merge them without taking care of repeated degrees. After this merge, the trees in the heap must be arranged in an increasing order of degree. Figure 11 is the result of this process. Next, we start from the head and move towards the right direction. As soon as we see two trees with the same degree, we merge them. In figure 11, there are two trees of degree 2. Figure 12 shows the result of merging these two trees. We repeat the similar process and merge two trees of degree 3.
The final result of merging two binomial heaps is given in figure We traverse the roots of these trees constant number of times. Insert a node Inserting a node x into a heap H is a three steps process as follows. It has a head pointer pointing to null. Create a new node x with all the necessary fields. Point the head of the heap to x. Figure 14 illustrates this process with an example. Delete Min or Extract Min The steps for deleting the node from a heap H with the smallest key is given below.
Search through the roots of binomial trees and find the root with the smallest key and call it x. Remove the x from the tree. Figure 15 illustrates this process with an example. Because of its length, I find it inappropriate to paste here. It is available on GitHub.
Binomial Heaps (With C++ Implementation)
Because the number of children for each node is variable, it does not work well for each node to have separate links to each of its children, as would be common in a binary tree ; instead, it is possible to implement this tree using links from each node to its highest-order child in the tree, and to its sibling of the next smaller order than it. These sibling pointers can be interpreted as the next pointers in a linked list of the children of each node, but with the opposite order from the linked list of roots: from largest to smallest order, rather than vice versa. This representation allows two trees of the same order to be linked together, making a tree of the next larger order, in constant time. The result is a tree of order 3. The operation of merging two heaps is used as a subroutine in most other operations. A basic subroutine within this procedure merges pairs of binomial trees of the same order. This may be done by comparing the keys at the roots of the two trees the smallest keys in both trees.