-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.h
More file actions
243 lines (235 loc) · 5.17 KB
/
Heap.h
File metadata and controls
243 lines (235 loc) · 5.17 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#pragma once
#include<stddef.h>
#include<iostream>
#include<exception>
template <typename T>
void myownswap(T& a, T& b) {
T c = a;
a = b;
b = c;
}
template <typename T>
T min(T a, T b) {
if (a < b)
return a;
return b;
}
template <typename T>
struct Node {
T key;
Node *Parent, *Left_child, *Right_child;
Node(T x) : Parent(NULL), Left_child(NULL), Right_child(NULL), key(x) {}
~Node() {
if (Left_child != NULL)
delete Left_child;
if (Right_child != NULL)
delete Right_child;
}
};
template <typename T>
class Heap {
private:
Node<T> *Root, *Last;
int Size;
void save_heap_up(Node<T> *Cur) {
if (Cur == Root || Cur->Parent->key <= Cur->key)
return;
T save = Cur->key;
Cur->key = Cur->Parent->key;
Cur->Parent->key = save;
save_heap_up(Cur->Parent);
}
Node<T> save_heap_up_node(Node<T> *Cur) {
if (Cur == Root || Cur->Parent->key <= Cur->key)
return Cur;
T save = Cur->key;
Cur->key = Cur->Parent->key;
Cur->Parent->key = save;
return save_heap_up(Cur->Parent);
}
void save_heap_down(Node<T> *Cur) {
if (Cur->Left_child == NULL || Cur->key <= Cur->Left_child->key && Cur->Right_child == NULL || Cur->Right_child != NULL && Cur->key <= Cur->Right_child->key && Cur->key <= Cur->Left_child->key)
return;
if (Cur->key > Cur->Left_child->key && (Cur->Right_child == NULL || Cur->Right_child->key >= Cur->Left_child->key)) {
myownswap(Cur->key, Cur->Left_child->key);
save_heap_down(Cur->Left_child);
return;
}
myownswap(Cur->key, Cur->Right_child->key);
save_heap_down(Cur->Right_child);
}
Node<T> save_heap_down_node(Node<T> *Cur) {
if (Cur->Left_child == NULL || Cur->key <= Cur->Left_child->key && Cur->Right_child == NULL || Cur->Right_child != NULL && Cur->key <= Cur->Right_child->key && Cur->key <= Cur->Left_child->key)
return Cur;
if (Cur->key > Cur->Left_child->key && (Cur->Right_child == NULL || Cur->Right_child->key >= Cur->Left_child->key)) {
myownswap(Cur->key, Cur->Left_child->key);
return save_heap_down(Cur->Left_child);
}
myownswap(Cur->key, Cur->Right_child->key);
return save_heap_down(Cur->Right_child);
}
public:
Heap() : Root(NULL), Size(0), Last(NULL) {}
Heap(T x) : Root(*Node<T>(x)), Size(1) {
Last = Root;
}
int size() {
return Size;
}
bool is_empty() const {
return Size == 0;
}
void insert(T key) {
if (Root == NULL) {
Last = Root = new Node<T>(key);
++Size;
save_heap_up(Last);
return;
}
if (Last == Root) {
Last = new Node<T>(key);
Root->Left_child = Last;
Last->Parent = Root;
if (Root->key > Last->key)
myownswap(Root->key, Last->key);
++Size;
save_heap_up(Last);
return;
}
++Size;
int x = Size >> 1, cnt = -1;
while (x) {
++cnt;
x >>= 1;
}
x = Size;
Node<T> *Cur = Root;
while (cnt) {
if ((x >> cnt) & 1)
Cur = Cur->Right_child;
else
Cur = Cur->Left_child;
--cnt;
}
Last = new Node<T>(key);
if (x & 1)
Cur->Right_child = Last;
else
Cur->Left_child = Last;
Last->Parent = Cur;
save_heap_up(Last);
}
Node<T> insert_node(T key) {
if (Root == NULL) {
Last = Root = new Node<T>(key);
++Size;
return save_heap_up_node(Last);
}
if (Last == Root) {
Last = new Node<T>(key);
Root->Left_child = Last;
Last->Parent = Root;
if (Root->key > Last->key)
myownswap(Root->key, Last->key);
++Size;
return save_heap_up_node(Last);
}
++Size;
int x = Size >> 1, cnt = -1;
while (x) {
++cnt;
x >>= 1;
}
x = Size;
Node<T> *Cur = Root;
while (cnt) {
if ((x >> cnt) & 1)
Cur = Cur->Right_child;
else
Cur = Cur->Left_child;
--cnt;
}
Last = new Node<T>(key);
if (x & 1)
Cur->Right_child = Last;
else
Cur->Left_child = Last;
Last->Parent = Cur;
return save_heap_up_node(Last);
}
T get_min() const {
try {
if (is_empty()) {
throw std::out_of_range("the heap is empty, can't get_min\n");//ошибка если куча пуста
}
else {
return Root->key;
}
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
return NULL;
}
}
T extract_min() {
try {
if (is_empty()) {
throw std::out_of_range("the heap is empty, can't get_min\n");//ошибка если куча пуста
}
else {
T Min_key = Root->key;
if (Root == Last) {
delete Root;
Root = Last = NULL;
--Size;
return Min_key;
}
Root->key = Last->key;
if (Last->Parent->Right_child == NULL)
Last->Parent->Left_child = NULL;
else
Last->Parent->Right_child = NULL;
delete Last;
//Last = NULL;
--Size;
int x = Size >> 1, cnt = -1;
while (x) {
++cnt;
x >>= 1;
}
x = Size;
Node<T> *Cur = Root;
while (cnt >= 0) {
if ((x >> cnt) & 1)
Cur = Cur->Right_child;
else
Cur = Cur->Left_child;
--cnt;
}
Last = Cur;
save_heap_down(Root);
return Min_key;
}
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
return NULL;
}
}
void del(Node<T> x) {
int save = x->key;
x->key = Root->key;
Root->key = save;
H.extract_min();
H.save_heap_up(x);
}
void change(Node<T> x, T k) {
x->key = k;
H.save_heap_down(x);
H.save_heap_up(x);
}
~Heap() {
if (Root != NULL)
delete Root;
}
};