0% found this document useful (0 votes)
22 views37 pages

Binomial Heaps

Uploaded by

Vanshika Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views37 pages

Binomial Heaps

Uploaded by

Vanshika Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Binomial Heaps

Binomial trees and binomial heaps


• A binomial heap is a collection of binomial trees
• The binomial tree Bk is an ordered tree defined
recursively.
• The binomial tree B0 consists of a single node.
• The binomial tree Bk consists of two binomial trees
Bk−1 that are linked together.
• The root of one is the leftmost child of the root of the
other.
• Figure shows the binomial trees B0 through B4.
Binomial trees and binomial heaps
• A binomial heap is a collection
of binomial trees
• The binomial tree Bk is an
ordered tree defined
recursively.
• The binomial tree B0 consists of
a single node.
• The binomial tree Bk consists of
two binomial trees Bk−1 that
are linked together.
• The root of one is the leftmost
child of the root of the other.
• Figure shows the binomial
trees B0 through B4.
Binomial trees and binomial heaps
• A binomial heap is a collection
of binomial trees
• The binomial tree Bk is an
ordered tree defined
recursively.
• The binomial tree B0 consists of
a single node.
• The binomial tree Bk consists of
two binomial trees Bk−1 that
are linked together.
• The root of one is the leftmost
child of the root of the other.
• Figure shows the binomial
trees B0 through B4.
Properties of binomial trees
Properties of binomial trees
Binomial heaps
Representing 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

Depending on what x, next-x, and sibling[next-x] point to, Union


links trees with the same root degree.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy