-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cpp
More file actions
204 lines (175 loc) · 4.77 KB
/
PriorityQueue.cpp
File metadata and controls
204 lines (175 loc) · 4.77 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef _PRIORITY_QUEUE_CPP
#define _PRIORITY_QUEUE_CPP
#include "PriorityQueue.h"
#include <iostream>
#include <stdexcept>
template<class T, class Comp>
PriorityQueue<T, Comp>::PriorityQueue(PriorityQueue &other)
{
root = copyHelper(other.root);
}
template<class T, class Comp>
PriorityQueue<T,Comp> &PriorityQueue<T, Comp>::operator =(PriorityQueue &other)
{
if(&other != this)
{
clearHelper(root);
root = copyHelper(other.root);
}
return *this;
}
template <class T, class Comp>
void PriorityQueue<T,Comp>::push(T &data)
{
numElements++;
if(root == nullptr)
{
root = new Node {data, true, nullptr, nullptr};
return;
}
else
insert(data, root);
}
template <class T, class Comp>
void PriorityQueue<T,Comp>::insert(T &data, Node *&at)
{
Node **curr = &at;
Comp compare;
//Търсим елемент, който чупи условието compare
while(*curr != nullptr && compare(data, (*curr)->data))
{
if((*curr)->side)
{
(*curr)->side = false;
curr = &(*curr)->left;
}
else
{
(*curr)->side = true;
curr = &(*curr)->right;
}
}
//Намерили сме и този елемент е листо. Можем да го запишем там
if((*curr) == nullptr)
{
*curr = new Node {data, true, nullptr, nullptr};
}
else //Трябва да се разместват елементи. Спрямо това дали завиваме наляво или надясно се пуска рекурсивно надолу разместването
{
T temp = (*curr)->data;
(*curr)->data = data;
if((*curr)->side)
{
(*curr)->side = false;
insert(temp, (*curr)->left);
}
else
{
(*curr)->side = true;
insert(temp, (*curr)->right);
}
}
}
template <class T, class Comp>
T &PriorityQueue<T,Comp>::top()
{
if(root == nullptr) throw std::runtime_error("Cannot get top on empty queue");
return root->data;
}
template <class T, class Comp>
void PriorityQueue<T,Comp>::pop()
{
if(root == nullptr) throw std::runtime_error("Cannot pop empty queue");
numElements--;
popHelper(root);
}
template <class T, class Comp>
void PriorityQueue<T,Comp>::popHelper(Node *&curr)
{
Comp compare;
if(curr->left == nullptr && curr->right == nullptr) //Листо
{
delete curr;
curr = nullptr;
}
else if(curr->left == nullptr) //Лявото поддърво е празно. Можем да заменим с дясното
{
Node *temp = curr->right;
delete curr;
curr = temp;
}
else if(curr->right == nullptr) //Дясното поддърво е празно. Можем да заменим с лявото
{
Node *temp = curr->left;
delete curr;
curr = temp;
}
else //И двете дървета имат нещо. Трябва да проверим по кой път да тръгнем
{
if(compare(curr->right->data, curr->left->data))
{
curr->data = curr->left->data;
popHelper(curr->left);
}
else
{
curr->data = curr->right->data;
popHelper(curr->right);
}
}
}
template <class T, class Comp>
void PriorityQueue<T,Comp>::print()
{
//BFS print with sentinels
std::queue<Node *> q;
q.push(root);
q.push(nullptr);
while(!q.empty())
{
Node *curr = q.front();
q.pop();
if(curr == nullptr)
{
std::cout << std::endl;
if(!q.empty()) q.push(nullptr);
}
else
{
std::cout << curr->data << " ";
if(curr->left != nullptr)
q.push(curr->left);
if(curr->right != nullptr)
q.push(curr->right);
}
}
}
template <class T, class Comp>
size_t PriorityQueue<T,Comp>::size()
{
return numElements;
}
template <class T, class Comp>
void PriorityQueue<T,Comp>::clearHelper(PriorityQueue<T,Comp>::Node *curr)
{
if(curr != nullptr)
{
clearHelper(curr->left);
clearHelper(curr->right);
delete curr;
}
}
template <class T, class Comp>
typename PriorityQueue<T,Comp>::Node *PriorityQueue<T,Comp>::copyHelper(PriorityQueue<T,Comp>::Node *other)
{
if(other == nullptr) return nullptr;
Node *left = copyHelper(other->left);
Node *right = copyHelper(other->right);
return new Node {other->priority, other->data, left, right};
}
template <class T, class Comp>
PriorityQueue<T,Comp>::~PriorityQueue()
{
clearHelper(root);
}
#endif