-
Notifications
You must be signed in to change notification settings - Fork 12
/
myBinNode.h
195 lines (173 loc) · 5.49 KB
/
myBinNode.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
#ifndef DSA_CPP_DENG_MYBINNODE_H
#define DSA_CPP_DENG_MYBINNODE_H
#include <cstdlib>
#include "myStack.h"
#include "myQueue.h"
#define BinNodePosi(T) BinNode<T>*
#define max(x, y) (x > y ? x : y)
#define stature(p) ((p) ? (p)->height : -1)
#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(x) (sibling((x).parent)) //返回叔叔
#define FromParentTo(x) (IsRoot(x) ? this->_root : \
(IsLChild(x) ? (x).parent->lChild : (x).parent->rChild)) //来自父亲的指针
typedef enum {RB_RED, RB_BLACK} RBColor;
template <typename T> struct BinNode {
T data;
BinNodePosi(T) parent;
BinNodePosi(T) lChild;
BinNodePosi(T) rChild;
int height;
int npl; //Null Path Length(空节点通路长度,左式堆,也可直接用height代替)
RBColor color;
/* 构造函数 */
BinNode() : parent(nullptr), lChild(nullptr), rChild(nullptr), height(0), npl(1),
color(RB_RED) {}
explicit 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<(BinNodePosi(T) bn) {
return data < bn->data;
}
bool operator>(BinNodePosi(T) bn) {
return data > bn->data;
}
bool operator==(BinNodePosi(T) bn) {
return data == bn->data;
}
bool operator<=(BinNodePosi(T) bn) {
return data <= bn->data;
}
bool operator>=(BinNodePosi(T) bn) {
return data >= bn->data;
}
template <typename VST> static void visitAlongLeftBranch(BinNodePosi(T) x, VST& visit, Stack<BinNodePosi(T)>& S) {
while(x) {
visit(x);
S.push(x->rChild);
x = x->lChild;
}
}
template <typename VST> void travPre_I2(BinNodePosi(T) x, VST& visit) {
Stack<BinNodePosi(T)> S;
while(true) {
visitAlongLeftBranch(x, visit, S);
if(S.empty())
break;
x = S.pop();
}
}
void goAlongLeftBranch(BinNodePosi(T) x, Stack<BinNodePosi(T)>& S) {
while(x) {
S.push(x);
x = x->lChild;
}
}
template <typename VST> void travIn_I1(BinNodePosi(T)x, VST visit) {
Stack<BinNodePosi(T)> S;
while (true) {
goAlongLeftBranch(x, S);
if(S.empty()) break;
visit(x);
x = S.pop();
x = x->rChild;
}
}
void gotoHLVFL(Stack<BinNodePosi(T)>& S) {
while(BinNodePosi(T) x = S.pop()) {
if(HasLChild(x)) {
if(HasRChild(x))
S.push(x->rChild);
S.push(x->lChild);
} else {
S.push(x->rChild);
}
}
S.pop();
}
template <typename VST> void travPost_I(BinNodePosi(T) x, VST visit) {
Stack<BinNodePosi(T)> S;
S.push(x);
while(!S.empty()) {
if(S.top() != x->parent)
gotoHLVFL(S);
x = S.pop();
visit(x);
}
}
};
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>
template <typename VST>
void BinNode<T>::travIn(VST& visit) {
travIn_I1(this, visit);
}
template <typename T> BinNodePosi(T) BinNode<T>::succ() {
BinNodePosi(T) x = this;
if(rChild) {
x = rChild;
while(HasLChild(x))
x = x->lChild;
} else {
while(IsRChild(*x))
x = x->parent;
x = x->parent;
}
return x;
}
template <typename T> int BinNode<T>::size() {
int l = 0, r = 0;
if(lChild != nullptr)
l = lChild->size();
if(rChild != nullptr)
r = rChild->size();
return l + r + 1;
}
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);
if(HasLChild(x))
Q.enqueue(x->lChild);
if(HasRChild(x))
Q.enqueue(x->rChild);
}
}
template<typename T>
template<typename VST>
void BinNode<T>::travPre(VST &visit) {
travPre_I2(this, visit);
}
template<typename T>
template<typename VST>
void BinNode<T>::travPost(VST & visit) {
travPost_I(this, visit);
}
#endif //DSA_CPP_DENG_MYBINNODE_H