-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.cpp
143 lines (137 loc) · 3.12 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
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <vector>
using namespace std;
typedef float datatype;
#define LEFT(x) (x<<1)
#define RIGHT(x) (x<<1)+1
#define PARENT(x) (x>>1)
const float min_num = -100000.0;
class Heap
{
private:
vector<datatype> data;
decltype(data.size()) datalength;
public:
Heap(vector<datatype> tempdata, decltype(data.size())templength);
void showdata()
{
for(auto &i : data)
cout << i << endl;
}
void max_heap(vector<datatype> &tempdata, decltype(data.size()) i);
void swap_data(decltype(data.size()) i, decltype(data.size()) j)
{
float temp_swap = data[i];
data[i] = data[j];
data[j] = temp_swap;
}
void build_max_heap();
friend class PriorityQueue;
};
Heap::Heap(vector<datatype> tempdata, decltype(data.size())templength)
{
data = tempdata;
datalength = templength;
}
void Heap::max_heap(vector<datatype> &tempdata, decltype(data.size())i)
{
decltype(data.size()) left_index = LEFT(i)-1;
decltype(data.size()) right_index = RIGHT(i)-1;
float largest = tempdata[i-1];
decltype(data.size()) key = i-1;
if(datalength >= 2*i && largest < tempdata[left_index])
{
largest = tempdata[left_index];
key = left_index;
}
if(datalength >= 2*i+1 && largest < tempdata[right_index])
{
largest = tempdata[right_index];
key = right_index;
}
if(key != i-1)
{
swap_data(i-1, key);
key++;
max_heap(tempdata, key);
}
}
void Heap::build_max_heap()
{
for(decltype(data.size()) j = (datalength)/2; j != 0; --j)
max_heap(data, j);
}
class PriorityQueue
{
private:
Heap *heap;
public:
PriorityQueue(Heap *data);
void Insert(datatype x);
datatype MaxMum();
void Extract_Max();
void Increase_key(datatype x, int k);
void showqueue()
{
heap -> showdata();
}
};
PriorityQueue::PriorityQueue(Heap *data)
{
heap = data;
}
datatype PriorityQueue::MaxMum()
{
if(heap->datalength < 1)
{
cout << "empty" << endl;
return -1;
}
else
return heap->data[0];
}
void PriorityQueue::Extract_Max()
{
if(heap->datalength < 1)
cout << "empty" << endl;
else
{
heap->swap_data(0, heap->datalength-1);
(heap->datalength)--;
heap->max_heap(heap->data, 1);
}
}
void PriorityQueue::Increase_key(datatype x, int k)
{
if(x < heap->data[k-1])
cout << "too small" << endl;
else
{
heap->data[k-1] = x;
while(k > 1 && heap->data[k-1] > heap->data[PARENT(k)-1])
{
heap->swap_data(k-1, PARENT(k)-1);
k = PARENT(k);
}
}
}
void PriorityQueue::Insert(datatype x)
{
(heap->datalength)++;
heap->data.push_back(min_num);
Increase_key(x, heap->datalength);
}
int main(int args, char *argv[])
{
vector<float> a = {1, 2, 3, 4, 5};
Heap myheap(a, a.size());
myheap.build_max_heap();
PriorityQueue myqueue(&myheap);
myqueue.showqueue();
cout << myqueue.MaxMum() << endl;
myqueue.Insert(3.4);
myqueue.showqueue();
myqueue.Insert(12);
myqueue.showqueue();
return 0;
}