Binomial Heaps
Binomial Heaps
parent
Each node is represented key
by a structure like this degree
child sibling
Representing binomial heaps
Operations on binomial heaps
Finding the minimum key
Complexity: ?
Finding the minimum key
Complexity: Because
there are at most lg n + 1
roots to check, the
running time of
BINOMIAL-HEAP-
MINIMUM is O(lg n).
Uniting two binomial heaps
The following procedure links the Bk−1 tree rooted at node y
to the Bk−1 tree rooted at node z; that is, it makes z the
parent of y. Node z thus becomes the root of a Bk tree in O(1).time
y z y z
Link Bk-1
Bk-1 Bk-1
Bk-1
Union of two Binomial Heap
• BINOMIAL-HEAP-UNION(H1, H2) procedure unites binomial
heaps H1 and H2, after uniting it return the resulting heap.
• It destroys the representations of H1 and H2 in the process.
• Besides BINOMIAL-LINK, the procedure uses an auxiliary
procedure BINOMIALHEAP- MERGE that merges the root lists
of H1 and H2 into a single linked list that is sorted by degree
into monotonically increasing order.
• The BINOMIAL-HEAPMERGE procedure works like merging
procedure in merge Sort.
Union of two Binomial Heap
H1, H2 Union H1 H2
H1 = H2 =
First, simply merge the two root lists by root degree (like merge sort).
Remaining Problem: Can have two trees with the same root degree.
Union (Continued)
Union traverses the new root list like this:
prev-x x next-x
Note: We may temporarily create three trees with the same root
degree.
Analogy
head[H1] 12 7 15 head[H2] 18 3 6
25 28 33 37 10 44
8 29
41 48 31 17
23 22
30
24 50
45 32
Union
55
prev-x x
head[H] 12 3 6
18 37
15 7
8 29 10 44
Like binary addition:
33 25 17
28 31
30 23 22 48
1 1 1 (carries)
41
24 50 00111
32
45
10011
55 11010
Union of two Binomial Heap
Algorithm
Different Cases in Union
• Case 1: occurs when degree[x] ≠ degree[next-x], that is,
when x is the root of a Bk-tree and next-x is the root of a
Bl-tree for some l > k.
Here we don’t link x and next-x, so we simply march the
pointers one position farther down the list.
Different Cases in Union
• Case 2: Occurs when x is the first of three roots of equal degree, that is,
when degree[x] = degree[next-x] = degree[sibling[next-x]] .
• We handle this case in the same manner as case 1: we just march the
pointers one position farther down the list.
• The next iteration will execute either case 3 or case 4
• to combine the second and third of the three equal-degree roots.
Different Cases in Union
• Cases 3 and 4 occur when x is the first of two roots of equal degree, that is,
when
• degree[x] = degree[next-x] ≠ degree[sibling[next-x]] .
• These cases may occur in any iteration, but one of them always occurs
immediately following case 2. In cases 3 and 4.
• Here we link x and next-x.
• The two cases are distinguished by whether x or next-x has the smaller key,
which determines the node that will be the root after the two are linked.
Different Cases in Union
• Cases 3 and 4 occur when x is the first of two roots of equal degree, that is,
when
• degree[x] = degree[next-x] ≠ degree[sibling[next-x]] .
• These cases may occur in any iteration, but one of them always occurs
immediately following case 2. In cases 3 and 4.
• Here we link x and next-x.
• The two cases are distinguished by whether x or next-x has the smaller key,
which determines the node that will be the root after the two are linked.
Example of Union of Binomial Heaps
Example of Union of Binomial Heaps
Example of Union of Binomial Heaps
Example of Union of Binomial Heaps
Example of Union of Binomial Heaps
Example of Union of Binomial Heaps
Inserting a node in Binomial
Extracting the node with minimum key
Extracting the node with minimum key: Example
Extracting the node with minimum key: Example
Decreasing a key
Decreasing a key: Example “26 is
decreased to 7”
Decreasing a key: Example
Deleting a key