-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityqueue.cpp
131 lines (117 loc) · 2.64 KB
/
priorityqueue.cpp
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
//using binary heap, a minimum heap
#include <iostream>
class priorityqueue;
class heap
{
int* nodes;
int size;
int lastindex;
public:
heap()
{
size = 1;
nodes = new int[size];
lastindex = -1;
}
void swap(int index1, int index2)
{
int temp = index1;
nodes[index1] = nodes[index2];
nodes[index2] = nodes[temp];
}
int parentnode(int index)
{
if(index == 0 || index == 1 || index == 2) return 0;
return index / 2;
}
int leftchild(int index)
{
int leftchild;
if(index == 0) leftchild = 1;
else leftchild = index * 2;
return (leftchild <= lastindex)? leftchild : -1;
}
int rightchild(int index)
{
int rightchild;
if(index == 0) rightchild = 2;
else rightchild = index * 2 + 1;
return (rightchild <= lastindex)? rightchild : -1;
}
void shiftup()
{
int minindex = lastindex;
int parentindex;
while(minindex > 0)
{
parentindex = parentnode(minindex);
if(nodes[parentindex] < nodes[minindex] )
{
swap(parentindex, minindex);
minindex = parentindex;
continue;
}
break;
}
}
void shiftdown()
{
int minindex = 0;
while(minindex <= lastindex)
{
int left = leftchild(minindex);
int right = rightchild(minindex);
int child = left <= right? left : right;
if(child <= lastindex && nodes[child] < nodes[minindex])
{
swap(child, minindex);
minindex = child;
continue;
}
return;
}
}
friend class priorityqueue;
};
class priorityqueue
{
heap h;
public:
void enqueue(int data)
{
if(h.lastindex == h.size - 1)
{
int* temp = h.nodes;
h.nodes = new int[h.size * 2];
for(int i = 0; i< h.size - 1; i++)
{
h.nodes[i] = temp[i];
}
h.size *= 2;
}
h.lastindex++;
h.nodes[h.lastindex] = data;
h.shiftup();
}
int dequeue()
{
int popped = h.nodes[0];
h.nodes[0] = h.nodes[h.lastindex];
h.lastindex--;
h.shiftdown();
return popped;
}
};
int main()
{
priorityqueue q;
q.enqueue(5);
q.enqueue(15);
q.enqueue(10);
q.enqueue(10);
q.enqueue(67);
q.enqueue(50);
std::cout<<q.dequeue()<<std::endl;
std::cout<<q.dequeue()<<std::endl;
return 0;
}