二叉查找树之C++实现



个人笔记,如有描述不当,欢迎留言指出~

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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

template<class T>
struct BSTreeNode{
T key;
BSTreeNode *left;
BSTreeNode *right;
BSTreeNode *parent;
BSTreeNode(T k ){
key=k;
left=NULL;
right=NULL;
parent=NULL;
}
BSTreeNode(){};
};
//错误信息
struct ErrorMessage{
bool flag;
string message;
};

//生成节点并插入树中
template<class T>
ErrorMessage insert(BSTreeNode<T>* &root,T key){
BSTreeNode<T>* node=NULL;
ErrorMessage error;
stringstream s;
if ((node=new BSTreeNode<T>(key)) == NULL)
{
s<<"值"<<key<<"分配节点内存失败";
error.flag=false;
error.message= s.str();
return error;
}
BSTreeNode<T>* x= root;
BSTreeNode<T>* y= NULL;
while (x !=NULL)
{
y=x;
if (node->key<x->key)
{
x=x->left;
}
else if(node->key>x->key){
x=x->right;
}
else{
s<<"值"<<key<<"重复,创建节点失败";
error.flag=false;
error.message=s.str();
return error;
}
}
node->parent=y;
if (y==NULL)
{
root = node;
}
else if(node->key<y->key){
y->left=node;
}
else{
y->right=node;
}
s<<"值"<<key<<"创建节点成功";
error.flag=true;
error.message=s.str();
return error;
}
//前序遍历
template<class T >
void preShow(BSTreeNode<T>* node){
if (node != NULL)
{
cout<<node->key<<" ";
preShow(node->left);
preShow(node->right);
}
}
//中序遍历
template<class T>
void midShow(BSTreeNode<T>* node){
if (node !=NULL)
{
midShow(node->left);
cout<<node->key<<" ";
midShow(node->right);
}
}

//后序遍历
template<class T >
void postShow(BSTreeNode<T>* node){
if (node != NULL)
{
cout<<node->key<<" ";
postShow(node->left);
postShow(node->right);
}
}

// 查找节点树下包含key的节点
template<class T >
BSTreeNode<T>* search(BSTreeNode<T>* node,T key){
if (node == NULL || node->key == key)
{
return node;
}
else
{
if (node->key>key)
{
search(node->left,key);
}
else if (node->key<key)
{
search(node->right,key);
}
}
}
// 返回节点树中最大值的节点
template<class T >
BSTreeNode<T>* maxNode(BSTreeNode<T>* node){
if (node==NULL)
{
return NULL;
}
if (node->right==NULL)
{
return node;
}
else
{
maxNode(node->right);
}
}

// 返回节点树中最小值的节点
template<class T >
BSTreeNode<T>* minNode(BSTreeNode<T>* node){
if (node==NULL)
{
return NULL;
}
if (node->left==NULL)
{
return node;
}
else
{
minNode(node->left);
}
}
//返回传入节点的后继节点
template<class T>
BSTreeNode<T>* successor(BSTreeNode<T>* node){
if(node == NULL){
return NULL;
}
if (node->right!=NULL)
{
return minNode(node->right);
}
BSTreeNode<T>* parent=node->parent;
BSTreeNode<T>* child=node;
while(parent!=NULL&&parent->left!=child){
child=parent;
parent=parent->parent;
}
return parent;
}

// 返回传入节点的前驱节点
template<class T>
BSTreeNode<T>* predecessor(BSTreeNode<T>* node){
if (node == NULL)
{
return NULL;
}
if (node->left!=NULL)
{
return maxNode(node->left);
}
BSTreeNode<T>* parent=node->parent;
BSTreeNode<T>* child=node;
while(parent!=NULL&&parent->right!=child){
child=parent;
parent=parent->parent;
}
return parent;
}
//删除节点
template<class T>
void delete_node(BSTreeNode<T>* &root, BSTreeNode<T>* node){
if (node->left==NULL&&node->right==NULL) //叶子节点
{
if (node == root)
{
root=NULL;
}
else if (isLeft(node))
{
node->parent->left=NULL;
}
else{
node->parent->right=NULL;
}
}
else if (node->left==NULL) //只有右子节点
{
if (node == root) //节点是根节点
{
root=node->right;
node->right->parent=NULL;
}
else if (isLeft(node)) //节点是左节点
{
node->parent->left=node->right;
node->right->parent=node->parent;
}
else{ //节点是右节点
node->parent->right=node->right;
node->right->parent=node->parent;
}
}
else if (node->right==NULL) //只有左节点
{
if (node == root) //节点是根节点
{
root=node->left;
node->left->parent=NULL;
}
else if (isLeft(node)) //节点是左节点
{
node->parent->left=node->left;
node->left->parent=node->parent;
}
else{ //节点是右节点
node->parent->right=node->left;
node->left->parent=node->parent;
}
}
else //左右节点都有
{
BSTreeNode<T>* suc= successor(node); //node 的后继节点
if (suc == node->right) //后继节点为右子节点
{
if (node == root)// 删除节点为根节点
{
root=suc;
suc->left=node->left;
node->left->parent=suc;
}
else{// 删除节点不为根节点
if (isLeft(node)) //删除节点为左节点
{
node->parent->left= suc;
}
else{ // 删除节点为右节点
node->parent->right=suc;
}
suc->parent=node->parent;
suc->left=node->left;
node->left->parent=suc;
}
}
else //后继节点不为右子节点
{
suc->parent->left=suc->right;
if (suc->right!=NULL) // 后继节点的右节点不空
{
suc->right->parent= suc->parent;
}
suc->left =node->left;
node->left->parent =suc;
suc->right=node->right;
node->right->parent =suc;
suc->parent=node->parent;
if (node==root)
{
root=suc;
}
}
}
delete node;
node=NULL;
}
template<class T>
bool deleteByKey(BSTreeNode<T>* &root, T key){
BSTreeNode<T>* node= search(root,key);
if (node == NULL)
{
return false;
}
else{
delete_node(root,node);
return true;
}
}
// 判断是否为左节点
template<class T>
bool isLeft(BSTreeNode<T>* node){
if (node->parent!=NULL&&node->parent->left==node)
{
return true;
}
return false;
}
int main(){

BSTreeNode<int>* root=NULL;
cout<<insert(root,5).message<<endl;
cout<<insert(root,3).message<<endl;
cout<<insert(root,1).message<<endl;
cout<<insert(root,2).message<<endl;
cout<<"前序"<<endl;
preShow(root);
cout<<endl<<"中序"<<endl;
midShow(root);
cout<<endl<<"后序"<<endl;
postShow(root);
cout<<endl;
if (search(root,4) != NULL)
{
cout<<"存在"<<4<<endl;
}else{
cout<<"不存在"<<4<<endl;
}
cout<<"最大值:"<<maxNode(root)->key<<endl;
cout<<"最小值:"<<minNode(root)->key<<endl;
cout<<"3的后继:"<<successor(search(root,3))->key<<endl;
cout<<"3的前驱:"<<predecessor(search(root,3))->key<<endl;
if ( deleteByKey(root,5))
{
cout<<"5删除成功"<<endl;
}
else
{
cout<<"5删除成功"<<endl;
}
cout<<"中序"<<endl;
midShow(root);

return 0;
}

请博主喝咖啡