-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_tree.c
253 lines (240 loc) · 5.69 KB
/
search_tree.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
#include "search_tree.h"
#include <stdio.h>
#include <stdlib.h>
//初始化二叉搜索树
void SearchTreeInit(SearchTreeNode** root)
{
if(root == NULL)
{
//非法输入
return;
}
*root = NULL;
}
//创建一个二叉搜索树的节点
SearchTreeNode* CreateNode(SearchTreeType value)
{
SearchTreeNode* new_node = (SearchTreeNode*)malloc(sizeof(SearchTreeNode));
new_node->data = value;
new_node->lchild = NULL;
new_node->rchild = NULL;
return new_node;
}
//插入数据
void SearchTreeInsert(SearchTreeNode** root, SearchTreeType key)
{
if(root == NULL)
{
//非法输入
return;
}
if(*root == NULL)
{
*root = CreateNode(key);
return;
}
if((*root)->data < key)
{
SearchTreeInsert(&(*root)->rchild,key);
}else if((*root)->data > key)
{
SearchTreeInsert(&(*root)->lchild,key);
}else
{
//相等的情况就直接舍弃掉好了
return;
}
}
//查找指定数据
SearchTreeNode* SearchTreeFind(SearchTreeNode* root, SearchTreeType to_find)
{
if(root == NULL)
{
return NULL;
}
if(root->data == to_find)
{
return root;
}
else if(root->data > to_find)
{
return SearchTreeFind(root->lchild,to_find);
}else
{
return SearchTreeFind(root->rchild,to_find);
}
}
void Swap(SearchTreeType* a, SearchTreeType* b)
{
SearchTreeType tmp = *a;
*a = *b;
*b = tmp;
}
void _SearchTreeRemove(SearchTreeNode** cur)
{
if(cur == NULL)
{
return;
}
//1.要删除的节点是叶子节点,就直接释放,然后置空,注意要用二级指针接收
if((*cur)->lchild == NULL && (*cur)->rchild == NULL)
{
free(*cur);
*cur = NULL;
return;
}
SearchTreeNode** to_delete = cur;
//仅有左子树,就把左子树上移,因为是二级指针,所以直接解引用将其置空就好
//仅有右子树,就把右子树上移
if((*cur)->rchild == NULL )
{
to_delete = cur;
*cur = (*cur)->lchild;
free(*to_delete);
return;
}
else if((*cur)->lchild == NULL )
{
to_delete = cur;
*cur = (*cur)->lchild;
free(*to_delete);
return;
}
//3.要删除节点的左孩子和右孩子节点不是叶子节点,也就是说子树较多
// 1.找到左子树中的最大值,
// 2.将这个最大值与当前节点要被删除的值交换
// 3.保证被删除的值处于一个叶子节点上
else{
to_delete = &(*cur)->lchild;
while((*to_delete)->rchild != NULL)
{
*to_delete = (*to_delete)->rchild;
}
Swap(&(*cur)->data,&(*to_delete)->data);
_SearchTreeRemove(to_delete);
}
}
//删除指定值
void SearchTreeRemove(SearchTreeNode** root, SearchTreeType key)
{
if(root == NULL)
{
//非法输入
return;
}
if(*root == NULL)
{
//空的二叉搜索树
return;
}
else{
if((*root)->data == key)
{
_SearchTreeRemove(root);
return;
}
else if(key < (*root)->data && (*root)->lchild != NULL)
{
SearchTreeRemove(&(*root)->lchild,key);
}else if (key > (*root)->data && (*root)->rchild != NULL)
{
SearchTreeRemove(&(*root)->rchild,key);
}
}
}
//////////////////////////////////////////////////
///////////////TEST PART/////////////////////////
//////////////////////////////////////////////////
void Preorder(SearchTreeNode* root)
{
if(root == NULL)
return;
printf("%c ",root->data);
Preorder(root->lchild);
Preorder(root->rchild);
}
void Inorder(SearchTreeNode* root)
{
if(root == NULL)
return;
Inorder(root->lchild);
printf("%c ",root->data);
Inorder(root->rchild);
}
void SearchTreeNodePrintChar(SearchTreeNode* root, const char* msg)
{
if(root == NULL || msg == NULL)
{
return;
}
printf("%s \n",msg);
printf("\n");
printf("前序遍历结果为: ");
Preorder(root);
printf("\n");
printf("中序遍历结果为: ");
Inorder(root);
printf("\n");
}
void TestInit()
{
TESTHEAD;
SearchTreeNode* root;
SearchTreeInit(&root);
printf("except NULL , actural %p \n",root);
}
void TestInsert()
{
TESTHEAD;
SearchTreeNode* root;
SearchTreeInit(&root);
SearchTreeInsert(&root,'d');
SearchTreeInsert(&root,'c');
SearchTreeInsert(&root,'e');
SearchTreeInsert(&root,'a');
SearchTreeInsert(&root,'b');
SearchTreeInsert(&root,'f');
SearchTreeInsert(&root,'g');
SearchTreeNodePrintChar(root,"插入七个元素");
}
void TestFind()
{
TESTHEAD;
SearchTreeNode* root;
SearchTreeInit(&root);
SearchTreeInsert(&root,'d');
SearchTreeInsert(&root,'c');
SearchTreeInsert(&root,'e');
SearchTreeInsert(&root,'a');
SearchTreeInsert(&root,'b');
SearchTreeNodePrintChar(root,"插入五个元素");
SearchTreeNode* cur = SearchTreeFind(root,'a');
if(cur != NULL)
{
printf("except a ,actural %c \n",cur->data);
}
}
void TestRemove()
{
TESTHEAD;
SearchTreeNode* root;
SearchTreeInit(&root);
SearchTreeInsert(&root,'d');
SearchTreeInsert(&root,'c');
SearchTreeInsert(&root,'e');
SearchTreeInsert(&root,'a');
SearchTreeInsert(&root,'b');
SearchTreeInsert(&root,'f');
SearchTreeInsert(&root,'g');
SearchTreeNodePrintChar(root,"插入七个元素");
SearchTreeRemove(&root,'g');
SearchTreeNodePrintChar(root,"删除一个指定元素");
}
int main()
{
TestInit();
TestInsert();
TestFind();
TestRemove();
return 0;
}