-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathpriority_queue.cc
More file actions
174 lines (136 loc) · 3.92 KB
/
Copy pathpriority_queue.cc
File metadata and controls
174 lines (136 loc) · 3.92 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "priority_queue.h"
namespace jw {
PriorityQueue::PriorityQueue(const int size) : capacity_(size) {
assert(size > 0 && size < INT_MAX);
elements_ = new PQElement[size];
}
PriorityQueue::~PriorityQueue() { delete[] elements_; }
void PriorityQueue::Insert(const int key, const std::string value) {
assert(size_ < capacity_);
elements_[size_].key_ = key;
elements_[size_].value_ = value;
++size_;
SiftUp(size_ - 1);
}
void PriorityQueue::SiftUp(int index) {
while (index > 0) {
int parent = (index - 1) / 2;
if (elements_[parent].key_ < elements_[index].key_) {
Swap(parent, index);
} else {
break;
}
index = parent;
}
}
void PriorityQueue::Swap(const int index1, const int index2) {
int temp_key = elements_[index1].key_;
std::string temp_value = elements_[index1].value_;
elements_[index1].key_ = elements_[index2].key_;
elements_[index1].value_ = elements_[index2].value_;
elements_[index2].key_ = temp_key;
elements_[index2].value_ = temp_value;
}
void PriorityQueue::PrintDebug() {
for (int i = 0; i < size_; ++i) {
std::cout << i << ":" << elements_[i].key_ << ":" << elements_[i].value_
<< std::endl;
}
std::cout << "--------" << std::endl;
}
PQElement* PriorityQueue::GetMax() {
assert(size_ > 0);
return &(elements_[0]);
}
int PriorityQueue::GetSize() { return size_; }
bool PriorityQueue::IsEmpty() { return size_ == 0; }
PQElement* PriorityQueue::ExtractMax() {
assert(size_ > 0);
PQElement* max = new PQElement;
max->key_ = elements_[0].key_;
max->value_ = elements_[0].value_;
Swap(0, size_ - 1);
--size_;
SiftDown(0);
return max;
}
void PriorityQueue::SiftDown(int index) {
while (index * 2 + 1 < size_) {
int left_child_index = index * 2 + 1;
int right_child_index = index * 2 + 2;
bool has_left_child = left_child_index < size_;
bool has_right_child = right_child_index < size_;
int swap_index = index;
if (has_left_child && has_right_child) {
if (elements_[left_child_index].key_ >
elements_[right_child_index].key_) {
swap_index = left_child_index;
} else { // greater or equal to right child
swap_index = right_child_index;
}
} else if (has_left_child) {
swap_index = left_child_index;
} else if (has_right_child) {
swap_index = right_child_index;
} else {
break;
}
if (elements_[swap_index].key_ > elements_[index].key_) {
Swap(swap_index, index);
index = swap_index;
} else {
break;
}
}
}
void PriorityQueue::Remove(int index) {
assert(index >= 0 && index < size_);
Swap(index, size_ - 1);
--size_;
SiftDown(index);
}
void heapify(int* numbers, int count) {
for (int i = count / 2 - 1; i >= 0; --i) {
percolate_down(numbers, count, i);
}
}
void heap_sort(int* numbers, int count) {
int temp;
for (int i = count - 1; i > 0; --i) {
temp = numbers[i];
numbers[i] = numbers[0];
numbers[0] = temp;
percolate_down(numbers, i, 0);
}
}
void percolate_down(int* numbers, int count, int index) {
while (index * 2 + 1 < count) {
int swap_index = index;
int left_child_index = index * 2 + 1;
int right_child_index = index * 2 + 2;
bool has_left_child = left_child_index < count;
bool has_right_child = right_child_index < count;
if (has_left_child && has_right_child) {
if (numbers[left_child_index] > numbers[right_child_index]) {
swap_index = left_child_index;
} else {
swap_index = right_child_index;
}
} else if (has_left_child) {
swap_index = left_child_index;
} else if (has_right_child) {
swap_index = right_child_index;
} else {
break;
}
if (numbers[swap_index] > numbers[index]) {
int temp = numbers[index];
numbers[index] = numbers[swap_index];
numbers[swap_index] = temp;
index = swap_index;
} else {
break;
}
}
}
} // namespace jw