Docsity

Binomial Heaps - Advanced Data Structures - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Binomial Heaps - Advanced Data Structures - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Binomial Heaps

Leftist Binomial heaps

trees Actual Amortized

Insert O(log n) O(1) O(1)

Remove min (or

max)

O(log n) O(n) O(log n)

Meld O(log n) O(1) O(1)

Min Binomial Heap

• Collection of min trees.

Node Structure

• Degree

ƒ Number of children.

• Child

ƒ Pointer to one of the node’s children.

ƒ Null iff node has no child.

• Sibling

ƒ Used for circular linked list of siblings.

• Data

Binomial Heap Representation

• Circular linked list of min trees.

A

Degree fields

not shown.

Meld

C

Remove Min

• Empty binomial heap => fail.

Nonempty Binomial Heap

• Remove a min tree.

• Reinsert subtrees of removed min tree.

• Update binomial heap pointer.

A

Remove Min Tree

• Same as remove a node from a circular list.

a b c d e

A

• No next node => empty after remove.

• Otherwise, copy next-node data and remove

next node.

d

Complexity Of Remove Min

• Remove a min tree.

ƒ O(1).

• Reinsert subtrees.

ƒ O(1).

• Update binomial heap pointer.

ƒ O(s), where s is the number of min trees in final top-level

circular list.

ƒ s = O(n).

• Overall complexity of remove min is O(n).

Enhanced Remove Min

• During reinsert of subtrees, pairwise combine

min trees whose roots have equal degree.

Pairwise Combine

Examine the s = 7 trees in some order.

Determined by the 2 top-level circular lists.

Pairwise Combine

Use a table to keep track of trees by degree.

tree table

Pairwise Combine

tree table

Update tree table.

Pairwise Combine

tree table

Combine 2 min trees of degree 1.

Make the one with larger root a subtree of other.

Pairwise Combine

tree table

Update tree table.

Pairwise Combine

tree table

Pairwise Combine

tree table

Update tree table.

Pairwise Combine

tree table

Pairwise Combine

tree table

Create circular list of remaining trees.

Complexity Of Remove Min

• Create and initialize tree table.

ƒ O(MaxDegree).

ƒ Done once only.

• Examine s min trees and pairwise combine.

ƒ O(s).

• Collect remaining trees from tree table, reset table

entries to null, and set binomial heap pointer.

ƒ O(MaxDegree).

• Overall complexity of remove min.

ƒ O(MaxDegree + s).

Number Of Nodes In B

k

• Nk = number of nodes in Bk.

B 0

• Bk , k > 0, is:

B 0 B 1

B 2

Bk-

N 0 = 1

• Nk = N 0 + N 1 + N 2 + …+ Nk-1 + 1

= 2k.

Equivalent Definition

• Bk , k > 0, is two Bk-1s.

• One of these is a subtree of the other.

B 1 B 2 B 3

N

k

And MaxDegree

• N 0 = 1

• Nk = 2Nk-

= 2k.

• If we start with zero elements and perform

operations as described, then all trees in all

binomial heaps are binomial trees.

• So, MaxDegree = O(log n).