-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinPQ.h
158 lines (135 loc) · 3.38 KB
/
MinPQ.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef MIN_PQ_H
#define MIN_PQ_H
#include <vector>
#include <stdexcept>
using namespace std;
class min_pq_error : public std::logic_error
{
public:
explicit min_pq_error(const std::string& what_arg = "")
: std::logic_error(what_arg) {}
};
// MinPQ class
//
// CONSTRUCTION: with an optional capacity (that defaults to 100)
//
// ******************PUBLIC OPERATIONS*********************
// void insert(x) --> Insert x
// deleteMin(minItem) --> Remove (and optionally return) smallest item
// Comparable findMin() --> Return smallest item
// bool isEmpty() --> Return true if empty; else false
// void makeEmpty() --> Remove all items
// ******************ERRORS********************************
// Throws min_pq_error as warranted
template <typename Comparable>
class MinPQ
{
public:
explicit MinPQ(int capacity = 100)
: array(capacity + 1), currentSize(0), orderOK(true) {}
explicit MinPQ(const vector<Comparable> & items)
: array(items.size() + 10), currentSize(items.size(), orderOK(false))
{
for(int i = 0; i < items.size(); i++)
array[i + 1] = items[i];
fixHeap();
}
bool isEmpty() const
{
return currentSize == 0;
}
/**
* Find the smallest item in the priority queue.
* Return the smallest item, or throw min_pq_error if empty.
*/
const Comparable & findMin()
{
if(isEmpty())
throw min_pq_error("findMin: tom heap");
if (!orderOK)
fixHeap();
return array[1];
}
/**
* Insert item x, maintaining heap order.
* Duplicates are allowed.
*/
void insert(const Comparable & x)
{
checkSize();
// Percolate up
int hole = ++currentSize;
if (orderOK) {
for( ; hole > 1 && x < array[hole / 2]; hole /= 2)
array[hole] = array[hole / 2];
}
array[hole] = x;
}
/**
* Insert item x, without maintaining heap order.
* Duplicates are allowed.
*/
void toss(const Comparable & x)
{
checkSize();
array[++currentSize] = x;
orderOK = orderOK && array.at(currentSize/2) <= x;
}
/**
* Remove the minimum item and place it in minItem.
* Throws min_pq_error if empty.
*/
void deleteMin(Comparable & minItem)
{
if(isEmpty())
throw min_pq_error("deleteMin: tom heap");
minItem = findMin();
array[1] = array[currentSize--];
percolateDown(1);
}
void makeEmpty()
{ currentSize = 0; }
private:
int currentSize; // Number of elements in heap
bool orderOK; // True if heap order is guaranteed
vector<Comparable> array; // The heap array
/**
* Establish heap order property from an arbitrary
* arrangement of items. Runs in linear time.
*/
void fixHeap()
{
for(int i = currentSize / 2; i > 0; i--)
percolateDown(i);
orderOK = true;
}
/**
* Internal method to percolate down in the heap.
* hole is the index at which the percolate begins.
*/
void percolateDown(int hole)
{
int child;
Comparable tmp = array[hole];
for( ; hole * 2 <= currentSize; hole = child)
{
child = hole * 2;
if(child != currentSize && array[child + 1] < array[child])
child++;
if(array[child] < tmp)
array[hole] = array[child];
else
break;
}
array[hole] = tmp;
}
/**
* Doubles the heap array if full
*/
void checkSize()
{
if(currentSize == array.size() - 1)
array.resize(array.size() * 2);
}
};
#endif