-
Notifications
You must be signed in to change notification settings - Fork 2
/
AVLTree_Node.h
394 lines (361 loc) · 8.58 KB
/
AVLTree_Node.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#pragma once
#include "SimpleAllocate.h"
namespace TinySTL
{
template<class T>
struct AVLTree_Node
{
typedef T value_type;
typedef size_t size_type;
typedef AVLTree_Node<T> * node_ptr;
int height;
value_type key;
node_ptr _left;
node_ptr _right;
node_ptr _parent;
AVLTree_Node()
{
_left = nullptr;
_right = nullptr;
_parent = nullptr;
}
};
template<class T>
int Height(AVLTree_Node<T> *r)
{
if (r==nullptr)
return -1;
return r->height;
}
template<class T>
T max(T a, T b)
{
if (a > b)
return a;
return b;
}
template<class T>
T abs(T a)
{
if (a < 0)
return -a;
return a;
}
template<class T>
AVLTree_Node<T> * singleRotateLeft(AVLTree_Node<T>* root);
template<class T>
AVLTree_Node<T> * singleRotateRight(AVLTree_Node<T>*root);
template<class T>
AVLTree_Node<T>* doubleRotateLeft(AVLTree_Node<T>*root);
template<class T>
AVLTree_Node<T>* doubleRotatoRight(AVLTree_Node<T>*root);
template<class T>
AVLTree_Node<T>* insertAVLTree(AVLTree_Node<T> *&root, AVLTree_Node<T> *parent, T value);
template<class T>
AVLTree_Node<T>* findMin(AVLTree_Node<T> * root);
template<class T>
AVLTree_Node<T>* Fix(AVLTree_Node<T> *root);
template<class T>
AVLTree_Node<T>* deleteAVLNode(AVLTree_Node<T> *&root, AVLTree_Node<T>*parent, T value);
template<class T>
void MidTraverse(AVLTree_Node<T> *root);
template<class T>
AVLTree_Node<T> *findNode(AVLTree_Node<T> *root, T value);
template<class T>
AVLTree_Node<T>* findNext(AVLTree_Node<T>*root);
template<class T>
AVLTree_Node<T>* findPre(AVLTree_Node<T>*root);
template<class T>
AVLTree_Node<T> * singleRotateLeft(AVLTree_Node<T>* root)
{
typedef AVLTree_Node<T> * node_ptr;
node_ptr tmp = root->_left; // 旋转的部分
root->_left = tmp->_right;
tmp->_right->_parent = root;
tmp->_right = root;
root->_parent = tmp;
root->height = max<int>(Height(root->_left), Height(root->_right))+1;
tmp->height = max<int>(Height(tmp->_left), Height(tmp->_right))+1;
return tmp;
}
template<class T>
AVLTree_Node<T> * singleRotateRight(AVLTree_Node<T>*root)
{
typedef AVLTree_Node<T> * node_ptr;
node_ptr tmp = root->_right;
root->_right = tmp->_left;
tmp->_left->_parent = root;
tmp->_left = root;
root->_parent = tmp;
root->height = max<int>(Height(root->_left), Height(root->_right))+1;
tmp->height = max<int>(Height(tmp->_left), Height(tmp->_right))+1;
return tmp;
}
template<class T>
AVLTree_Node<T>* doubleRotateLeft(AVLTree_Node<T>*root)
{
root->_left = singleRotateRight(root->_left);
root->_left->_parent = root;
return singleRotateLeft(root);
}
template<class T>
AVLTree_Node<T>* doubleRotatoRight(AVLTree_Node<T>*root)
{
root->_right = singleRotateLeft(root->_right);
root->_right->_parent = root;
return singleRotateRight(root);
}
template<class T>
AVLTree_Node<T>* insertAVLTree(AVLTree_Node<T> *&root, AVLTree_Node<T> *parent, T value)
{
if (root == nullptr)
{
root = new AVLTree_Node<T>();
root->key = value;
root->_parent = parent;
}
else
{
if (root->key > value)
{
root->_left = insertAVLTree(root->_left, root, value);
root->_left->_parent = root;
if (Height(root->_left) - Height(root->_right) >= 2)
{
if (root->_left->key < value)
{
root = doubleRotateLeft(root);
root->_parent = parent;
}
else
{
root = singleRotateLeft(root);
root->_parent = parent;
}
}
}
else if (root->key < value)
{
root->_right = insertAVLTree(root->_right, root, value);
root->_right->_parent = root;
if (Height(root->_right) - Height(root->_left) >= 2)
{
if (root->_right->key < value)
{
root = doubleRotatoRight(root);
root->_parent = parent;
}
else
{
root = singleRotateRight(root);
root->_parent = parent;
}
}
}
else
return root;
}
root->height = max<int>(Height(root->_left), Height(root->_right)) + 1;
return root;
}
template<class T>
AVLTree_Node<T>* findMin(AVLTree_Node<T> * root)
{
if (root != nullptr)
{
while (root->_left != nullptr)
root = root->_left;
}
return root;
}
template<class T>
AVLTree_Node<T>* Fix(AVLTree_Node<T>* root)
{
if (Height(root->_left) > Height(root->_right))
{
//左儿子的左儿子的高度大于左儿子的右儿子的高度, 执行左单旋转, 否则执行左-右双旋转
if (Height(root->_left->_left) > Height(root->_left->_right))
root = singleRotateLeft(root);
else if (Height(root->_left->_left) < Height(root->_left->_right))
root = doubleRotatoRight(root);
}
else if (Height(root->_left) < Height(root->_right))
{
//右儿子的右儿子的高度大于右儿子的左儿子的高度, 执行右单旋转, 否则执行右-左双旋转
if (Height(root->_right->_right) > Height(root->_right->_left))
root = singleRotateRight(root);
else if (Height(root->_right->_right) < Height(root->_right->_left))
root = doubleRotatoRight(root);
}
return root;
}
template<class T>
AVLTree_Node<T>* deleteAVLNode(AVLTree_Node<T> *&root, AVLTree_Node<T>*parent, T value)
{
AVLTree_Node<T> * ptr;
if (root == nullptr)
return nullptr;
// 找到当前节点
if (root->key == value)
{
if (root->_left && root->_right)
{
ptr = findMin(root->_right);
root->key = ptr->key;
root->_right = deleteAVLNode(root->_right, root, root->key);
}
else
{
ptr = root;
if (root->_left != nullptr)
{
root = root->_left;
root->_parent = parent;
}
else if (root->_right != nullptr)
{
root = root->_right;
root->_parent = parent;
}
else
{
root = nullptr;
// 这时候需要把root置nullptr,否则指向的内存以及被释放了会出错
}
delete ptr;
ptr = nullptr;
}
}
else
{
if (root->key < value)
root->_right = deleteAVLNode(root->_right, root, value);
else
root->_left = deleteAVLNode(root->_left, root, value);
}
if (root)
{
root->height = max<int>(Height(root->_left), Height(root->_right)) + 1;
if (abs<int>(Height(root->_left) - Height(root->_right))>=2)
{
root = Fix(root);
root->_parent = parent;
root->height = max<int>(Height(root->_left), Height(root->_right)) + 1;
}
}
return root;
}
template<class T>
void MidTraverse(AVLTree_Node<T> *root)
{
if (root == nullptr)
return;
MidTraverse(root->_left);
cout << root->key << " ";
MidTraverse(root->_right);
}
template<class T>
AVLTree_Node<T> *findNode(AVLTree_Node<T> *root, T value)
{
if (root == nullptr)
return nullptr;
if (value == root->key)
return root;
if (value < root->key)
return findNode(root->_left, value);
else
return findNode(root->_right, value);
}
template<class T>
AVLTree_Node<T>* findNext(AVLTree_Node<T>*root)
{
if (root == nullptr)
return root;
if (root->_right != nullptr)
{
// 找到最左节点
root = root->_right;
while (root->_left)
root = root->_left;
return root;
}
else
{
// 找到是父节点的左子节点的第一个
while (root->_parent && root->_parent->_right == root)
root = root->_parent;
return root->_parent;
}
}
template<class T>
AVLTree_Node<T>* findPre(AVLTree_Node<T>*root)
{
if (root == nullptr)
return root;
if (root->_left != nullptr)
{
// 找到最右节点
root = root->_left;
while (root->_right)
root = root->_right;
return root;
}
else
{
while (root->_parent && root->_parent->_left == root)
root = root->_parent;
return root->_parent;
}
}
template<class T>
struct AVLTree_Node_Iterator
{
typedef T value_type;
typedef AVLTree_Node<T> * node_ptr;
typedef AVLTree_Node<T> node_type;
AVLTree_Node_Iterator<T> operator++(int); // 后置
AVLTree_Node_Iterator<T> operator++();
AVLTree_Node_Iterator<T> operator--(int);
AVLTree_Node_Iterator<T> operator--();
AVLTree_Node_Iterator(node_ptr it) :ptr(it) {}
node_ptr ptr;
bool operator ==(const AVLTree_Node_Iterator<T> & a)
{
return ptr == a.ptr;
}
bool operator !=(const AVLTree_Node_Iterator<T> & a)
{
return ptr != a.ptr;
}
value_type operator*()
{
return ptr->key;
}
};
template<class T>
AVLTree_Node_Iterator<T> AVLTree_Node_Iterator<T>::operator++(int)
{
auto res = ptr;
ptr = findNext(ptr);
return AVLTree_Node_Iterator<T>(res);
}
template<class T>
AVLTree_Node_Iterator<T> AVLTree_Node_Iterator<T>::operator++()
{
ptr = findNext(ptr);
return *this;
}
template<class T>
AVLTree_Node_Iterator<T> AVLTree_Node_Iterator<T>::operator--(int)
{
auto res = ptr;
ptr = findPre(ptr);
return AVLTree_Node_Iterator<T>(res);
}
template<class T>
AVLTree_Node_Iterator<T> AVLTree_Node_Iterator<T>::operator--()
{
ptr = findPre(ptr);
return *this;
}
}