© 2000-2003 Haluk Bingöl v2.23
Data Structures and Algorithms - 03Data Structures and Algorithms - 03
Heaps and Priority QueuesHeaps and Priority Queues
Dr. Haluk BingölDr. Haluk Bingöl
BÜ - CmpEBÜ - CmpE
[email protected]@boun.edu.tr
meraklisina.commeraklisina.com
BU-SWE 510BU-SWE 510
Fall 2003Fall 2003
meraklisina.comeraklisina.co
mm
2
© 2
00
3 b
ing
ol
ContentContent
• MotivationMotivation
• Binary HeapBinary Heap
• Leftist HeapsLeftist Heaps
• Binomial HeapsBinomial Heaps
meraklisina.comeraklisina.co
mm
3
© 2
00
3 b
ing
ol
MotivationMotivation
meraklisina.comeraklisina.co
mm
4
© 2
00
3 b
ing
ol
Motivation … Motivation … ProblemProblem
• Waiting Waiting – Queue Queue
1 4 2
meraklisina.comeraklisina.co
mm
5
© 2
00
3 b
ing
ol
Motivation … Motivation … Problem - At the BankProblem - At the Bank
• Queue at the counterQueue at the counter
• Give priority to Give priority to – ElderlyElderly
– PregnantPregnant
– With babyWith baby
– ……
1 4 2
meraklisina.comeraklisina.co
mm
6
© 2
00
3 b
ing
ol
Motivation … Motivation … Problem - Operating Problem - Operating SystemSystem• Processes waiting for processorProcesses waiting for processor
• Give priority to Give priority to – I/O boundI/O bound
– InterrupsInterrups
– ……
1 4 2
meraklisina.comeraklisina.co
mm
7
© 2
00
3 b
ing
ol
Motivation … Motivation … Problem – Ho1spitalProblem – Ho1spital
• Patients waiting for help in Emergency Patients waiting for help in Emergency RoomRoom
• Give priority to Give priority to – Severely woundedSeverely wounded
– Bleading Bleading
– ……
1 4 2
meraklisina.comeraklisina.co
mm
8
© 2
00
3 b
ing
ol
Motivation … Motivation … Problem - In GeneralProblem - In General
• Anything waiting for serviceAnything waiting for service
– PrinterPrinter
– CPUCPU
1 4 2
meraklisina.comeraklisina.co
mm
9
© 2
00
3 b
ing
ol
Review of QueuesReview of Queues
meraklisina.comeraklisina.co
mm
10
© 2
00
3 b
ing
ol
Review QueueReview Queue
FIFO FIFO – first come first come
– first servefirst serve
• One entry placeOne entry place
• One exit placeOne exit place
• No change in orderNo change in order
• No priorityNo priority
public interface Queue extends Container {Object getHead();void enqueue(Object object);Object dequeue();
}
1 4 2
meraklisina.comeraklisina.co
mm
11
© 2
00
3 b
ing
ol
Review Queue …Review Queue …
Sample operationSample operation
1enqueque(4)
1 4enqueque(2)
1 4 2enqueque(3)
1 4 2 3dequeque()
4 2 3dequeque()
2 3enqueque(1)
2 3 1enqueque(1)
2 3 1 1dequeque()
3 1 1dequeque()
1 1dequeque()
1
meraklisina.comeraklisina.co
mm
12
© 2
00
3 b
ing
ol
Review Queue …Review Queue …
PrioritiesPriorities - a few- a few
• If there is a need for change in the If there is a need for change in the orderorder– One queue for each priorityOne queue for each priority
a1 a4
b3 b1 b27 b3 b3
c6 c4 c9
AlgorithmAlgorithm• enqueueenqueue
– put in to proper put in to proper queuequeue
• dequeuedequeue– get from P1 firstget from P1 first– then get from P2then get from P2– then get from P3then get from P3
P1
P2
P3
meraklisina.comeraklisina.co
mm
13
© 2
00
3 b
ing
ol
Review Queue …Review Queue …
PrioritiesPriorities - more- more
• If there is a need for change in the orderIf there is a need for change in the order– One queue for each priorityOne queue for each priority
– Number of different priority categories is Number of different priority categories is knownknown
a1 a4
b3 b1 b27 b3 b3
c6 c4 c9
P1
P2
P3
AlgorithmAlgorithm• enqueueenqueue
– put in to proper put in to proper queuequeue
• dequeuedequeue– get from P1 firstget from P1 first– then get from P2then get from P2– then get from P3then get from P3– ……– then get from P123then get from P123
7 12 41P123
...
meraklisina.comeraklisina.co
mm
14
© 2
00
3 b
ing
ol
Review Queue …Review Queue …
PrioritiesPriorities – too many– too many
a1 a4
b3 b1 b27 b3 b3
c6 c4 c9
P1
P2
P3
AlgorithmAlgorithm• enqueueenqueue
– put in to proper put in to proper queuequeue
• dequeuedequeue– get from P1 firstget from P1 first– then get from P2then get from P2– then get from P3then get from P3– ……– then get from P123then get from P123
7 12 41P123
...
• If there is a need for change in the orderIf there is a need for change in the order– One queue for each priorityOne queue for each priority
– Number of different priority categories is Number of different priority categories is ununknownknown
meraklisina.comeraklisina.co
mm
15
© 2
00
3 b
ing
ol
Priority QueuesPriority Queues
meraklisina.comeraklisina.co
mm
16
© 2
00
3 b
ing
ol
Priority Queues …Priority Queues …AbstractionsAbstractions
• Priority queuePriority queue– A list of items A list of items
– Each item has an associated priorityEach item has an associated priority
• InsertionInsertion– Arbitrary orderArbitrary order
– Arbitrary priorityArbitrary priority
• DeletionDeletion– Item with the lowest(highest) priorityItem with the lowest(highest) priority
meraklisina.comeraklisina.co
mm
17
© 2
00
3 b
ing
ol
Priority Queues … Priority Queues … Abstract OperationsAbstract Operations
• enqueueenqueue– Puts an object in the containerPuts an object in the container
• findMinfindMin– Returns a reference to the smallest Returns a reference to the smallest
object in the containerobject in the container
• dequeueMindequeueMin– Removes the smallest object from the Removes the smallest object from the
containercontainer
meraklisina.comeraklisina.co
mm
18
© 2
00
3 b
ing
ol
Simple ImplementationsSimple Implementations
meraklisina.comeraklisina.co
mm
19
© 2
00
3 b
ing
ol
Simple Implementations … Simple Implementations … ArraysArrays
• Unordered ArrayUnordered Array– InsertionInsertion
• at the endat the end
• O(1)O(1)
– DeletionDeletion• Search the minSearch the min
• Linear search O(n)Linear search O(n)
meraklisina.comeraklisina.co
mm
20
© 2
00
3 b
ing
ol
Simple Implementations … Simple Implementations … ArraysArrays
• Ordered ArrayOrdered Array– InsertionInsertion
• In the orderIn the order
• O(n)O(n)
– DeletionDeletion
• At the endAt the end
• O(1)O(1)
• Unordered ArrayUnordered Array– InsertionInsertion
• at the endat the end
• O(1)O(1)
– DeletionDeletion• Search the minSearch the min
• Linear search O(n)Linear search O(n)
meraklisina.comeraklisina.co
mm
21
© 2
00
3 b
ing
ol
Simple Implementations … Simple Implementations … Link ListsLink Lists
• Unordered ArrayUnordered Array– InsertionInsertion
• at the endat the end
• O(1)O(1)
– DeletionDeletion• Search the minSearch the min
• Linear search O(n)Linear search O(n)
• Unordered Link ListUnordered Link List
• Ordered ArrayOrdered Array– InsertionInsertion
• In the orderIn the order
• O(n)O(n)
– DeletionDeletion
• At the endAt the end
• O(1)O(1)
• Ordered Link ListOrdered Link List
meraklisina.comeraklisina.co
mm
22
© 2
00
3 b
ing
ol
Simple Implementations … Simple Implementations … Binary Search TreeBinary Search Tree
• InsertInsert– O(log n)O(log n)
• DeletionDeletion– O(log n)O(log n)
• Unused propertiesUnused properties
a
b c
meraklisina.comeraklisina.co
mm
23
© 2
00
3 b
ing
ol
Priority QueuesPriority QueuesHeapHeap
meraklisina.comeraklisina.co
mm
24
© 2
00
3 b
ing
ol
Class HierarchyClass Hierarchy
ContainerContainer
PriorityQueuePriorityQueue TreeTree
BinaryHeaBinaryHeapp
MergeablePriorityQueMergeablePriorityQueueue
BinomialQueBinomialQueueue
LeftistHeapLeftistHeap
BinaryTreBinaryTreee
GeneralTreGeneralTreee
BinomialTreBinomialTreee
meraklisina.comeraklisina.co
mm
25
© 2
00
3 b
ing
ol
Priority Queues … Priority Queues … Priority Queue InterfacePriority Queue Interface
• enqueueenqueue
– Puts an object in the Puts an object in the priorityQueuepriorityQueue
• findMinfindMin
– Returns a reference to Returns a reference to the smallest object in the smallest object in the priorityQueuethe priorityQueue
• dequeueMindequeueMin
– Removes the smallest Removes the smallest object from the object from the priorityQueuepriorityQueue
public interface PriorityQueue extends Container {
void enqueue(Comparable object);Comparable findMin();Comparable dequeueMin();
}
meraklisina.comeraklisina.co
mm
26
© 2
00
3 b
ing
ol
Priority Queues … Priority Queues … Mergeable Priority Queue Mergeable Priority Queue InterfaceInterface
public interface MergeablePriorityQueue extends PriorityQueue {
void merge(MergeablePriorityQueue queue);}
meraklisina.comeraklisina.co
mm
27
© 2
00
3 b
ing
ol
Priority Queues … Priority Queues … TypesTypes
• HeapHeap
• Binary HeapBinary Heap
• Leftist HeapLeftist Heap
• Binomial HeapBinomial Heap
meraklisina.comeraklisina.co
mm
28
© 2
00
3 b
ing
ol
HeapHeap
meraklisina.comeraklisina.co
mm
29
© 2
00
3 b
ing
ol
ReReviewviewN-ary TreeN-ary Tree
DefinitionDefinition
An An N-ary treeN-ary tree T is a finite set of nodes with the following properties: T is a finite set of nodes with the following properties:
1.1. Either the set is empty, Either the set is empty, T = T = ; or ; or
2.2. The set consists of a root, R, and exactly N distinct N-ary trees. That is, the The set consists of a root, R, and exactly N distinct N-ary trees. That is, the remaining nodes are partitioned into Nremaining nodes are partitioned into N0 subsets,0 subsets,TT0 0 , T, T11, … , T, … , TN-1N-1 , each of which is an N-ary tree such that , each of which is an N-ary tree such that T = {R , TT = {R , T0 0 , T, T11, … , T, … , TN-1N-1 }. }.
RemarkRemark The empty tree, T = The empty tree, T = , is a tree, is a tree
DefinitionDefinition The empty trees are called The empty trees are called external nodesexternal nodes because they have no subtrees and because they have no subtrees and therefore appear at the extremities of the tree. Conversely, the non-empty trees are therefore appear at the extremities of the tree. Conversely, the non-empty trees are called called internal nodesinternal nodes..
meraklisina.comeraklisina.co
mm
30
© 2
00
3 b
ing
ol
HeapHeap
DefinitionDefinition
A A (Min) Heap (Min) Heap is a tree, is a tree,
T = {R , TT = {R , T0 0 , T, T11, … , T, … , Tn-1n-1 } }
with the following properties: with the following properties:
1.1. Every subtree of T is a heap; and, Every subtree of T is a heap; and,
2.2. The root of T is less than or equal to the root of every The root of T is less than or equal to the root of every subtree of T. subtree of T. That is, R That is, R R Rii for all i, 0 for all i, 0 i i n , where R n , where Rii is the root of T is the root of Tii . .
R
R1 R1 R1…
meraklisina.comeraklisina.co
mm
31
© 2
00
3 b
ing
ol
HeapHeap
DefinitionDefinition
A A (Min) Heap (Min) Heap is a tree, is a tree, T = {R , TT = {R , T0 0 , T, T11, … , T, … , T0-10-1 } } with the following properties: with the following properties:
1.1. Every subtree of T is a heap; and, Every subtree of T is a heap; and,
2.2. The root of T is less than or equal to the root of every The root of T is less than or equal to the root of every subtree of T. subtree of T. That is, R That is, R R Rii for all i, 0 for all i, 0 i i n , where R n , where Rii is the root of T is the root of Tii . .
R
R1 R1 R1…
Observations• Each node is less then all the subtrees of it.• No restrictions for the relative ordering of the subtrees.
meraklisina.comeraklisina.co
mm
32
© 2
00
3 b
ing
ol
Binary HeapBinary Heap
meraklisina.comeraklisina.co
mm
33
© 2
00
3 b
ing
ol
RecapRecapPerfect Binary TreePerfect Binary Tree
DefinitionDefinition
A A perfect binary treeperfect binary tree of height of height hh00 is a is a binary tree T = binary tree T = {R, T{R, TLL, T, TRR} }
with the following properties: with the following properties:
1.1. If h = 0, TIf h = 0, TLL = = and T and TRR = = . .
2.2. If h > 0, thenIf h > 0, thenTTLL is a perfect binary tree of height h-1. is a perfect binary tree of height h-1. TTRR is a perfect binary tree of height h-1. is a perfect binary tree of height h-1.
TheoremTheorem A perfect binary tree of height h has exactly 2A perfect binary tree of height h has exactly 2h+1h+1 - 1 internal - 1 internal nodes.nodes.
TheoremTheoremThe height of a perfect binary tree with n internal nodes is The height of a perfect binary tree with n internal nodes is log (n+1).log (n+1).
meraklisina.comeraklisina.co
mm
34
© 2
00
3 b
ing
ol
Complete Binary TreeComplete Binary Tree
DefinitionDefinition
A A complete binary tree complete binary tree of height h of height h0, is a 0, is a binary tree T={R, Tbinary tree T={R, TLL, T, TRR} } with the following properties: with the following properties:
1.1. If h=0, TIf h=0, TLL = = and T and TRR = = . .
2.2. For h>0 there are two possibilities: For h>0 there are two possibilities:
a.a. TTLL is a perfect binary tree of height h-1 and is a perfect binary tree of height h-1 and TTRR is a complete binary tree of height h-1 is a complete binary tree of height h-1
b.b. TTLL is a complete binary tree of height h-1 and is a complete binary tree of height h-1 and TTRR is a perfect binary tree of height h-2. is a perfect binary tree of height h-2.
meraklisina.comeraklisina.co
mm
35
© 2
00
3 b
ing
ol
Complete Binary Tree …Complete Binary Tree …• 11
– 11LL
• 22LL
• 22RR is a complete binary tree of height 2 is a complete binary tree of height 2– 55LL is a perfect binary tree of height 1 is a perfect binary tree of height 1– 55RR is a perfect binary tree of height 0 is a perfect binary tree of height 0
– 11RR
5L 5R
2R
meraklisina.comeraklisina.co
mm
36
© 2
00
3 b
ing
ol
Complete Binary Tree …Complete Binary Tree …• 11
– 11LL is a complete binary tree of height 3 is a complete binary tree of height 3 (2a)(2a)
• 22LL is a perfect binary tree of height 2 is a perfect binary tree of height 2
• 22RR is a complete binary tree of height 2 is a complete binary tree of height 2– 55LL is a perfect binary tree of height 1 is a perfect binary tree of height 1– 55RR is a perfect binary tree of height 0 is a perfect binary tree of height 0
– 11RR
2L 2R
1L
meraklisina.comeraklisina.co
mm
37
© 2
00
3 b
ing
ol
Complete Binary Tree …Complete Binary Tree …• 1 is a complete binary tree of height 4 1 is a complete binary tree of height 4 (2b)(2b)
– 11LL is a complete binary tree of height 3 is a complete binary tree of height 3 (2a)(2a)
• 22LL is a perfect binary tree of height 2 is a perfect binary tree of height 2
• 22RR is a complete binary tree of height 2 is a complete binary tree of height 2– 55LL is a perfect binary tree of height 1 is a perfect binary tree of height 1– 55RR is a perfect binary tree of height 0 is a perfect binary tree of height 0
– 11RR is a perfect binary tree of height 2 is a perfect binary tree of height 2
1L 1R
meraklisina.comeraklisina.co
mm
38
© 2
00
3 b
ing
ol
Complete Binary Tree …Complete Binary Tree …Array representationArray representation
a
b c
b = 2a
c = 2a+1
a = b/2a = c/2
meraklisina.comeraklisina.co
mm
39
© 2
00
3 b
ing
ol
Complete Binary Tree …Complete Binary Tree …
TheoremTheorem
A complete binary tree of height hA complete binary tree of height h0 0 contains contains at least 2at least 2hh and and at most 2at most 2h+1 h+1 - 1 nodes.- 1 nodes.
meraklisina.comeraklisina.co
mm
40
© 2
00
3 b
ing
ol
Complete Binary Tree …Complete Binary Tree …
TheoremTheorem
The internal path length of a The internal path length of a binary treebinary tree with n nodes with n nodes is at least as big as is at least as big as the internal path length of a the internal path length of a complete binary treecomplete binary tree with n with n nodes. nodes.
DefinitionDefinition
The The internal path lengthinternal path length of a tree is simply the sum of the of a tree is simply the sum of the depths (levels) of all the internal nodes in the treedepths (levels) of all the internal nodes in the tree
n
iidthLengthinternalPa
1
ddii is the depth of is the depth of the ithe ithth node node
n is the number n is the number of nodesof nodes
meraklisina.comeraklisina.co
mm
41
© 2
00
3 b
ing
ol
Complete N-ary TreeComplete N-ary Tree
InformallyInformally
• a complete tree is a tree in which a complete tree is a tree in which – all the levels are full except for the all the levels are full except for the
bottom level and bottom level and
– the bottom level is filled from left to the bottom level is filled from left to right.right.
meraklisina.comeraklisina.co
mm
42
© 2
00
3 b
ing
ol
Complete N-ary Tree …Complete N-ary Tree …
DefinitionDefinition
A A complete N-ary treecomplete N-ary tree of height of height hh00, is an , is an N-ary tree N-ary tree T = {R , TT = {R , T0 0 , T, T11, … , T, … , TN-1N-1 } }with the following properties with the following properties
1.1. If h=0, If h=0, TTii = = for all i, 0 for all i, 0 i < N. i < N.
2.2. For h>0 there exists a j, 0 For h>0 there exists a j, 0 j < N such that j < N such that a. Ta. Tii is a perfect binary tree of height h-1 for all i, 0 is a perfect binary tree of height h-1 for all i, 0 i i < j < j b. Tb. Tjj is a complete binary tree of height h-1 is a complete binary tree of height h-1 c. c. TTii is a perfect binary tree of height h-2 for all i, is a perfect binary tree of height h-2 for all i, j<i<N j<i<N
meraklisina.comeraklisina.co
mm
43
© 2
00
3 b
ing
ol
Complete N-ary Tree … Complete N-ary Tree … Array RepresentationArray Representation
c1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
c2 cN…
i
meraklisina.comeraklisina.co
mm
44
© 2
00
3 b
ing
ol
Binary Heap Binary Heap ImplementationImplementation
meraklisina.comeraklisina.co
mm
45
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …ConstructorsConstructors
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];
}
…}
meraklisina.comeraklisina.co
mm
46
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …enqueueenqueue
meraklisina.comeraklisina.co
mm
47
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …enqueueenqueue
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 upint hole = ++currentSize;for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2)
array[hole] = array[hole / 2];array[hole] = x;
}
…}
meraklisina.comeraklisina.co
mm
48
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …enqueueenqueue
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 upint 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)
meraklisina.comeraklisina.co
mm
49
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …enqueueenqueue - Exacution - Exacution
• insert 14
meraklisina.comeraklisina.co
mm
50
© 2
00
3 b
ing
ol
Binary Heap Implementation Binary Heap Implementation ……enqueueenqueue - Exacution - Exacution
• insert 14
meraklisina.comeraklisina.co
mm
51
© 2
00
3 b
ing
ol
Binary Heap Implementation Binary Heap Implementation ……enqueueenqueue - Exacution - Exacution
• insert 14
meraklisina.comeraklisina.co
mm
52
© 2
00
3 b
ing
ol
Binary Heap Implementation Binary Heap Implementation ……enqueueenqueue - Exacution - Exacution
• insert 14
meraklisina.comeraklisina.co
mm
53
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …dequeuedequeue
meraklisina.comeraklisina.co
mm
54
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …dequeuedequeue
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;}…}
meraklisina.comeraklisina.co
mm
55
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …dequeuedequeue
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;}…}
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; // leftif (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;
}…}
meraklisina.comeraklisina.co
mm
56
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …dequeuedequeue
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;}…}
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; // leftif (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;
}…}
Worst-case: O(log n)
meraklisina.comeraklisina.co
mm
57
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …dequeuedequeue – Execution – Execution
meraklisina.comeraklisina.co
mm
58
© 2
00
3 b
ing
ol
Binary Heap Implementation Binary Heap Implementation ……dequeuedequeue – Execution – Execution
meraklisina.comeraklisina.co
mm
59
© 2
00
3 b
ing
ol
Binary Heap Implementation Binary Heap Implementation ……dequeuedequeue – Execution – Execution
meraklisina.comeraklisina.co
mm
60
© 2
00
3 b
ing
ol
Binary Heap Implementation Binary Heap Implementation ……dequeuedequeue – Execution – Execution
meraklisina.comeraklisina.co
mm
61
© 2
00
3 b
ing
ol
Binary Heap Implementation Binary Heap Implementation ……dequeuedequeue – Execution – Execution
meraklisina.comeraklisina.co
mm
62
© 2
00
3 b
ing
ol
Binary Heap Implementation Binary Heap Implementation ……dequeuedequeue – Execution – Execution
meraklisina.comeraklisina.co
mm
63
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …buildHeapbuildHeap
public class BinaryHeap {…
/** * Establish heap order property from an arbitrary * arrangement of items. Runs in linear time. */private void buildHeap() {
for (int i = currentSize / 2; i > 0; i--)percolateDown(i);
}
…}
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; // leftif (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;
}…}
meraklisina.comeraklisina.co
mm
64
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …buildHeapbuildHeap
public class BinaryHeap {…
/** * Establish heap order property from an arbitrary * arrangement of items. Runs in linear time. */private void buildHeap() {
for (int i = currentSize / 2; i > 0; i--)percolateDown(i);
}
…}
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; // leftif (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;
}…}
meraklisina.comeraklisina.co
mm
65
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …buildHeapbuildHeap
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; // leftif (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;
}…}
•Weiss•Fig 6.15•6.16•6.17•6.18
meraklisina.comeraklisina.co
mm
66
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …findMinfindMin
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];}…}
meraklisina.comeraklisina.co
mm
67
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …Best Best Practices: Practices:
Header SampleHeader 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
/** * Implements a binary heap. * Note that all "matching" is based on the compareTo method. * @author Mark Allen Weiss */public class BinaryHeap {…}
meraklisina.comeraklisina.co
mm
68
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …Best Best Practices: Practices:
Test SampleTest Sample• Use main() Use main()
for testingfor testingpublic class BinaryHeap {…// Test programpublic static void main(String[] args) {
int numItems = 10000;BinaryHeap h = new BinaryHeap(numItems);int i = 37;
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);
}}}
meraklisina.comeraklisina.co
mm
69
© 2
00
3 b
ing
ol
Binary Heap Implementation …Binary Heap Implementation …Complexity SummaryComplexity Summary
OperationOperation ComplexityComplexity
enqueueenqueue O(logO(log22 n) n)
dequeuedequeue O(logO(log22 n) n)
builtbuilt O(n)O(n)
• BinaryBinary
meraklisina.comeraklisina.co
mm
70
© 2
00
3 b
ing
ol
N-ary Heap Implementation …N-ary Heap Implementation …Complexity SummaryComplexity Summary
OperationOperation ComplexityComplexity
enqueueenqueue O(logO(logNN n) n)
dequeuedequeue O(logO(logNN n) n)
builtbuilt O(n)O(n)
• N-aryN-ary
meraklisina.comeraklisina.co
mm
71
© 2
00
3 b
ing
ol
Leftist TreesLeftist Trees
meraklisina.comeraklisina.co
mm
72
© 2
00
3 b
ing
ol
Leftist TreeLeftist Tree
• Mergeable prMergeable priority queuesiority queues– Leftist heapsLeftist heaps
meraklisina.comeraklisina.co
mm
73
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Null PathNull Path
DefinitionDefinition
Consider an arbitrary node x in some binary tree T. Consider an arbitrary node x in some binary tree T. The The null pathnull path of node x is the shortest path in T from x to an of node x is the shortest path in T from x to an external node of T. external node of T.
The The null path lengthnull path length of node x is the length of its null path. of node x is the length of its null path.
DefinitionDefinition
The The null path length null path length of an empty tree is zero.of an empty tree is zero.
The The null path lengthnull path length of a non-empty binary tree T={R, T of a non-empty binary tree T={R, TLL, T, TRR} is } is the null path length its root R. the null path length its root R.
meraklisina.comeraklisina.co
mm
74
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Leftist TreeLeftist Tree
DefinitionDefinition
A A leftist treeleftist tree is a binary tree is a binary tree TT with the following properties: with the following properties:
1.1. Either Either TT = = ; or ; or
2.2. T= {R, TT= {R, TLL, T, TRR}} , where both , where both TTLL and and TTRR are leftist trees are leftist trees which have null path lengths dwhich have null path lengths dLL and d and dLL, respectively, such that, respectively, such that d dLL d dLL
RemarkRemark A leftist tree is a tree in which the shortest path to an A leftist tree is a tree in which the shortest path to an external node is always on the right. external node is always on the right.
meraklisina.comeraklisina.co
mm
75
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Length of the Right PathLength of the Right Path
TheoremTheorem
Consider a leftist tree Consider a leftist tree TT which contains which contains nn internal internal nodes. The path leading from the root of nodes. The path leading from the root of TT downwards to the rightmost external node contains downwards to the rightmost external node contains at most at most log2(n+1)log2(n+1) nodes. nodes.
meraklisina.comeraklisina.co
mm
76
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Leftist HeapsLeftist Heaps
• A heap-ordered treeA heap-ordered tree– The positions of the keys in the treeThe positions of the keys in the tree
• A leftist treeA leftist tree– The shape of the treeThe shape of the tree
meraklisina.comeraklisina.co
mm
77
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Merging Leftist HeapsMerging Leftist Heaps• if h1 is empty, swap h1 and if h1 is empty, swap h1 and
h2h2
• otherwise, assume the root otherwise, assume the root of h2 is larger than the root of h2 is larger than the root of h1:of h1:
• recursively merge h2 with recursively merge h2 with the right subheap of h1the right subheap of h1
• if the right subheap of h1 if the right subheap of h1 now has a larger null path now has a larger null path length then its left subheap, length then its left subheap, swap the left and right swap the left and right subheapssubheaps
• if h2 initially has the smaller if h2 initially has the smaller root, exchange h1 and h2 root, exchange h1 and h2 and proceed as aboveand proceed as above
meraklisina.comeraklisina.co
mm
78
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … ImplementationImplementation - - LeftHeapNodeLeftHeapNode
// 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; }
// 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}
meraklisina.comeraklisina.co
mm
79
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Implementation – MergingImplementation – 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; }
/** * 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); } …}
meraklisina.comeraklisina.co
mm
80
© 2
00
3 b
ing
ol
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; }
/** * 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); } …
Leftist Trees … Leftist Trees … Implementation – MergingImplementation – Merging
… /** * Internal static method to merge two roots. * Assumes trees are not empty, * and h1's root contains smallest item. */ private static LeftHeapNode merge1(LeftHeapNode h1, LeftHeapNode h2) { if (h1.left == null) // Single node h1.left = h2; // Other fields in h1 already accurate else { h1.right = merge(h1.right, h2); if (h1.left.npl < h1.right.npl) swapChildren(h1); h1.npl = h1.right.npl + 1; } return h1; }
/** * Swaps t's two children. */ private static void swapChildren(LeftHeapNode t) { LeftHeapNode tmp = t.left; t.left = t.right; t.right = tmp; } …}
meraklisina.comeraklisina.co
mm
81
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Implementation – enqueue()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); } …}
meraklisina.comeraklisina.co
mm
82
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Implementation – findMin()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; } …}
meraklisina.comeraklisina.co
mm
83
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Implementation – Implementation – dequeueMin()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; } …}
meraklisina.comeraklisina.co
mm
84
© 2
00
3 b
ing
ol
Leftist Trees … Leftist Trees … Complexity SummaryComplexity Summary
OperationOperation ComplexityComplexity
mergemerge O(logO(log22 n n11 + log + log22 nn22))
enqueueenqueue O(logO(log22 n) n)
dequeuedequeue O(logO(log22 n) n)
findMinfindMin O(O(11))
meraklisina.comeraklisina.co
mm
85
© 2
00
3 b
ing
ol
Binomial QueuesBinomial Queues
meraklisina.comeraklisina.co
mm
86
© 2
00
3 b
ing
ol
Binomial QueuesBinomial Queues
• binomial queuesbinomial queues– binomial treesbinomial trees
– forestsforests
– merging of binomial queuesmerging of binomial queues
meraklisina.comeraklisina.co
mm
87
© 2
00
3 b
ing
ol
Binomial Queues …Binomial Queues …
DefinitionDefinition
The The binomial treebinomial tree of order k of order k 0 0 with root R is the tree Bwith root R is the tree Bkk defined defined as followsas follows
1.1. If k = 0, BIf k = 0, B00 = {R}. = {R}. i.e., the binomial tree of order i.e., the binomial tree of order zero consists of a single node, zero consists of a single node, R.R.
2.2. If k > 0, BIf k > 0, Bkk = {R, B = {R, B00, B, B11, … , B, … , Bk-k-
11}. }. i.e., the binomial tree of order k i.e., the binomial tree of order k > 0 comprises the root R, and > 0 comprises the root R, and k binomial subtrees, B0, k binomial subtrees, B0, B1, . . . , B1, . . . , BBk-1k-1..
meraklisina.comeraklisina.co
mm
88
© 2
00
3 b
ing
ol
Binomial Queues …Binomial Queues …Different ViewsDifferent Views
meraklisina.comeraklisina.co
mm
89
© 2
00
3 b
ing
ol
Binomial Queues …Binomial Queues …Number of NodesNumber of Nodes
TheoremTheorem
The binomial tree of order k, BThe binomial tree of order k, Bkk, , contains 2contains 2kk nodes. nodes.
meraklisina.comeraklisina.co
mm
90
© 2
00
3 b
ing
ol
Binomial Queues …Binomial Queues …HeightHeight
TheoremTheorem
The height of the binomial tree of order The height of the binomial tree of order k, Bk, Bkk, is k., is k.
meraklisina.comeraklisina.co
mm
91
© 2
00
3 b
ing
ol
Binomial Queues …Binomial Queues …Binomial TheoremBinomial Theorem
TheoremTheorem
The nThe nthth power of the binomial x+y for n power of the binomial x+y for n 0 is given by0 is given by
where where is called is called binomial coefficientbinomial coefficient
n
i
inin yxi
nyx
0
)!(!
!
ini
n
i
n
meraklisina.comeraklisina.co
mm
92
© 2
00
3 b
ing
ol
Binomial Queues …Binomial Queues …Why Binomial ?Why Binomial ?
TheoremTheorem
The number of nodes at level The number of nodes at level ll in B in Bkk, , the binomial treethe binomial tree of order k, where 0 ≤ of order k, where 0 ≤ ll ≤ ≤ k, k, is given by the binomialis given by the binomial coecoeffifficientcient
l
n
levellevel
0 1 2 3 4 ...0 1 2 3 4 ...
11
1 11 1
1 2 11 2 1
1 3 3 11 3 3 1
11 4 6 4 14 6 4 1
......
kk
00
11
22
33
44
......
meraklisina.comeraklisina.co
mm
93
© 2
00
3 b
ing
ol
• 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:
where bi {0, 1} is the ith bit
• To hold n items use a forest of binomial trees:Fn = {Bi : bi = 1};
Binomial Queues …Binomial Queues …Any Size ?Any Size ?
n
i
iibn
2log
0
2
meraklisina.comeraklisina.co
mm
94
© 2
00
3 b
ing
ol
Binomial Queues …Binomial Queues …Merging Binomial QueuesMerging Binomial Queues
• Merging two binomial queues is like doing binary addition
BB44 BB33 BB11 BB00 FF22
77
2727 11 11 00 11 11
++ BB33 BB11 FF11
00
++ 1010 ++ 11 00 11 00
BB55 BB22 BB00 FF33
77
3737 11 00 00 11 00 11
meraklisina.comeraklisina.co
mm
95
© 2
00
3 b
ing
ol
Binomial Queues …Binomial Queues …Merging Binomial Queues ...Merging Binomial Queues ...
meraklisina.comeraklisina.co
mm
96
© 2
00
3 b
ing
ol
Debuging ToolsDebuging Tools
meraklisina.comeraklisina.co
mm
97
© 2
00
3 b
ing
ol
Debuging ToolsDebuging Tools 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; } }
______binary tree______ ./-4\ .______binary tree______ . / -4 \ ./-2\ .______binary tree______ . / -4 \ ./-2\ . / -6 \ .______binary tree______ . / -7 \ . / -4 \ ./-2\ . / -6 \ .______binary tree______ . / -7 \ . / -2 \ . / -4 \ ./-1\ . / -6 \ .
meraklisina.comeraklisina.co
mm
98
© 2
00
3 b
ing
ol
Debuging ToolsDebuging 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); }
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 + "."); } }
meraklisina.comeraklisina.co
mm
99
© 2
00
3 b
ing
ol
Debuging ToolsDebuging 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); }
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 + "."); } }
...
int numItems = 10000;
BinaryHeap h = new BinaryHeap(numItems);h.insert(new MyInteger(4));
h.insert(new MyInteger(2));
h.insert(new MyInteger(6));
h.insert(new MyInteger(7));
h.insert(new MyInteger(1));
h.printTree();...
______binary tree______ . / -7 \ . / -2 \ . / -4 \ ./-1\ . / -6 \ .
meraklisina.comeraklisina.co
mm
100
© 2
00
3 b
ing
ol
SummarySummary
• Binary HeapBinary Heap
• Leftist HeapsLeftist Heaps
• Binomial HeapsBinomial Heaps
meraklisina.comeraklisina.co
mm
101
© 2
00
3 b
ing
ol
ReferencesReferences
• Data Structures and Algorithms Data Structures and Algorithms with Object-Oriented Design Patterns in Javawith Object-Oriented Design Patterns in JavaPreiss Preiss http://www.brpreiss.com/books/opus5/http://www.brpreiss.com/books/opus5/
• Data Structures and AlgorithmsData Structures and Algorithmswith Object-Oriented Design Patters in C++with Object-Oriented Design Patters in C++PreissPreissWiley, 1999 Wiley, 1999
• Data Structures and Algorithm Analysis in JavaData Structures and Algorithm Analysis in JavaWeissWeissAddison-Wesley, 1999Addison-Wesley, 1999
Top Related