-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpqueue-heap.cpp
125 lines (112 loc) · 2.97 KB
/
pqueue-heap.cpp
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
/*************************************************************
* File: pqueue-heap.cpp
*
* Implementation file for the HeapPriorityQueue
* class.
*/
#include "pqueue-heap.h"
#include "error.h"
HeapPriorityQueue::HeapPriorityQueue() {
capacity=INITIAL_CAPACITY;
array = new string[capacity];
head=0;
tail = 0;
}
HeapPriorityQueue::~HeapPriorityQueue() {
delete [] array;
}
int HeapPriorityQueue::size() {
return (tail+capacity-head) % capacity;
}
bool HeapPriorityQueue::isEmpty() {
return (head==tail);
}
void HeapPriorityQueue::enqueue(string value) {
if (head==tail){
array[head]=value;
tail++;
} else {
if (size()==capacity-1) expandCapacity();
array[tail]=value;
bubbleUp(tail);
tail++;
}
}
string HeapPriorityQueue::peek() {
if (isEmpty()) error("Attempting to peek an empty queue");
return array[head];
}
string HeapPriorityQueue::dequeueMin() {
if (isEmpty()) error("Attempting to dequeue an empty queue");
string result = array[head];
swap(array[head], array[tail-1]);
deleteTail();
bubbleDown(head);
return result;
}
void HeapPriorityQueue::expandCapacity() {
int count = size();
string *oldArray = array;
array = new string[2*capacity];
for (int i = 0; i < count; i++){
array[i] = oldArray[i];
}
capacity *=2;
head=0;
tail=count;
delete [] oldArray;
}
void HeapPriorityQueue::deleteTail(){
string *oldArray = array;
array = new string[capacity];
for (int i = 0; i < tail; i++){
array[i] = oldArray[i];
}
tail--;
delete [] oldArray;
}
void HeapPriorityQueue::bubbleUp(int child){
if (child!=0){
int parent;
if (child % 2==0){
parent = (child-2)/2;
} else {
parent = (child-1)/2;
}
if (array[child]<array[parent]){
swap(array[parent],array[child]);
bubbleUp(parent);
}
}
}
void HeapPriorityQueue::bubbleDown(int parent){
int child1 = 2*parent+1;
int child2 = 2*parent+2;
// CASE 1: CHILD1 < TAIL AND CHILD2 < TAIL
if(child1<tail&&child2<tail){
if (array[child1]<array[child2]){
if (array[parent]>array[child1]){
swap(array[parent], array[child1]);
bubbleDown(child1);
}
} else if (array[parent]>array[child2]){
swap(array[parent], array[child2]);
bubbleDown(child2);
}
}
// CASE 2: CHILD1 < TAIL BUT CHILD2>= TAIL
if(child1<tail&&child2>=tail){
if (array[parent]>array[child1]){
swap(array[parent], array[child1]);
bubbleDown(child1);
}
}
// CASE 3: CHILD2< TAIL BUT CHILD1>= TAIL
if(child2<tail&&child1>=tail){
if (array[parent]>array[child2]){
swap(array[parent], array[child2]);
bubbleDown(child2);
}
}
// CASE 4: CHILD1>= TAIL AND CHILD2>= TAIL
}