-
Notifications
You must be signed in to change notification settings - Fork 12
/
myBinTree.h
158 lines (142 loc) · 4.4 KB
/
myBinTree.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
#ifndef DSA_CPP_DENG_MYBINTREE_H
#define DSA_CPP_DENG_MYBINTREE_H
#include "myBinNode.h"
template <typename T> static void release(T* x) {
delete x;
}
template <typename T> class BinTree {
protected:
int _size;
BinNodePosi(T) _root;
virtual int updateHeight(BinNodePosi(T) x); //更新节点x的高度
void updateHeightAbove(BinNodePosi(T) x); //更新节点x及其祖先的高度
static int removeAt(BinNodePosi(T) x);
public:
BinTree() : _size(0), _root(nullptr) {}
~BinTree() {
if(0 < _size)
remove(_root);
}
int size() const {
return _size;
}
bool empty() const {
return !_root;
}
BinNodePosi(T) root() const {
return _root;
}
BinNodePosi(T) insertAsRoot(T const& e);
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>* &e); //T作为左子树插入
BinNodePosi(T) attachAsRC(BinNodePosi(T) x, BinTree<T>* &e); //T作为右子树插入
int remove(BinNodePosi(T) x); //删除以节点x为根的子树,返回其规模
BinTree<T>* secede(BinNodePosi(T) x); //将子树摘除,并将其转换为独立的子树
template <typename VST> void travLevel(VST& visit) { //层次遍历
if(_root)
_root->travLevel(visit);
}
template <typename VST> void travPre(VST& visit) { //前序遍历
if(_root)
_root->travPre(visit);
}
template <typename VST> void travIn(VST& visit) { //中序遍历
if(_root)
_root->travIn(visit);
}
template <typename VST> void travPost(VST& visit) { //后序遍历
if(_root)
_root->travPost(visit);
}
/* 比较器、判等器 */
bool operator<(BinTree<T> const& t) {
return _root && t._root && lt(_root, t._root);
}
bool operator>(BinTree<T> const& t) {
return _root && t._root && !lt(_root, t._root);
}
bool operator==(BinTree<T> const& t) {
return _root && t._root && (_root == t._root);
}
bool operator<=(BinTree<T> const& t) {
return _root && t._root && (_root <= t._root);
}
bool operator>=(BinTree<T> const& t) {
return _root && t._root && (_root >= t._root);
}
};
template <typename T> int BinTree<T>::updateHeight(BinNode<T> *x) {
return x->height = 1 + max(stature(x->lChild), stature(x->rChild));
}
template <typename T> void BinTree<T>::updateHeightAbove(BinNode<T> *x) {
while(x) {
updateHeight(x);
x = x->parent;
}
}
template <typename T> BinNodePosi(T) BinTree<T>::insertAsRoot(const T &e) {
_size = 1;
return _root = new BinNode<T>(e);
}
template <typename T> BinNodePosi(T) BinTree<T>::insertAsLC(BinNode<T> *x, T const &e) {
x->insertAsLC(e);
updateHeightAbove(x);
++_size;
return x->lChild;
}
template <typename T> BinNodePosi(T) BinTree<T>::insertAsRC(BinNode<T> *x, T const &e) {
x->insertAsRC(e);
updateHeightAbove(x);
++_size;
return x->rChild;
}
template <typename T>
BinNodePosi(T) BinTree<T>::attachAsLC(BinNode<T> *x, BinTree<T> *&S) {
if((x->lChild = S->_root))
x->lChild->parent = x;
_size += S->_size;
updateHeightAbove(x);
S->root = nullptr;
S->_size = 0;
release(S);
S = NULL;
return x;
}
template <typename T>
BinNodePosi(T) BinTree<T>::attachAsRC(BinNode<T> *x, BinTree<T> *&S) {
if((x->rChild = S->_root))
x->rChild->parent = x;
_size += S->_size;
updateHeightAbove(x);
S->root = nullptr;
S->_size = 0;
release(S);
S = NULL;
return x;
}
template <typename T> int BinTree<T>::remove(BinNode<T> *x) {
FromParentTo(*x) = nullptr;
updateHeightAbove(x->parent);
int n = removeAt(x);
_size -= n;
return n;
}
template <typename T> int BinTree<T>::removeAt(BinNode<T> *x) {
if(!x)
return 0;
int n = 1 + removeAt(x->lChild) + removeAt(x->rChild);
release(x);
return n;
}
template <typename T> BinTree<T>* BinTree<T>::secede(BinNodePosi(T) x) {
FromParentTo(*x) = nullptr;
updateHeightAbove(x->parent);
BinTree<T>* S = new BinTree<T>();
S->_root = x;
S->_root->parent = nullptr;
S->_size = x->size();
_size = S->_size;
return S;
}
#endif //DSA_CPP_DENG_MYBINTREE_H