-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.hpp
More file actions
136 lines (112 loc) · 3.83 KB
/
Copy pathpriority_queue.hpp
File metadata and controls
136 lines (112 loc) · 3.83 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
#ifndef priority_queue_H
#define priority_queue_H
#include "vector.hpp"
#include <functional>
template <typename T, typename Compare = std::greater<T>>
class priority_queue{
// left_child : 2i+1
// right_chile : 2i+2
// parent : (i-1)//2
private:
vector<T> heap;
Compare comp;
void swap_and_update_arg1_to_arg2(int& pos1, int& pos2){
T swap_ = heap[pos1];
heap[pos1] = heap[pos2];
heap[pos2] = swap_;
pos1 = pos2;
}
public:
priority_queue(Compare c = Compare()) : comp(c){}
~priority_queue(){}
priority_queue(const priority_queue& other){
heap = other.heap;
}
priority_queue(priority_queue&& other) noexcept {
if(&other!=this){
this->heap = std::move(other.heap);
vector<T> empty_vector;
other.heap = empty_vector;
}
}
priority_queue& operator=(const priority_queue& other){
heap = other.heap;
return *this;
}
priority_queue& operator=(priority_queue&& other){
if(&other!=this){
this->heap = std::move(other.heap);
vector<T> empty_vector;
other.heap = empty_vector;
}
return *this;
}
void push(T element){
heap.push_back(element);
int curr_index = heap.size()-1;
while (curr_index > 0) {
int parent_index = (curr_index - 1) / 2;
if (comp(heap[curr_index], heap[parent_index])) {
swap_and_update_arg1_to_arg2(curr_index, parent_index);
}
else break;
}
}
const T& top() const {
if(heap.empty()) throw std::runtime_error("Tried to peek into an empty priority_queue: priority_queue.hpp\n");
return heap[0];
}
T pop(){
if(heap.empty()) throw std::runtime_error("Tried to pop an empty priority_queue: priority_queue.hpp\n");
T popped_element = heap[0];
heap[0] = heap[heap.size()-1];
heap.pop_back();
int curr_index = 0;
while(curr_index<heap.size()){
int child_left_index = (curr_index*2)+1;
int child_right_index = (curr_index*2)+2;
if(child_left_index<heap.size() && child_right_index<heap.size()){
bool left_greater = comp(heap[child_left_index],heap[curr_index]);
bool right_greater = comp(heap[child_right_index],heap[curr_index]);
if(!left_greater && !right_greater){
break;
}
else if(left_greater && !right_greater){
swap_and_update_arg1_to_arg2(curr_index, child_left_index);
}
else if(right_greater && !left_greater){
swap_and_update_arg1_to_arg2(curr_index, child_right_index);
}
else{
if(comp(heap[child_left_index],heap[child_right_index])){
swap_and_update_arg1_to_arg2(curr_index, child_left_index);
}
else{
swap_and_update_arg1_to_arg2(curr_index, child_right_index);
}
}
}
else if(child_left_index>=heap.size() && child_right_index>=heap.size()){
break;
}
else{
int child_index = (child_left_index>=heap.size()) ? child_right_index : child_left_index;
bool child_greater = comp(heap[child_index],heap[curr_index]);
if(child_greater){
swap_and_update_arg1_to_arg2(curr_index, child_index);
}
else{
break;
}
}
}
return popped_element;
}
bool empty() const {
return heap.empty();
}
int size(){
return heap.size();
}
};
#endif