-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinnode.h
357 lines (304 loc) · 8.96 KB
/
binnode.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
#pragma once
#include"stack.h"
#include"queue.h"
namespace mystl {
#define BinNodePosi(T) BinNode<T>*
#define stature(p) ((p) ? (p)->height:-1) // 节点高度
typedef enum { RB_RED, RB_BLACK } RBColor; // 节点颜色
// 状态和性质的判断
#define IsRoot(x) (!((x).parent))
#define IsLChild(x) (!IsRoot(x) && (&(x) == (x).parent->lChild))
#define IsRChild(x) (!IsRoot(x) && (&(x) == (x).parent->rChild))
#define HasParent(x) (!IsRoot(x))
#define HasLChild(x) ((x).lChild)
#define HasRChild(x) ((x).rChild)
#define HasChild(x) (HasLChild(x) || HasRChild(x))
#define HasBothChild(x) (HasLChild(x) && HasRChild(x))
#define IsLeaf(x) (!HasChild(x))
// 与此节点相关的节点以及指针
#define sibling(p) ( \
IsLChild(*(p)) ? \
(p)->parent->rChild : \
(p)->parent->lChild \
) //兄弟
#define uncle(p) ( \
IsLChild(*((p)->parent)) ? \
(p)->parent->parent->rChild \
(p)->parent->parent->lChild \
) // 叔叔= 父亲的兄弟
#define FromParentTo(x) ( \
IsRoot(x) ? _root : ( \
IsLChild(x) ? (x).parent->lChild : (x).parent->rChild \
) \
) //来自父亲的指针
// 二叉树节点模板类
template<typename T> struct BinNode {
T data;
BinNodePosi(T) parent; BinNodePosi(T) lChild; BinNodePosi(T) rChild;
int height;
int npl; // null path length
RBColor color;
// 构造函数
BinNode() : parent(nullptr), lChild(nullptr), rChild(nullptr), height(0), npl(1), color(RB_RED) {};
BinNode(T e, BinNodePosi(T) p = nullptr, BinNodePosi(T) lc = nullptr, BinNodePosi(T) rc = nullptr, int h = 0, int l = 1, RBColor c = RB_RED)
:data(e), parent(p), lChild(lc), rChild(rc), height(h), npl(l), color(c) {}
// 操作
int size(); //当前节点的后代总数,子树的规模
BinNodePosi(T) insertAsLC(T const&); // 插入到左孩子
BinNodePosi(T) insertAsRC(T const&);
BinNodePosi(T) succ(); //当前节点的中序遍历的直接后继
template<typename VST> void travLevel(VST&); //子树层次遍历
template<typename VST> void travPre(VST&); //子树先序遍历
template<typename VST> void travIn(VST&); //子树中序遍历
template<typename VST> void travPost(VST&); //子树后序遍历
// 比较器, 判别器
bool operator<(BinNode const& bn) { return data < bn.data; }
bool operator==(BinNode const& bn) { return data == bn.data; }
private:
template<typename VST> void travPre_R(BinNodePosi(T) x, VST& visit);
template<typename VST> void travIn_R(BinNodePosi(T) x, VST& visit);
template<typename VST> void travPost_R(BinNodePosi(T) x, VST& visit);
template<typename VST> void travPre_I1(BinNodePosi(T) x, VST& visit);
template<typename VST> void travPre_I2(BinNodePosi(T) x, VST& visit);
template<typename VST> void travIn_I1(BinNodePosi(T) x, VST& visit);
template<typename VST> void travIn_I2(BinNodePosi(T) x, VST& visit);
template<typename VST> void travIn_I3(BinNodePosi(T) x, VST& visit);
template<typename VST> void travIn_I4(BinNodePosi(T) x, VST& visit);
template<typename VST> void travPost_I(BinNodePosi(T) x, VST& visit);
};
/*
IMPLEMENT
*/
template<typename T>
int BinNode<T>::size() {
if (!this) return 0;
int count = 0;
queue<BinNodePosi(T)> Q;
Q.enqueue(this);
while (!Q.empty()) {
BinNodePosi(T) x = Q.dequeue(); count++;
if (HasLChild(*x)) Q.enqueue(x->lChild);
if (HasRChild(*x)) Q.enqueue(x->rChild);
}
return count;
}
// 插入孩子节点
template<typename T>
BinNodePosi(T) BinNode<T>::insertAsLC(T const& e) { return lChild = new BinNode(e, this); }
template<typename T>
BinNodePosi(T) BinNode<T>::insertAsRC(T const& e) { return rChild = new BinNode(e, this); }
// 中序遍历的直接后继: 若有右孩子,则为右子树的最左侧左孩子,否则(没有右孩子),则回溯到父节点(如果此父节点仍然是右孩子,说明已经访问过,继续回溯,直到为左孩子)
template<typename T>
BinNodePosi(T) BinNode<T>::succ() {
BinNodePosi(T) s = this;
if (rChild) {
s = rChild;
while (HasLChild(*s)) s = s->lChild;
}
else {
while (IsRChild(*s)) s = s->parent;
s = s->parent; // 最后再右上方移动一步,抵达直接后继
}
return s;
}
//遍历
//先序
template<typename T>
template<typename VST>
void BinNode<T>::travPre(VST&visit) {
switch (rand() % 3) {
case 1: travPre_I1(this, visit); break;
case 2: travPre_I2(this, visit); break;
default: travPre_R(this, visit); break;
}
}
// recursion
template<typename T>
template<typename VST>
void BinNode<T>::travPre_R(BinNodePosi(T) x, VST& visit) {
if (!x) return;
visit(x->data);
travPre_R(x->lChild, visit);
travPre_R(x->rChild, visit);
}
// version 1
template<typename T>
template<typename VST>
void BinNode<T>::travPre_I1(BinNodePosi(T) x, VST& visit) {
stack <BinNodePosi(T)> S;
if (x) S.push(x);
while (!S.empty()) {
x = S.pop(); visit(x->data);
if (HasRChild(*x)) S.push(x->rChild); //先压入右侧,先入后出
if (HasLChild(*x)) S.push(x->lChild);
}
}
// version 2
template<typename T, typename VST>
static void visitAlongLeftBranch(BinNodePosi(T) x, VST& visit, stack <BinNodePosi(T)>& S) {
while (x) {
visit(x->data);
S.push(x->rChild);
x = x->lChild;
}
}
template<typename T>
template<typename VST>
void BinNode<T>::travPre_I2(BinNodePosi(T) x, VST& visit) {
stack<BinNodePosi(T)> S;
while (true) {
visitAlongLeftBranch(x, visit, S);
if (S.empty()) break;
x = S.pop();
}
}
// 中序
template<typename T>
template <typename VST>
void BinNode<T> ::travIn(VST& visit) {
switch (rand() % 5) {
case 1: travIn_I1(this, visit); break;
case 2: travIn_I2(this, visit); break;
case 3: travIn_I3(this, visit); break;
case 4: travIn_I4(this, visit); break;
default: travIn_R(this, visit); break;
}
}
// recursion
template<typename T>
template<typename VST>
void BinNode<T>::travIn_R(BinNodePosi(T) x, VST& visit) {
if (!x) return;
travIn_R(x->lChild, visit);
visit(x->data);
travIn_R(x->rChild, visit);
}
// version1
template<typename T>
static void goAlongLeftBranch(BinNodePosi(T) x, stack<BinNodePosi(T)>& S) {
while (x) {
S.push(x); x = x->lChild;
}
}
template<typename T>
template<typename VST>
void BinNode<T>::travIn_I1(BinNodePosi(T) x, VST& visit) {
stack<BinNodePosi(T)> S;
while (true) {
goAlongLeftBranch(x, S);
if (S.empty()) break;
x = S.pop();
visit(x->data);
x = x->rChild;
}
}
// version 2
template<typename T>
template<typename VST>
void BinNode<T>::travIn_I2(BinNodePosi(T) x, VST& visit) {
stack<BinNodePosi(T)> S;
while (true) {
if (x) {
S.push(x);
x = x->lChild;
}
else if (!S.empty()) {
x = S.pop();
visit(x->data);
x = x->rChild;
}
else
break;
}
}
// version 3 无辅助栈
template<typename T>
template<typename VST>
void BinNode<T>::travIn_I3(BinNodePosi(T) x, VST& visit) {
bool backtrack = false; //前一步是否从右子树回溯,省去栈
while (true)
if (!backtrack && HasLChild(*x)) //若有左子树且不是刚刚回溯,则
x = x->lChild;
else { //否则,无左子树或刚刚回溯(相当于无左子树)
visit(x->data);
if (HasRChild(*x)) {
x = x->rChild; //深入右子树继续遍历
backtrack = false; // 关闭回溯标志
}
else {// 若右子树为空,则
if (!(x = x->succ())) break; //回溯(含抵达末节点时的退出返回)
backtrack = true; //并设置回溯标志
}
}
}
//version 4, 无辅助栈,无标志位
template<typename T>
template<typename VST>
void BinNode<T>::travIn_I4(BinNodePosi(T) x, VST& visit) {
while (true) {
if (HasLChild(*x))
x = x->lChild;
else {
visit(x->data);
while (!HasRChild(*x))
if (!(x = x->succ())) return;
else visit(x->data);
x = x->rChild;
}
}
}
// 后序
template<typename T>
template<typename VST>
void BinNode<T>::travPost(VST& visit) {
switch (rand() % 2) {
case 1: travPost_I(this, visit);
default: travPost_R(this, visit);
}
}
// recursion
template<typename T>
template<typename VST>
void BinNode<T>::travPost_R(BinNodePosi(T) x, VST& visit) {
if (!x) return;
travPost_R(x->lChild, visit);
travPost_R(x->rChild, visit);
visit(x->data);
}
// 迭代版后续遍历, 尽可能往左,实在没左节点的话,再向右
template<typename T> // 在以S栈顶节点为根的子树中,找到最高左侧可见叶节点
static void gotoHLVFL(stack <BinNodePosi(T)>& S) { // 沿途所遇节点依次入栈
while (BinNodePosi(T) x = S.top()) {
if (HasLChild(*x)) {//尽可能往左
if (HasRChild(*x)) S.push(x->rChild); // 若有右孩子,优先入栈
S.push(x->lChild); //然后才转至左孩子
}
else
S.push(x->rChild);
}
S.pop();
}
template<typename T>
template<typename VST>
void BinNode<T>::travPost_I(BinNodePosi(T) x, VST& visit) {
stack<BinNodePosi(T)> S; //辅助栈
if (x) S.push(x);
while (!S.empty()) {
if (S.top() != x->parent)//若栈顶非当前节点之父(则必为其右兄), 此时需要
gotoHLVFL(S); // 在以其右兄为根的子树中,找到HLVFL
x = S.pop(); visit(x->data); //弹出栈顶(即前一个节点的后继), 并访问之
}
}
// 层次遍历 level
template<typename T>
template<typename VST>
void BinNode<T>::travLevel(VST& visit) {//层次遍历算法
queue<BinNodePosi(T)> Q;
Q.enqueue(this);
while (!Q.empty()) {
BinNodePosi(T) x = Q.dequeue(); visit(x->data);
if (HasLChild(*x)) Q.enqueue(x->lChild);
if (HasRChild(*x)) Q.enqueue(x->rChild);
}
}
};