TheRiver | blog

You have reached the world's edge, none but devils play past here

0%

二叉搜索树

数据结构

bst.jpg

1
2
3
4
5
6
7
struct BST
{
int data;
BST *left;
BST *right;
BST *parent;
};

前序遍历:A,B,C

中序遍历:B,A,C

后续遍历:B,C,A

递归

前序遍历

1
2
3
4
5
6
7
8
9
void PreOrderTraversal(BST *root)
{
if (root == NULL)
return;

cout << root->data << " ";
PreOrderTraversal(root->left);
PreOrderTraversal(root->right);
}

中序遍历

1
2
3
4
5
6
7
8
9
void InOrderTraversal(BST *root)
{
if (root == NULL)
return;

InOrderTraversal(root->left);
cout << root->data << " ";
InOrderTraversal(root->right);
}

后续遍历

1
2
3
4
5
6
7
8
9
void PostOrderTraversal(BST *root)
{
if (root == NULL)
return;

PostOrderTraversal(root->left);
PostOrderTraversal(root->right);
cout << root->data << " ";
}

插入

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
BST* InsertBST(BST *root, int data)
{
static BST *prev = NULL;

if (root == NULL)
{
BST *n = new BST;
memset(n, 0, sizeof(BST));
n->data = data;

if (prev == NULL)
;
else if (data > prev->data)
prev->right = n;
else
prev->left = n;

n->parent = prev;

return n;
}

prev = root;
if (data > root->data)
InsertBST(root->right, data);
else
InsertBST(root->left, data);

return root;
}

删除有三种情况:

  • 删除的是叶子节点
  • 删除节点包含1个孩子节点
  • 删除节点包含2个孩子节点

删除

1
2
3
4
void DeleteBST(int data)
{

}

非递归

前序遍历

1
2
3
4
void PreOrderTraversal
{

}

中序遍历

1
2
3
4
void InOrderTraversal
{

}

后序遍历

1
2
3
4
void PostOrderTraversal
{

}

插入

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
BST* InsertBST(BST *root, int data)
{
BST *n = new BST;
memset(n, 0, sizeof(BST));
n->data = data;

if (root == NULL)
root = n;
else
{
BST *next = root;
BST *prev = NULL;
while(next)
{
prev = next;
if (data > next->data)
next = next->right;
else
next = next->left;
}

if (data > prev->data)
prev->right = n;
else
prev->left = n;

n->parent = prev;
}

return n;
}

删除

1
2
3
4
void DeleteBST(int data)
{

}

DEMO

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
#include "pch.h"
#include <iostream>
#include <memory.h>

using namespace std;

class BST;
void Delete(BST *root);
BST *gRoot = NULL;

class BST
{
public:
int data;
BST *left;
BST *right;
BST *parent;

BST()
{

}
BST(int data)
{
this->data = data;
left = NULL;
right = NULL;
parent = NULL;
}

~BST()
{

}
};


#if 0
BST* InsertBST(BST *root, int data)
{
BST *n = new BST;
memset(n, 0, sizeof(BST));
n->data = data;

if (root == NULL)
root = n;
else
{
BST *next = root;
BST *prev = NULL;
while (next)
{
prev = next;
if (data > next->data)
next = next->right;
else
next = next->left;
}

if (data > prev->data)
prev->right = n;
else
prev->left = n;

n->parent = prev;
}

return n;
}
#endif

BST* InsertBST(BST *root, int data)
{
static BST *prev = NULL;

if (root == NULL)
{
BST *n = new BST(data);

if (prev == NULL)
;
else if (data > prev->data)
prev->right = n;
else
prev->left = n;

n->parent = prev;

return n;
}

prev = root;
if (data > root->data)
InsertBST(root->right, data);
else
InsertBST(root->left, data);

return root;
}

void PreOrderTraversal(BST *root)
{
if (root == NULL)
return;

cout << root->data << " ";
PreOrderTraversal(root->left);
PreOrderTraversal(root->right);
}

void InOrderTraversal(BST *root)
{
if (root == NULL)
return;
InOrderTraversal(root->left);
cout << root->data << " ";
InOrderTraversal(root->right);
}

void PostOrderTraversal(BST *root)
{
if (root == NULL)
return;

PostOrderTraversal(root->left);
PostOrderTraversal(root->right);
cout << root->data << " ";
}

void DeleteBST(BST *root, int data)
{
if (root == NULL)
return;

if (data == root->data)
return Delete(root);
else if (data > root->data)
DeleteBST(root->right, data);
else
DeleteBST(root->left, data);
}

BST * RightAdjustNode(BST *node)
{
while (node->left)
node = node->left;

return node;
}

void Delete(BST *root)
{
if (root == NULL)
return;

BST *AdjustNode = NULL;

//status 1 删除叶子节点
if (root->left == NULL && root->right == NULL)
{
if (root->parent == NULL)
;
else if (root->parent->left == root)
root->parent->left = NULL;
else
root->parent->right = NULL;

delete root;
}
//status 2 有两个孩子节点 采用后驱节点
else if (root->left != NULL && root->right != NULL)
{
//查找后驱节点
AdjustNode = RightAdjustNode(root->right);
//后驱节点替换删除节点的值
if (root->parent == NULL)
gRoot->data = AdjustNode->data;
else if (root->parent->left == root)
root->parent->left->data = AdjustNode->data;
else
root->parent->right->data = AdjustNode->data;
//删除后驱节点
Delete(AdjustNode);
}
//status 3 有一个孩子节点
else
{
//查找孩子节点
AdjustNode = (root->left != NULL ? root->left : root->right);
//孩子节点替换删除节点
if (root->parent == NULL)
gRoot = AdjustNode;
else if (root->parent->left == root)
root->parent->left = AdjustNode;
else
root->parent->right = AdjustNode;
//删除孩子节点
delete root;
}

return;
}




int main()
{
int data;
bool flag = false;

while (cin >> data)
{
if (flag == false)
{
gRoot = InsertBST(gRoot, data);
flag = true;
}
else
{
InsertBST(gRoot, data);
}
}

cout << "PreOrder " << endl;
PreOrderTraversal(gRoot);
cout << endl;
cout << "InOrder " << endl;
InOrderTraversal(gRoot);
cout << endl;
cout << "PostOrder " << endl;
PostOrderTraversal(gRoot);

cout << endl;
cout << "Input a data to delete:" << endl;
#if 0
cout << endl;
cout << "delete 1" << endl;
DeleteBST(gRoot, 1);
InOrderTraversal(gRoot);
#endif
#if 0
cout << endl;
cout << "delete 3" << endl;
DeleteBST(gRoot, 3);
InOrderTraversal(gRoot);
#endif
#if 1
cout << endl;
cout << "delete 6" << endl;
DeleteBST(gRoot, 6);
InOrderTraversal(gRoot);
#endif

return 0;
}

bst_test1.jpg

1
3
5
2
6
^Z
PreOrder
1 3 2 5 6
InOrder
1 2 3 5 6
PostOrder
2 6 5 3 1

ending

76266339_p0_master1200_lit.jpg

----------- ending -----------