-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.h
163 lines (110 loc) · 3.53 KB
/
PriorityQueue.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
159
160
#pragma once
#include"PriorityKey.h"
#include"UI.h"
const int MAX_ARRAY = 1000;
#include<exception>
template<typename T>
class PriorityQueue
{
private:
PriorityKey<T>** arr;
const int capacity;
int size;
void bubbleUP();
int minChild(int index);
void bubbleDown();
void swap(PriorityKey<T>*& val1, PriorityKey<T>*& val2);
public:
PriorityQueue(int s = MAX_ARRAY);
void enqueue(T& val, int key);
bool dequeue();
T& peekFront() ;
int getSize() const { return size; };
void Print(UI * ,char seperator = ',') const;
bool isEmpty() const;
~PriorityQueue();
};
template<typename T>
PriorityQueue<T>::PriorityQueue(int s) : capacity(s), size(0) {
arr = new PriorityKey<T> *[capacity];
}
template<typename T>
void PriorityQueue<T>::enqueue(T& val, int key) {
if (size == capacity) throw std::exception("Piority Queue is FULL");
PriorityKey<T>* newHeap = new PriorityKey<T>(val, key);
arr[size++] = newHeap;//add new node in heap at most left leve
bubbleUP();//swap up till best position
}
template<typename T>
bool PriorityQueue<T>::dequeue() {
if (size == 0) throw std::exception("Piority Queue is Empty");
swap(arr[0], arr[size - 1]); //swap between root[least node]
delete arr[size - 1]; //delete the least node
size--;
bubbleDown();//swap down till best position
return true;
}
template<typename T>
T& PriorityQueue<T>::peekFront() {
if (size == 0) throw std::exception("Piority Queue is Empty");
return arr[0]->getValue();
}
template<typename T>
void PriorityQueue<T>::Print(UI* ptr, char seperator) const {
if (size == 0) return;
for (int i = 0; i < size - 1; i++) {
ptr->Print(arr[i]->getValue(), seperator);
}
ptr->Print(arr[size - 1]->getValue(), '\0');
}
template<typename T>
bool PriorityQueue<T>::isEmpty() const
{
return (size==0);
}
template<typename T>
PriorityQueue<T>::~PriorityQueue() {
for (int i = 0; i < size; i++) delete arr[i];
}
//////////////////////////////////////////////////////////////////////////////////////
//...............................Private Functions.................................//
/////////////////////////////////////////////////////////////////////////////////////
template<typename T>
void PriorityQueue<T>::bubbleUP() {
int index = size - 1;
int parent = (index - 1) / 2;
//swap up till
//be parent of all nodes
//or it's parent be smaller than it
while (index > 0 && arr[index]->getKey() < arr[parent]->getKey()) { ///Change Here if you Want MAX HEAP
swap(arr[index], arr[parent]);
index = parent;
parent = (index - 1) / 2;
}
}
template<typename T>
void PriorityQueue<T>::bubbleDown() {
int index = 0;
//get [Right || Left] child of parent Node depend on how is minimum
int minchild = minChild(index); // get smaller child of index
while (arr[minchild]->getKey() < arr[index]->getKey()) { // check if it's valid
swap(arr[minchild], arr[index]); // swap if index not a valid parent
index = minchild;
minchild = minChild(index);
}
}
template<typename T>
int PriorityQueue<T>::minChild(int index) { // return same index if no child
//2 * index + 1 = [ left child ]
//2 * index + 2 = [ right child ]
if (2 * index + 1 >= size) return index; // no left child => no child
///Change Here if you Want MAX HEAP
if (2 * index + 2 >= size) return 2 * index + 1; // if no right child
else return (arr[2 * index + 2]->getKey() < arr[2 * index + 1]->getKey()) ? 2 * index + 2 : 2 * index + 1;
};
template<typename T>
void PriorityQueue<T>::swap(PriorityKey<T>*& val1, PriorityKey<T>*& val2) {
PriorityKey<T>* temp = val1;
val1 = val2;
val2 = temp;
}