-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBintree.h
More file actions
598 lines (493 loc) · 12.2 KB
/
Bintree.h
File metadata and controls
598 lines (493 loc) · 12.2 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#ifndef MY_BINTREE_H
#define MY_BINTREE_H
#include "Stack.h"
#include "Queue.h"
#include <algorithm>
#include <iostream>
#include <cstdio>
using std::max;
using std::cout;
using std::endl;
#define BinNodePosi(T) BinNode<T>*
#define stature(p) ((p) ? (p)->height : -1) //该结点的高度 空节点高度为-1
//左孩子的水平位置 用于display
#define posilchild(p) ((p)->lchild ? (p)->lchild->horizontal_position : (p)->horizontal_position)
#define posirchild(p) ((p)->rchild ? (p)->rchild->horizontal_position : (p)->horizontal_position)
enum RB_COLOR{
RED, BLACK
};
//二叉树结点数据结构
template <typename T>
class BinNode
{
public:
T data; //该结点具体数据
BinNodePosi(T) parent; //父节点指针
BinNodePosi(T) lchild; //左孩子指针
BinNodePosi(T) rchild; //右孩子指针
enum RB_COLOR color; //颜色 在红黑树中使用
int height; //记录该结点高度
int horizontal_position; //记录中序遍历下该结点的序号,用于display显示
int distance_to_root; //记录到根节点的距离 用于display显示
int size(); //计算以该结点为根的二叉树结点个数(递归算法)
BinNodePosi(T) insertAsLC(T const &); //插入左孩子
BinNodePosi(T) insertAsRC(T const &); //插入右孩子
BinNodePosi(T) succ(); //返回中序遍历时该结点的后继指针
//遍历算法
template <typename VST> void travPre_R(VST &); //先序遍历递归算法
template <typename VST> void travPre_I(VST &); //先序遍历基于栈的算法1
template <typename VST> void travPre_II(VST &); //先序遍历基于栈的算法2
template <typename VST> void travIn_R(VST &); //中序遍历递归算法
template <typename VST> void travIn_I(VST &); //中序遍历基于栈的算法
template <typename VST> void travPost_R(VST &); //后序遍历递归算法
template <typename VST> void travPost_I(VST &); //后序遍历基于栈的算法
template <typename VST> void travLevel(VST &); //层次遍历 基于队列
BinNode(T e, BinNodePosi(T) p); //构造二叉树,父节点p,节点内容e
BinNode();
virtual ~BinNode(){}
};
template <typename T>
BinNode<T>::BinNode(T e, BinNodePosi(T) p) : data(e), parent(p)
{
height = 0;
lchild = 0;
rchild = 0;
horizontal_position = 0;
distance_to_root = 0;
}
template <typename T>
BinNode<T>::BinNode() : data(0), parent(0)
{
height = 0;
lchild = 0;
rchild = 0;
horizontal_position = 0;
distance_to_root = 0;
}
/*
返回该结点为根的子树的结点个数
递归求解 return lc->size() + rc->size() + 1
叶子节点 return 1
*/
template <typename T>
int BinNode<T>::size()
{
int s = 1;
if (lchild) s += lchild->size();
if (rchild) s += rchild->size();
return s;
}
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> class BinTree
{
protected:
int _size; //记录该二叉树结点个数
BinNodePosi(T) _root; //二叉树树根节点指针
virtual int updateHeight(BinNodePosi(T) x); //更新结点x的高度
void updateHeightAbove(BinNodePosi(T) x); //更新x以及x的所有祖先元素的高度
void updateDistanceToRoot(BinNodePosi(T) x); //更新x结点及其孩子距离根节点的距离
void calculatePosition(); //计算结点位置 结果存放在每个结点的horizontal_position和distance_to_root中
BinNodePosi(T) siblingOf(BinNodePosi(T) x); //返回结点x的兄弟结点
public:
BinTree(BinNodePosi(T) root);
BinTree();
virtual ~BinTree();
int size() const { return _size; } //返回树中结点个数
int height() const { return _root ? _root->height : 0; } //返回树的高度
bool empty() const { return !_root; } //判断树是否为空树
BinNodePosi(T) root() const { return _root; } //返回根节点指针
//拓扑连接
BinNodePosi(T) insertAsLC(BinNodePosi(T) x, T const & e); //把e作为x的左孩子插入
BinNodePosi(T) insertAsRC(BinNodePosi(T) x, T const & e); //把e作为x的右孩子插入
BinNodePosi(T) attachAsLC(BinNodePosi(T) x, BinTree<T>* & S); //把S作为x的左子树插入
BinNodePosi(T) attachAsRC(BinNodePosi(T) x, BinTree<T>* & S); //把S作为x的右子树插入
//删除x结点
virtual int remove(BinNodePosi(T) x);
//将以x为根的子树从树中删去
BinNodePosi(T) secede(BinNodePosi(T) x);
//树的遍历算法
template <typename VST> void travPre(VST &v) { _root->travPre_I(v); } //先序遍历 对每个节点执行v
template <typename VST> void travIn(VST &v) { _root->travIn_I(v); } //中序遍历
template <typename VST> void travPost(VST &v) { _root->travPost_I(v); } //后序遍历
template <typename VST> void travLevel(VST &v) { _root->travLevel_I(v); } //层次遍历
//命令行直观显示二叉树
void display();
};
template <typename T>
BinNodePosi(T) BinTree<T>::siblingOf(BinNodePosi(T) x)
{
if (x->parent == 0)
return 0;
if (x->parent->lchild == x)
return x->parent->rchild;
else
return x->parent->lchild;
}
template <typename T>
BinTree<T>::BinTree()
{
_size = 0;
_root = NULL;
}
template <typename T>
BinTree<T>::BinTree(BinNodePosi(T) root) : _root(root)
{
_size = root->size();
root->parent = NULL;
}
template <typename T>
BinTree<T>::~BinTree()
{
removeTreeByRootNode(_root);
}
//更新x结点的高度
template <typename T>
int BinTree<T>::updateHeight(BinNodePosi(T) x)
{
return x->height = 1 + max(stature(x->lchild), stature(x->rchild));
}
//更新x及其祖先结点高度
template <typename T>
void BinTree<T>::updateHeightAbove(BinNodePosi(T) x)
{
while (x)
{
updateHeight(x);
x = x->parent;
}
}
//拓扑连接 步骤为更新size 插入 更新高度
template <typename T>
BinNodePosi(T) BinTree<T>::insertAsLC(BinNodePosi(T) x, T const & e)
{
++_size;
x->insertAsLC(e);
updateHeightAbove(x);
return x->lchild;
}
template <typename T>
BinNodePosi(T) BinTree<T>::insertAsRC(BinNodePosi(T) x, T const & e)
{
++_size;
x->insertAsRC(e);
updateHeightAbove(x);
return x->rchild;
}
template <typename T>
BinNodePosi(T) BinTree<T>::attachAsLC(BinNodePosi(T) x, BinTree<T>* & S)
{
x->lchild = S->_root;
if (x->lchild) x->lchild->parent = x;
_size += S->_size;
updateHeightAbove(x);
S->_root = NULL;
S->_size = 0;
delete S;
S = NULL;
return x;
}
template <typename T>
BinNodePosi(T) BinTree<T>::attachAsRC(BinNodePosi(T) x, BinTree<T>* & S)
{
x->rchild = S->_root;
if (x->rchild) x->rchild->parent = x;
_size += S->_size;
updateHeightAbove(x);
S->_root = NULL;
S->_size = 0;
delete S;
S = NULL;
return x;
}
//删除结点
template <typename T>
int BinTree<T>::remove(BinNodePosi(T) x)
{
if (x->parent->lchild == x) x->parent->lchild = NULL;
if (x->parent->rchild == x) x->parent->rchild = NULL;
updateHeightAbove(x->parent);
int n = removeTreeByRootNode(x);
_size -= n;
return n;
}
//分离子树 返回子树的树根
template <typename T>
BinNodePosi(T) BinTree<T>::secede(BinNodePosi(T) x)
{
if (x->parent->lchild == x) x->parent->lchild = NULL;
if (x->parent->rchild == x) x->parent->rchild = NULL;
updateHeightAbove(x->parent);
BinTree<T> * S = new BinTree<T>;
S->_root = x;
x->parent = NULL;
S->_size = x->size();
_size -= S->size;
return S;
}
//删除子树 释放空间
template <typename T>
int removeTreeByRootNode(BinNodePosi(T) x)
{
if (x == NULL) return 0;
int n = 1 + removeTreeByRootNode(x->lchild) + removeTreeByRootNode(x->rchild);
delete x;
return n;
}
template <typename T>
template <typename VST>
void BinNode<T>::travPre_R(VST & visit)
{
if (this == NULL) return;
visit(this->data);
this->lchild->travPre_R(visit);
this->rchild->travPre_R(visit);
}
template <typename T>
template <typename VST>
void BinNode<T>::travPre_I(VST & visit)
{
Stack<BinNodePosi(T)> stk;
BinNodePosi(T) x;
stk.push(this);
while (!stk.empty())
{
x = stk.pop();
while (x != NULL)
{
visit(x->data);
stk.push(x->rchild);
x = x->lchild;
}
}
}
template <typename T>
template <typename VST>
void BinNode<T>::travPre_II(VST & visit)
{
Stack<BinNodePosi(T)> stk;
BinNodePosi(T) x;
stk.push(this);
while (!stk.empty())
{
x = stk.pop();
if (x != NULL) visit(x->data);
if (x->rchild) stk.push(x->rchild);
if (x->lchild) stk.push(x->lchild);
}
}
template <typename T>
template <typename VST>
void BinNode<T>::travIn_R(VST & visit)
{
if (this == NULL) return;
this->lchild->travIn_R(visit);
visit(this->data);
this->rchild->travIn_R(visit);
}
template <typename T>
template <typename VST>
void BinNode<T>::travIn_I(VST & visit)
{
Stack<BinNodePosi(T)> stk;
BinNodePosi(T) x = this;
stk.push(x);
x = x->lchild;
while (true)
{
while (x != NULL)
{
stk.push(x);
x = x->lchild;
}
if (stk.empty()) break;
x = stk.pop();
visit(x->data);
x = x->rchild;
}
}
template <typename T>
template <typename VST>
void BinNode<T>::travPost_R(VST & visit)
{
if (this == NULL) return;
this->lchild->travPost_R(visit);
this->rchild->travPost_R(visit);
visit(this->data);
}
template <typename T>
template <typename VST>
void BinNode<T>::travPost_I(VST & visit)
{
Stack<BinNodePosi(T)> stk;
BinNodePosi(T) x = this;
if (x) stk.push(this);
while (!stk.empty())
{
if (x->parent != stk.top())
{
while ((x = stk.top()) != NULL)
{
if (x->lchild)
{
if (x->rchild)
stk.push(x->rchild);
stk.push(x->lchild);
}
else
{
stk.push(x->rchild);
}
}
stk.pop();
}
x = stk.pop();
visit(x->data);
}
}
template <typename T>
template <typename VST>
void BinNode<T>::travLevel(VST & visit)
{
Queue<BinNodePosi(T)> q;
BinNodePosi(T) x = this;
q.enqueue(x);
while (!q.empty())
{
x = q.dequeue();
if (x) visit(x->data);
if (x->lchild) q.enqueue(x->lchild);
if (x->rchild) q.enqueue(x->rchild);
}
}
//返回中序遍历下的后继指针
template <typename T>
BinNodePosi(T) BinNode<T>::succ()
{
BinNodePosi(T) x;
if (rchild != NULL)
{
x = rchild;
while (x->lchild != NULL)
x = x->lchild;
}
else
{
x = this;
while (x->parent != NULL && x->parent->lchild != x)
x = x->parent;
x = x->parent;
}
return x;
}
//更新x到root的距离 用于display
template <typename T>
void BinTree<T>::updateDistanceToRoot(BinNodePosi(T) x)
{
if (!x) return;
int i = 0;
BinNodePosi(T) p = x;
while (p != NULL)
{
p = p->parent;
++i;
}
x->distance_to_root = i-1;
Queue<BinNodePosi(T)> q;
q.enqueue(x);
while (!q.empty())
{
x = q.dequeue();
if (x && x->parent)
x->distance_to_root = x->parent->distance_to_root + 1;
if (x->lchild) q.enqueue(x->lchild);
if (x->rchild) q.enqueue(x->rchild);
}
}
template <typename T>
void BinTree<T>::calculatePosition()
{
//计算垂直位置
updateDistanceToRoot(_root);
//计算水平位置
int count = 0;
BinNodePosi(T) x = _root;
//找到中序遍历的第一个结点
while (x && x->lchild != NULL)
x = x->lchild;
//按照中序遍历的次序记录结点访问次序
while (x != NULL)
{
x->horizontal_position = ++count;
x->horizontal_position *= 4; //水平位置放缩4倍 命令行显示时结点之间的空隙4个字符
x = x->succ();
}
}
template <typename T>
void BinTree<T>::display()
{
calculatePosition();
Queue<BinNodePosi(T)> q;
BinNodePosi(T) x = _root;
q.enqueue(x);
int nowheight = 0;
int lastheight = 0;
int levelcount = 0;
int i = 0;
while (!q.empty())
{
x = q.dequeue();
long long int tmpposi;
if (x)
{
nowheight = x->distance_to_root;
if (nowheight != lastheight)
{
lastheight = nowheight;
cout << endl;
levelcount = 0;
}
for(i = levelcount; i < posilchild(x); ++i)
{
printf(" ");
}
int firstprint = 1;
for (; i < x->horizontal_position; ++i)
{
if (firstprint)
{
printf("_");
firstprint = 0;
}
else
printf("_");
}
tmpposi = cout.tellp();
cout << x->data;
long long int tellpppp = cout.tellp() - tmpposi;
levelcount += (int) (tellpppp);
levelcount += x->horizontal_position - levelcount;
for (i = levelcount; i < posirchild(x); ++i)
{
if (i == posirchild(x) - 1)
printf("_");
else
printf("_");
}
levelcount = i;
}
if (x && x->lchild) q.enqueue(x->lchild);
if (x && x->rchild) q.enqueue(x->rchild);
}
cout << endl << endl;
}
#endif