Java Course (c)2001 Haluk Bingol

Download Report

Transcript Java Course (c)2001 Haluk Bingol

Data Structures and Algorithms - 03
Heaps and Priority Queues
BU-SWE 510
Fall 2003
Dr. Haluk Bingöl
BÜ - CmpE
[email protected]
meraklisina.com
© 2000-2003 Haluk Bingöl v2.23
Content
• Motivation
• Binary Heap
• Leftist Heaps
© 2003 bingol
• Binomial Heaps
2
meraklisina.com
© 2003 bingol
Motivation
3
meraklisina.com
Motivation …
Problem
• Waiting
– Queue
© 2003 bingol
1
4
4
2
meraklisina.com
Motivation …
Problem - At the Bank
• Queue at the counter
1
4
2
• Give priority to
– Elderly
– Pregnant
– With baby
© 2003 bingol
– …
5
meraklisina.com
Motivation …
Problem - Operating System
• Processes waiting for processor
1
4
2
• Give priority to
– I/O bound
– Interrups
© 2003 bingol
– …
6
meraklisina.com
Motivation …
Problem – Ho1spital
• Patients waiting for help in Emergency Room
1
4
2
• Give priority to
– Severely wounded
– Bleading
© 2003 bingol
– …
7
meraklisina.com
Motivation …
Problem - In General
• Anything waiting for service
1
4
2
– Printer
© 2003 bingol
– CPU
8
meraklisina.com
© 2003 bingol
Review of Queues
9
meraklisina.com
Review Queue
public interface Queue extends Container {
Object getHead();
void enqueue(Object object);
Object dequeue();
}
FIFO
– first come
– first serve
• One entry place
1
4
2
• One exit place
• No change in order
© 2003 bingol
• No priority
10
meraklisina.com
Review Queue …
© 2003 bingol
1
4
1
4
2
1
4
2
4
2
3
2
3
2
3
1
2
3
1
3
1
1
1
1
1
11
enqueque(4)
1
enqueque(2)
3
enqueque(3)
dequeque()
dequeque()
Sample operation
enqueque(1)
1
enqueque(1)
dequeque()
dequeque()
dequeque()
meraklisina.com
Review Queue …
Priorities - a few
• If there is a need for change in the order
© 2003 bingol
– One queue for each priority
12
P1
a1
a4
P2
b3
b1
b27
P3
c6
c4
c9
b3
b3
Algorithm
• enqueue
– put in to proper queue
• dequeue
– get from P1 first
– then get from P2
– then get from P3
meraklisina.com
Review Queue …
Priorities - more
• If there is a need for change in the order
– One queue for each priority
– Number of different priority categories is known
P1
a1
a4
P2
b3
b1
b27
P3
c6
c4
c9
© 2003 bingol
..
.
P123
13
7
12
41
b3
b3
Algorithm
• enqueue
– put in to proper queue
• dequeue
– get from P1 first
– then get from P2
– then get from P3
– …
meraklisina.com
– then get from P123
Review Queue …
Priorities – too many
• If there is a need for change in the order
– One queue for each priority
– Number of different priority categories is unknown
P1
a1
a4
P2
b3
b1
b27
P3
c6
c4
c9
© 2003 bingol
..
.
P123
14
7
12
41
b3
b3
Algorithm
• enqueue
– put in to proper queue
• dequeue
– get from P1 first
– then get from P2
– then get from P3
– …
meraklisina.com
– then get from P123
© 2003 bingol
Priority Queues
15
meraklisina.com
Priority Queues …
Abstractions
• Priority queue
– A list of items
– Each item has an associated priority
• Insertion
– Arbitrary order
– Arbitrary priority
• Deletion
© 2003 bingol
– Item with the lowest(highest) priority
16
meraklisina.com
Priority Queues …
Abstract Operations
• enqueue
– Puts an object in the container
• findMin
– Returns a reference to the smallest object in
the container
• dequeueMin
© 2003 bingol
– Removes the smallest object from the
container
17
meraklisina.com
© 2003 bingol
Simple Implementations
18
meraklisina.com
Simple Implementations …
Arrays
• Unordered Array
– Insertion
• at the end
• O(1)
– Deletion
• Search the min
© 2003 bingol
• Linear search O(n)
19
meraklisina.com
Simple Implementations …
Arrays
• Unordered Array
– Insertion
© 2003 bingol
– Insertion
• at the end
• In the order
• O(1)
• O(n)
– Deletion
20
• Ordered Array
– Deletion
• Search the min
• At the end
• Linear search O(n)
• O(1)
meraklisina.com
Simple Implementations …
Link Lists
• Unordered Array
– Insertion
• In the order
• O(1)
• O(n)
– Deletion
• Search the min
• At the end
• Linear search O(n)
• O(1)
• Unordered Link List
© 2003 bingol
– Insertion
• at the end
– Deletion
21
• Ordered Array
• Ordered Link List
meraklisina.com
Simple Implementations …
Binary Search Tree
• Insert
– O(log n)
a
• Deletion
– O(log n)
b
c
© 2003 bingol
• Unused properties
22
meraklisina.com
© 2003 bingol
Priority Queues
Heap
23
meraklisina.com
Class Hierarchy
Container
PriorityQueue
BinaryHeap
MergeablePriorityQueue
© 2003 bingol
BinomialQueue
24
Tree
LeftistHeap
BinaryTree
GeneralTree
BinomialTree
meraklisina.com
Priority Queues …
Priority Queue Interface
public interface PriorityQueue
extends Container {
void enqueue(Comparable object);
Comparable findMin();
Comparable dequeueMin();
}
• enqueue
– Puts an object in the
priorityQueue
• findMin
– Returns a reference to the
smallest object in the
priorityQueue
• dequeueMin
© 2003 bingol
– Removes the smallest object
from the priorityQueue
25
meraklisina.com
Priority Queues …
Mergeable Priority Queue
Interface
public interface MergeablePriorityQueue
extends PriorityQueue {
void merge(MergeablePriorityQueue queue);
© 2003 bingol
}
26
meraklisina.com
Priority Queues …
Types
• Heap
• Binary Heap
• Leftist Heap
© 2003 bingol
• Binomial Heap
27
meraklisina.com
© 2003 bingol
Heap
28
meraklisina.com
Review
N-ary Tree
Definition
An N-ary tree T is a finite set of nodes with the following properties:
1. Either the set is empty, T =  ; or
2. The set consists of a root, R, and exactly N distinct N-ary trees. That
is, the remaining nodes are partitioned into N0 subsets,
T0 , T1, … , TN-1 , each of which is an N-ary tree such that
T = {R , T0 , T1, … , TN-1 }.
Remark
The empty tree, T = , is a tree
Definition
© 2003 bingol
The empty trees are called external nodes because they have no subtrees
and therefore appear at the extremities of the tree. Conversely, the non-empty
trees are called internal nodes.
29
meraklisina.com
Heap
Definition
R
A (Min) Heap is a tree,
T = {R , T0 , T1, … , Tn-1 }
R1
R1
…
R1
with the following properties:
1. Every subtree of T is a heap; and,
© 2003 bingol
2. The root of T is less than or equal to the root of every subtree of T.
That is, R  Ri for all i, 0  i  n , where Ri is the root of Ti .
30
meraklisina.com
Heap
Definition
A (Min) Heap is a tree,
T = {R , T0 , T1, … , T0-1 }
with the following properties:
R
R1
R1
…
R1
1. Every subtree of T is a heap; and,
2. The root of T is less than or equal to the root of every subtree of T.
That is, R  Ri for all i, 0  i  n , where Ri is the root of Ti .
© 2003 bingol
Observations
• Each node is less then all the subtrees of it.
• No restrictions for the relative ordering of the subtrees.
31
meraklisina.com
© 2003 bingol
Binary Heap
32
meraklisina.com
Recap
Perfect Binary Tree
Definition
A perfect binary tree of height h0 is a
binary tree T = {R, TL, TR}
with the following properties:
1. If h = 0, TL =  and TR = .
2. If h > 0, then
TL is a perfect binary tree of height h-1.
TR is a perfect binary tree of height h-1.
© 2003 bingol
Theorem
A perfect binary tree of height h has exactly 2h+1 - 1 internal nodes.
Theorem
The height of a perfect binary tree with n internal nodes is log
(n+1).
33
meraklisina.com
Complete Binary Tree
Definition
A complete binary tree of height h0, is a
binary tree T={R, TL, TR}
with the following properties:
1. If h=0, TL =  and TR = .
2. For h>0 there are two possibilities:
a. TL is a perfect binary tree of height h-1 and
TR is a complete binary tree of height h-1
© 2003 bingol
b. TL is a complete binary tree of height h-1 and
TR is a perfect binary tree of height h-2.
34
meraklisina.com
Complete Binary Tree …
• 1
– 1L
• 2L
• 2R is a complete binary tree of height 2
– 5L is a perfect binary tree of height 1
– 5R is a perfect binary tree of height 0
– 1R
2R
© 2003 bingol
5L
35
5R
meraklisina.com
Complete Binary Tree …
• 1
– 1L is a complete binary tree of height 3
(2a)
• 2L is a perfect binary tree of height 2
• 2R is a complete binary tree of height 2
– 5L is a perfect binary tree of height 1
– 5R is a perfect binary tree of height 0
– 1R
© 2003 bingol
2L
36
1L
2R
meraklisina.com
Complete Binary Tree …
• 1 is a complete binary tree of height 4 (2b)
– 1L is a complete binary tree of height 3
(2a)
• 2L is a perfect binary tree of height 2
• 2R is a complete binary tree of height 2
– 5L is a perfect binary tree of height 1
– 5R is a perfect binary tree of height 0
– 1R is a perfect binary tree of height 2
© 2003 bingol
1L
37
1R
meraklisina.com
Complete Binary Tree …
Array representation
a
b
c
© 2003 bingol
b
c
a
a
38
=
=
=
=
2a
2a+1
b/2
c/2
meraklisina.com
Complete Binary Tree …
Theorem
© 2003 bingol
A complete binary tree of height h0 contains
at least 2h and
at most 2h+1 - 1 nodes.
39
meraklisina.com
Complete Binary Tree …
Theorem
The internal path length of a binary tree with n nodes
is at least as big as
the internal path length of a complete binary tree with n nodes.
Definition
The internal path length of a tree is simply the sum of the depths
(levels) of all the internal nodes in the tree
n
internalPathLength  di
i 1
© 2003 bingol
di is the depth of
the ith node
n is the number of
nodes
40
meraklisina.com
Complete N-ary Tree
Informally
• a complete tree is a tree in which
– all the levels are full except for the bottom
level and
© 2003 bingol
– the bottom level is filled from left to right.
41
meraklisina.com
Complete N-ary Tree …
Definition
A complete N-ary tree of height h0, is an
N-ary tree T = {R , T0 , T1, … , TN-1 }
with the following properties
1. If h=0, Ti =  for all i, 0  i < N.
© 2003 bingol
2. For h>0 there exists a j, 0  j < N such that
a. Ti is a perfect binary tree of height h-1 for all i, 0  i < j
b. Tj is a complete binary tree of height h-1
c. Ti is a perfect binary tree of height h-2 for all i, j<i<N
42
meraklisina.com
Complete N-ary Tree …
Array Representation
i
© 2003 bingol
c1
c2
…
cN
Children of node i
c1 = N(i-1)+2
c2 = N(i-1)+3
c3 = N(i-1)+4
…
cN = Ni+i
Parent of node i
(i-1)N
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
meraklisina.com
© 2003 bingol
Binary Heap Implementation
44
meraklisina.com
Binary Heap Implementation …
Constructors
public class BinaryHeap {
…
/**
* Construct the binary heap.
*/
public BinaryHeap() {
this(DEFAULT_CAPACITY);
}
/**
* Construct the binary heap.
* @param capacity the capacity of the binary heap.
*/
public BinaryHeap(int capacity) {
currentSize = 0;
array = new Comparable[capacity + 1];
}
© 2003 bingol
…
}
45
meraklisina.com
Binary Heap Implementation …
© 2003 bingol
enqueue
46
meraklisina.com
Binary Heap Implementation …
enqueue
public class BinaryHeap {
…
/**
* Insert into the priority queue, maintaining heap order.
* Duplicates are allowed.
* @param x the item to insert.
* @exception Overflow if container is full.
*/
public void insert(Comparable x) throws Overflow {
if (isFull())
throw new Overflow();
// Percolate up
int hole = ++currentSize;
for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2)
array[hole] = array[hole / 2];
array[hole] = x;
}
© 2003 bingol
…
}
47
meraklisina.com
Binary Heap Implementation …
enqueue
public class BinaryHeap {
…
/**
* Insert into the priority queue, maintaining heap order.
* Duplicates are allowed.
* @param x the item to insert.
* @exception Overflow if container is full.
*/
public void insert(Comparable x) throws Overflow {
if (isFull())
throw new Overflow();
// Percolate up
int hole = ++currentSize;
for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2)
array[hole] = array[hole / 2];
array[hole] = x;
•weiss
•Fig 6.6
•Fig 6.7
Worst-case:
O(log n)
}
© 2003 bingol
…
}
48
meraklisina.com
Binary Heap Implementation …
enqueue - Exacution
© 2003 bingol
• insert 14
49
meraklisina.com
Binary Heap Implementation …
enqueue - Exacution
© 2003 bingol
• insert 14
50
meraklisina.com
Binary Heap Implementation …
enqueue - Exacution
© 2003 bingol
• insert 14
51
meraklisina.com
Binary Heap Implementation …
enqueue - Exacution
© 2003 bingol
• insert 14
52
meraklisina.com
Binary Heap Implementation …
© 2003 bingol
dequeue
53
meraklisina.com
Binary Heap Implementation …
dequeue
public class BinaryHeap {
…
/**
* Remove the smallest item from the priority queue.
* @return the smallest item, or null, if empty.
*/
public Comparable deleteMin() {
if (isEmpty())
return null;
Comparable minItem = findMin();
array[1] = array[currentSize--];
percolateDown(1);
return minItem;
© 2003 bingol
}
…
}
54
meraklisina.com
Binary Heap Implementation …
dequeue
public class BinaryHeap {
…
/**
* Remove the smallest item from the priority queue.
* @return the smallest item, or null, if empty.
*/
public class BinaryHeap {
public Comparable deleteMin() {
…
if (isEmpty())
/**
return null;
* Internal method to percolate down in the heap.
* @param hole the index at which the percolate begins.
Comparable minItem = findMin();
*/
array[1] = array[currentSize--]; private void percolateDown(int hole) {
percolateDown(1);
int child;
Comparable tmp = array[hole];
return minItem;
}
for (; hole * 2 <= currentSize; hole = child) {
…
child = hole * 2; // left
}
if (child != currentSize
© 2003 bingol
&& array[child + 1].compareTo(array[child]) < 0)
child++;
if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];
else
break;
}
array[hole] = tmp;
}
…
}
55
meraklisina.com
Binary Heap Implementation …
dequeue
Worst-case:
O(log n)
public class BinaryHeap {
…
/**
* Remove the smallest item from the priority queue.
* @return the smallest item, or null, if empty.
*/
public class BinaryHeap {
public Comparable deleteMin() {
…
if (isEmpty())
/**
return null;
* Internal method to percolate down in the heap.
* @param hole the index at which the percolate begins.
Comparable minItem = findMin();
*/
array[1] = array[currentSize--]; private void percolateDown(int hole) {
percolateDown(1);
int child;
Comparable tmp = array[hole];
return minItem;
}
for (; hole * 2 <= currentSize; hole = child) {
…
child = hole * 2; // left
}
if (child != currentSize
© 2003 bingol
&& array[child + 1].compareTo(array[child]) < 0)
child++;
if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];
else
break;
}
array[hole] = tmp;
}
…
}
56
meraklisina.com
Binary Heap Implementation …
© 2003 bingol
dequeue – Execution
57
meraklisina.com
© 2003 bingol
Binary Heap Implementation …
dequeue – Execution
58
meraklisina.com
© 2003 bingol
Binary Heap Implementation …
dequeue – Execution
59
meraklisina.com
© 2003 bingol
Binary Heap Implementation …
dequeue – Execution
60
meraklisina.com
© 2003 bingol
Binary Heap Implementation …
dequeue – Execution
61
meraklisina.com
© 2003 bingol
Binary Heap Implementation …
dequeue – Execution
62
meraklisina.com
Binary Heap Implementation …
buildHeap
public class BinaryHeap {
…
/**
* Establish heap order property from an arbitrary
public
* arrangement of items. Runs in linear
time.class BinaryHeap {
…
*/
/**
private void buildHeap() {
* Internal
method to percolate down in the heap.
for (int i = currentSize / 2; i > 0;
i--)
* @param hole the index at which the percolate begins.
percolateDown(i);
*/
}
private void percolateDown(int hole) {
int child;
…
Comparable tmp = array[hole];
}
© 2003 bingol
for (; hole * 2 <= currentSize; hole = child) {
child = hole * 2; // left
if (child != currentSize
&& array[child + 1].compareTo(array[child]) < 0)
child++;
if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];
else
break;
}
array[hole] = tmp;
}
…
}
63
meraklisina.com
Binary Heap Implementation …
buildHeap
public class BinaryHeap {
…
/**
* Establish heap order property from an arbitrary
public
* arrangement of items. Runs in linear
time.class BinaryHeap {
…
*/
/**
private void buildHeap() {
* Internal
method to percolate down in the heap.
for (int i = currentSize / 2; i > 0;
i--)
* @param hole the index at which the percolate begins.
percolateDown(i);
*/
}
private void percolateDown(int hole) {
int child;
…
Comparable tmp = array[hole];
}
© 2003 bingol
for (; hole * 2 <= currentSize; hole = child) {
child = hole * 2; // left
if (child != currentSize
&& array[child + 1].compareTo(array[child]) < 0)
child++;
if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];
else
break;
}
array[hole] = tmp;
}
…
}
64
meraklisina.com
Binary Heap Implementation …
buildHeap
© 2003 bingol
•Weiss
•Fig 6.15
•6.16
•6.17
•6.18
public class BinaryHeap {
…
/**
* Internal method to percolate down in the heap.
* @param hole the index at which the percolate begins.
*/
private void percolateDown(int hole) {
int child;
Comparable tmp = array[hole];
for (; hole * 2 <= currentSize; hole = child) {
child = hole * 2; // left
if (child != currentSize
&& array[child + 1].compareTo(array[child]) < 0)
child++;
if (array[child].compareTo(tmp) < 0)
array[hole] = array[child];
else
break;
}
array[hole] = tmp;
}
…
}
65
meraklisina.com
Binary Heap Implementation …
findMin
© 2003 bingol
public class BinaryHeap {
…
/**
* Find the smallest item in the priority queue.
* @return the smallest item, or null, if empty.
*/
public Comparable findMin() {
if (isEmpty())
return null;
return array[1];
}
…
}
66
meraklisina.com
Binary Heap Implementation …Best Practices:
Header Sample
//
//
//
//
//
//
//
//
//
//
//
//
//
BinaryHeap class
CONSTRUCTION: with optional capacity (that defaults to 100)
******************PUBLIC OPERATIONS*********************
void insert( x )
--> Insert x
Comparable deleteMin( )--> Return and remove smallest item
Comparable findMin( ) --> Return smallest item
boolean isEmpty( )
--> Return true if empty; else false
boolean isFull( )
--> Return true if full; else false
void makeEmpty( )
--> Remove all items
******************ERRORS********************************
Throws Overflow if capacity exceeded
© 2003 bingol
/**
* Implements a binary heap.
* Note that all "matching" is based on the compareTo method.
* @author Mark Allen Weiss
*/
public class BinaryHeap {
…
}
67
meraklisina.com
Binary Heap Implementation …Best Practices:
Test Sample
public class BinaryHeap {
…
// Test program
public static void main(String[] args) {
int numItems = 10000;
BinaryHeap h = new BinaryHeap(numItems);
int i = 37;
• Use main()
for testing
© 2003 bingol
try {
for (i = 37; i != 0; i = (i + 37) % numItems)
h.insert(new MyInteger(i));
for (i = 1; i < numItems; i++)
if (((MyInteger) (h.deleteMin())).intValue() != i)
System.out.println("Oops! " + i);
for (i = 37; i != 0; i = (i + 37) % numItems)
h.insert(new MyInteger(i));
h.insert(new MyInteger(0));
i = 9999999;
h.insert(new MyInteger(i));
for (i = 1; i <= numItems; i++)
if (((MyInteger) (h.deleteMin())).intValue() != i)
System.out.println("Oops! " + i + " ");
} catch (Overflow e) {
System.out.println("Overflow (expected)! " + i);
}
}
}
68
meraklisina.com
Binary Heap Implementation …
Complexity Summary
© 2003 bingol
• Binary
69
Operation
Complexity
enqueue
O(log2 n)
dequeue
O(log2 n)
built
O(n)
meraklisina.com
N-ary Heap Implementation …
Complexity Summary
© 2003 bingol
• N-ary
70
Operation
Complexity
enqueue
O(logN n)
dequeue
O(logN n)
built
O(n)
meraklisina.com
© 2003 bingol
Leftist Trees
71
meraklisina.com
Leftist Tree
• Mergeable priority queues
© 2003 bingol
– Leftist heaps
72
meraklisina.com
Leftist Trees …
Null Path
Definition
Consider an arbitrary node x in some binary tree T.
The null path of node x is the shortest path in T from x to an external
node of T.
The null path length of node x is the length of its null path.
Definition
The null path length of an empty tree is zero.
© 2003 bingol
The null path length of a non-empty binary tree T={R, TL, TR} is the null
path length its root R.
73
meraklisina.com
Leftist Trees …
Leftist Tree
Definition
A leftist tree is a binary tree T with the following properties:
1. Either T = ; or
2. T= {R, TL, TR} , where both TL and TR are leftist trees
which have null path lengths dL and dL, respectively, such that
dL  dL
© 2003 bingol
Remark A leftist tree is a tree in which the shortest path to an external
node is always on the right.
74
meraklisina.com
Leftist Trees …
Length of the Right Path
Theorem
© 2003 bingol
Consider a leftist tree T which contains n internal nodes.
The path leading from the root of T downwards to the
rightmost external node contains at most log2(n+1)
nodes.
75
meraklisina.com
Leftist Trees …
Leftist Heaps
• A heap-ordered tree
– The positions of the keys in the tree
• A leftist tree
© 2003 bingol
– The shape of the tree
76
meraklisina.com
Leftist Trees …
Merging Leftist Heaps
• if h1 is empty, swap h1 and h2
• otherwise, assume the root of
h2 is larger than the root of h1:
• recursively merge h2 with the
right subheap of h1
• if the right subheap of h1 now
has a larger null path length
then its left subheap, swap the
left and right subheaps
© 2003 bingol
• if h2 initially has the smaller
root, exchange h1 and h2 and
proceed as above
77
meraklisina.com
Leftist Trees …
Implementation - LeftHeapNode
// Basic node stored in leftist heaps
// Note that this class is not accessible outside
// of package DataStructures
class LeftHeapNode {
// Constructors
LeftHeapNode(Comparable theElement) {
this(theElement, null, null);
}
LeftHeapNode(Comparable theElement, LeftHeapNode lt, LeftHeapNode rt) {
element = theElement;
left = lt;
right = rt;
npl = 0;
}
© 2003 bingol
// Friendly data; accessible by other package routines
Comparable element; // The data in the node
LeftHeapNode left; // Left child
LeftHeapNode right; // Right child
int npl; // null path length
}
78
meraklisina.com
Leftist Trees …
Implementation – Merging
public class LeftistHeap {
…
/**
* Merge rhs into the priority queue.
* rhs becomes empty. rhs must be different from this.
* @param rhs the other leftist heap.
*/
public void merge(LeftistHeap rhs) {
if (this == rhs) // Avoid aliasing problems
return;
root = merge(root, rhs.root);
rhs.root = null;
© 2003 bingol
}
/**
* Internal static method to merge two roots.
* Deals with deviant cases and calls recursive merge1.
*/
private static LeftHeapNode merge(LeftHeapNode h1,
LeftHeapNode h2) {
if (h1 == null)
return h2;
if (h2 == null)
return h1;
if (h1.element.compareTo(h2.element) < 0)
return merge1(h1, h2);
else
return merge1(h2, h1);
}
…
}
79
meraklisina.com
Leftist Trees …
© 2003 bingol
Implementation – Merging
public class LeftistHeap {
…
/**
…
* Merge rhs into the priority/**
queue.
* rhs becomes empty. rhs must *beInternal
different
from method
this. to merge two roots.
static
* @param rhs the other leftist* heap.
Assumes trees are not empty,
*/
* and h1's root contains smallest item.
public void merge(LeftistHeap rhs)
{
*/
if (this == rhs) // Avoid private
aliasingstatic
problems
LeftHeapNode merge1(LeftHeapNode h1,
return;
LeftHeapNode h2) {
if (h1.left == null) // Single node
root = merge(root, rhs.root);
h1.left = h2; // Other fields in h1 already accurate
rhs.root = null;
else {
}
h1.right = merge(h1.right, h2);
if (h1.left.npl < h1.right.npl)
/**
swapChildren(h1);
* Internal static method to merge twoh1.npl
roots.= h1.right.npl + 1;
* Deals with deviant cases and calls
recursive merge1.
}
*/
return h1;
private static LeftHeapNode merge(LeftHeapNode
h1,
}
LeftHeapNode h2) {
if (h1 == null)
/**
return h2;
* Swaps t's two children.
if (h2 == null)
*/
return h1;
private static void swapChildren(LeftHeapNode t) {
if (h1.element.compareTo(h2.element)
< 0) tmp = t.left;
LeftHeapNode
return merge1(h1, h2);
t.left = t.right;
else
t.right = tmp;
return merge1(h2, h1);}
}
…
…
}
80
meraklisina.com
Leftist Trees …
Implementation – enqueue()
public class LeftistHeap {
…
/**
* Insert into the priority queue,
* maintaining heap order.
* @param x the item to insert.
*/
public void insert(Comparable x) {
root = merge(new LeftHeapNode(x), root);
}
…
© 2003 bingol
}
81
meraklisina.com
Leftist Trees …
Implementation – findMin()
public class LeftistHeap {
…
/**
* Find the smallest item in the priority queue.
* @return the smallest item, or null, if empty.
*/
public Comparable findMin() {
if (isEmpty())
return null;
return root.element;
}
…
© 2003 bingol
}
82
meraklisina.com
Leftist Trees …
Implementation – dequeueMin()
public class LeftistHeap {
…
/**
* Remove the smallest item from the priority queue.
* @return the smallest item, or null, if empty.
*/
public Comparable deleteMin() {
if (isEmpty())
return null;
Comparable minItem = root.element;
root = merge(root.left, root.right);
return minItem;
}
…
© 2003 bingol
}
83
meraklisina.com
Leftist Trees …
© 2003 bingol
Complexity Summary
84
Operation
Complexity
merge
O(log2 n1 + log2 n2)
enqueue
O(log2 n)
dequeue
O(log2 n)
findMin
O(1)
meraklisina.com
© 2003 bingol
Binomial Queues
85
meraklisina.com
Binomial Queues
• binomial queues
– binomial trees
– forests
© 2003 bingol
– merging of binomial queues
86
meraklisina.com
Binomial Queues …
Definition
The binomial tree of order k  0
with root R is the tree Bk defined as
follows
1. If k = 0, B0 = {R}.
i.e., the binomial tree of order zero
consists of a single node, R.
© 2003 bingol
2. If k > 0, Bk = {R, B0, B1, … , Bk-1}.
i.e., the binomial tree of order k >
0 comprises the root R, and k
binomial subtrees, B0, B1, . . . ,
Bk-1.
87
meraklisina.com
Binomial Queues …
© 2003 bingol
Different Views
88
meraklisina.com
Binomial Queues …
Number of Nodes
Theorem
© 2003 bingol
The binomial tree of order k, Bk, contains 2k
nodes.
89
meraklisina.com
Binomial Queues …
Height
Theorem
© 2003 bingol
The height of the binomial tree of order k, Bk,
is k.
90
meraklisina.com
Binomial Queues …
Binomial Theorem
Theorem
The nth power of the binomial x+y for n  0 is
given by
n
 n  i n i
n
x  y      x y
i 0
where
© 2003 bingol
 n
n!
  
 i  i!(n  i)!
91
i
is called binomial coefficient
meraklisina.com
Binomial Queues …
Why Binomial ?
Theorem
The number of nodes at level l in Bk,
the binomial tree of order k, where 0 ≤ l ≤ k,
n
is given by the binomial coefficient  l 
© 2003 bingol
k
0
1
2
3
4
...
92
level
0 1
1
1 1
1 2
1 3
1 4
...
2 3 4
...
1
3 1
6 4 1
meraklisina.com
Binomial Queues …
Any Size ?
• Binomial trees only come in sizes that are a power of 2
• How to represent arbitrary number, n, of items?
• Consider the binary representation of the number n:
log n
where bi  {0, 1} is the ith bit
n
b 2i

2
i 0
i
© 2003 bingol
• To hold n items use a forest of binomial trees:
Fn = {Bi : bi = 1};
93
meraklisina.com
Binomial Queues …
Merging Binomial Queues
• Merging two binomial queues is like doing
binary addition
B4
© 2003 bingol
+
B5
94

B3

B1
B0
F27
B3

B1

F10

B2

B0
F37
27
+
10
37
1
+
1
0
1
0
1
1
1
0
1
0
0
1
0
1
meraklisina.com
Binomial Queues …
© 2003 bingol
Merging Binomial Queues ...
95
meraklisina.com
© 2003 bingol
Debuging Tools
96
meraklisina.com
Debuging Tools
© 2003 bingol
public static void main(String[] args) {
int numItems = 10000;
BinaryHeap h = new BinaryHeap(numItems);
switch (1) {
case 1 :
try {
h.insert(new MyInteger(4));
h.printTree();
h.insert(new MyInteger(2));
h.printTree();
h.insert(new MyInteger(6));
h.printTree();
h.insert(new MyInteger(7));
h.printTree();
h.insert(new MyInteger(1));
h.printTree();
} catch (Overflow e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
case 2 :
...
break;
default :
break;
}
}
97
______binary
.
/
-4
\
.
______binary
.
/
-4
\
.
/
-2
\
.
______binary
.
/
-4
\
.
/
-2
\
.
/
-6
\
.
______binary
.
/
-7
\
.
/
-4
\
.
/
-2
\
.
/
-6
\
.
______binary
.
/
-7
\
.
/
-2
\
.
/
-4
\
.
/
-1
\
.
/
-6
\
.
tree______
tree______
tree______
tree______
tree______
meraklisina.com
Debuging Tools
public void printTree() {
System.out.println("______binary tree______");
if (isEmpty())
System.out.println("Empty tree");
else
printTree(1, 0);
}
public void printTree(String name) {
System.out.println("______" + name + "______");
if (isEmpty())
System.out.println("Empty tree");
else
printTree(1, 0);
}
© 2003 bingol
private void printTree(int t, int depth) {
String space = "";
for (int i = 0; i < depth; i++) {
space += "
";
}
if (t <= currentSize) {
int d = ++depth;
printTree(2 * t, d);
System.out.println(space + "/");
System.out.print(space + "-");
System.out.println(array[t]);
System.out.println(space + "\\");
printTree(2 * t + 1, d);
} else {
System.out.println(space + ".");
}
}
98
meraklisina.com
Debuging Tools
public void printTree() {
System.out.println("______binary tree______");
if (isEmpty())
System.out.println("Empty tree");
else
printTree(1, 0);
}
...
int numItems = 10000;
BinaryHeap h = new BinaryHeap(numItems);
h.insert(new MyInteger(4));
h.insert(new MyInteger(2));
h.insert(new MyInteger(6));
public void printTree(String name) {
System.out.println("______" + name + "______"); h.insert(new MyInteger(7));
if (isEmpty())
h.insert(new MyInteger(1));
System.out.println("Empty tree");
h.printTree();
else
...
printTree(1, 0);
}
© 2003 bingol
private void printTree(int t, int depth) {
String space = "";
for (int i = 0; i < depth; i++) {
space += "
";
}
if (t <= currentSize) {
int d = ++depth;
printTree(2 * t, d);
System.out.println(space + "/");
System.out.print(space + "-");
System.out.println(array[t]);
System.out.println(space + "\\");
printTree(2 * t + 1, d);
} else {
System.out.println(space + ".");
}
}
99
______binary tree______
.
/
-7
\
.
/
-2
\
.
/
-4
\
.
/
-1
\
.
/
-6
\
.
meraklisina.com
Summary
• Binary Heap
• Leftist Heaps
© 2003 bingol
• Binomial Heaps
100
meraklisina.com
References
• Data Structures and Algorithms
with Object-Oriented Design Patterns in Java
Preiss
http://www.brpreiss.com/books/opus5/
• Data Structures and Algorithms
with Object-Oriented Design Patters in C++
Preiss
Wiley, 1999
© 2003 bingol
• Data Structures and Algorithm Analysis in Java
Weiss
Addison-Wesley, 1999
101
meraklisina.com