-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cpp
More file actions
87 lines (76 loc) · 1.35 KB
/
PriorityQueue.cpp
File metadata and controls
87 lines (76 loc) · 1.35 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
#include "PriorityQueue.h"
PriorityQueue::PriorityQueue(unsigned int capacity)
{
capacity_ = capacity;
heap_ = new Share[capacity_ + 1];
size_ = 0;
}
PriorityQueue::~PriorityQueue()
{
delete [] heap_;
heap_ = NULL;
}
unsigned int PriorityQueue::size() const
{
return size_;
}
bool PriorityQueue::empty() const
{
if (size_ == 0)
return true;
else
return false;
}
bool PriorityQueue::full() const
{
if (size_ == capacity_)
return true;
else
return false;
}
Share PriorityQueue::max() const
{
return heap_[1];
}
void PriorityQueue::enqueue(Share val)
{
heap_[size_ + 1] = val;
size_++;
int i = size_;
Share temp;
while (heap_[i / 2].getFactor() <= heap_[i].getFactor() && i != 1)
{
temp = heap_[i];
heap_[i] = heap_[i / 2];
heap_[i / 2] = temp;
i = i / 2;
}
}
void PriorityQueue::dequeue()
{
heap_[1] = heap_[size_];
size_--;
int i = 1;
int childNode = 0;
Share maxChild;
Share temp;
while (2 * i <= size_)
{
childNode = 2 * i;
maxChild = heap_[childNode];
if (heap_[childNode + 1].getFactor() > maxChild.getFactor())
{
childNode++;
maxChild = heap_[childNode];
}
if (heap_[i].getFactor() < maxChild.getFactor())
{
temp = heap_[i];
heap_[i] = maxChild;
heap_[childNode] = temp;
i = childNode;
}
else
break;
}
}