-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinarySearchTree.cpp
369 lines (323 loc) · 5.91 KB
/
BinarySearchTree.cpp
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
/*
* 二叉搜索树/二叉排序树
*/
#include <iostream>
#include <queue>
#include <stack>
template <typename T>
struct BSTNode
{
BSTNode(const T &key)
: _key(key)
, pLeft(NULL)
, pRight(NULL)
{}
BSTNode *pLeft;
BSTNode *pRight;
T _key;
};
template <typename T>
class BSTree
{
typedef BSTNode<T> Node;
public:
BSTree()
: pRoot(NULL)
{}
bool Insert(const T &key)
{
// 空树直接插入
if (NULL == pRoot)
{
pRoot = new Node(key);
}
else
{
Node *pParent = NULL;
Node *pCur = pRoot;
// 查找要插入的位置
while (NULL != pCur)
{
if (pCur->_key > key)
{
pParent = pCur;
pCur = pCur->pLeft;
}
else if (pCur->_key < key)
{
pParent = pCur;
pCur = pCur->pRight;
}
else
return false;
}
if (key > pParent->_key)
pParent->pRight = new Node(key);
else
pParent->pLeft = new Node(key);
}
return true;
}
bool Remove(const T &key)
{
if (NULL == pRoot)
return false;
// 先找被删除结点,同时保存其双亲结点
Node *pParent = NULL;
Node *pCur = pRoot;
while (NULL != pCur)
{
if (key > pCur->_key)
{
pParent = pCur;
pCur = pCur->pRight;
}
else if (key < pCur->_key)
{
pParent = pCur;
pCur = pCur->pLeft;
}
else
break;
}
// 没找到
if (NULL == pCur)
return false;
// 1.被删除结点无左孩子
// 2.被删除结点无右孩子
// 3.被删除结点左右孩子同时有
// 注意根节点并无双亲结点,特殊处理!
if (NULL == pCur->pLeft)
{
if (pCur == pRoot)
pRoot = pCur->pRight;
else
{
if (pCur == pParent->pLeft)
pParent->pLeft = pCur->pRight;
else
pParent->pRight = pCur->pRight;
}
}
else if (NULL == pCur->pRight)
{
if (pCur == pRoot)
pRoot = pCur->pLeft;
else
{
if (pCur == pParent->pLeft)
pParent->pLeft = pCur->pLeft;
else
pParent->pRight = pCur->pLeft;
}
}
else
{
// 将右子树的最左结点(大于当前被删除结点的最小值)的值赋给之前的结点,
// 转换为删除这个结点
pParent = pCur;
pCur = pCur->pRight;
while (NULL != pCur->pLeft)
{
pParent = pCur;
pCur = pCur->pLeft;
}
pParent->_key = pCur->_key;
if (pCur == pParent->pLeft)
pParent->pLeft = pCur->pRight;
else
pParent->pRight = pCur->pRight;
}
delete pCur;
}
Node * Find(const T &key)
{
Node *pCur = pRoot;
while (NULL != pCur)
{
if (pCur->_key > key)
pCur = pCur->pLeft;
else if (pCur->_key < key)
pCur = pCur->pRight;
else
return pCur;
}
return NULL;
}
void InOrder()
{
_InOrder(pRoot);
std::cout << std::endl;
}
int Height()
{
return _Height(pRoot);
}
int LeafNums()
{
return _LeafNums(pRoot);
}
void LevelOrder()
{
std::queue<Node *> q;
if (NULL != pRoot)
q.push(pRoot);
while (!q.empty())
{
Node *pCur = q.front();
q.pop();
std::cout << pCur->_key << " ";
if (NULL != pCur->pLeft)
q.push(pCur->pLeft);
if (NULL != pCur->pRight)
q.push(pCur->pRight);
}
std::cout << std::endl;
}
void PrevOrderNonR()
{
std::stack<Node *> s;
if (NULL != pRoot)
s.push(pRoot);
while (!s.empty())
{
Node *pCur = s.top();
s.pop();
std::cout << pCur->_key << " ";
if (NULL != pCur->pRight)
s.push(pCur->pRight);
if (NULL != pCur->pLeft)
s.push(pCur->pLeft);
}
std::cout << std::endl;
}
void InOrderNonR()
{
std::stack<Node *> s;
Node *pCur = pRoot;
while (pCur || !s.empty())
{
if (NULL != pCur)
{
s.push(pCur);
pCur = pCur->pLeft;
}
else
{
pCur = s.top();
s.pop();
std::cout << pCur->_key << " ";
pCur = pCur->pRight;
}
}
std::cout << std::endl;
}
void PostOrderNonR()
{
if (NULL == pRoot)
return;
std::stack<Node *> s;
Node *pCur = pRoot, *pPrev = NULL;
// 先将pCur移动到左子树最下边
while (NULL != pCur)
{
s.push(pCur);
pCur = pCur->pLeft;
}
while (!s.empty())
{
// 到这里,pCur都是空,并且已经遍历到左子树底端
pCur = s.top();
// 一个根节点被访问的前提是无右子树或右子树被访问过
if (NULL == pCur->pRight || pPrev == pCur->pRight)
{
s.pop();
std::cout << pCur->_key << " ";
pPrev = pCur;
}
else // 左子树刚被访问过,需进入右子树(有右子树且未被访问过)
{
// 进入右子树,且右子树一定不为空
pCur = pCur->pRight;
while (NULL != pCur)
{
s.push(pCur);
pCur = pCur->pLeft;
}
}
}
std::cout << std::endl;
}
private:
int _LeafNums(Node *root)
{
if (NULL == root)
return 0;
if (NULL == root->pLeft && NULL == root->pRight)
return 1;
return _LeafNums(root->pLeft) + _LeafNums(root->pRight);
}
int _Height(Node *root)
{
if (NULL == root)
return 0;
if (NULL == root->pLeft && NULL == root->pRight)
return 1;
int left = _Height(root->pLeft);
int right = _Height(root->pRight);
return left > right ? left + 1 : right + 1;
}
void _InOrder(Node *root)
{
if (NULL != root)
{
_InOrder(root->pLeft);
std::cout << root->_key << " ";
_InOrder(root->pRight);
}
}
private:
Node *pRoot;
};
int main()
{
BSTree<int> t;
t.Insert(5);
t.Insert(3);
t.Insert(7);
t.Insert(1);
t.Insert(4);
t.Insert(6);
t.Insert(8);
t.Insert(0);
t.Insert(2);
t.Insert(9);
// t.InOrder();
//
// t.Remove(0);
// t.Remove(1);
// t.Remove(9);
// t.Remove(7);
//
// t.InOrder();
//
// for (int i = 0; i < 10; i++)
// t.Remove(i);
// t.InOrder();
// t.Find(9);
// for (int i = 0; i <= 10; i++)
// {
// BSTNode<int> *ret = t.Find(i);
// if (NULL != ret)
// std::cout << ret->_key << std::endl;
// else
// std::cout << i << " not found !" << std::endl;
// }
// std::cout << t.Height() << std::endl;
// std::cout << t.LeafNums() << std::endl;
// t.LevelOrder();
// t.PrevOrderNonR();
// t.InOrderNonR();
t.PostOrderNonR();
return 0;
}