-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.c
More file actions
246 lines (204 loc) · 5.53 KB
/
Copy pathbinarytree.c
File metadata and controls
246 lines (204 loc) · 5.53 KB
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
/*
Description:二叉查找树,包括建树,查找,最小值,最大值,前驱,后继,插入和删除结点等基本操作
Date:2012/10/09
Author:Roger Liu
*/
#include <stdio.h>
struct tree;
typedef struct tree tree;
typedef struct tree *ltree;
struct tree{
int data;
ltree parent;
ltree lchild;
ltree rchild;
};
ltree tree_create(void);
ltree tree_search(ltree, int);
ltree tree_maximum(ltree);
ltree tree_minimum(ltree);
ltree tree_predecessor(ltree);
ltree tree_successor(ltree);
void tree_insert(ltree, ltree);
void tree_delete(ltree, ltree);
int main()
{
ltree root, x, y;
printf("请输入根节点,0表示NULL:\n");
root = tree_create();
root->parent = NULL;
/*printf("最大的结点是:%d\n", tree_maximum(root)->data);
printf("最小的结点是:%d\n", tree_minimum(root)->data);
x = tree_search(root, 9);
printf("结点9的前驱结点是:%d\n", tree_predecessor(x)->data);
printf("结点9的后继结点是:%d\n", tree_successor(x)->data);
*/
/*y = (ltree)malloc(sizeof(tree));
y->data = 13;
y->lchild = y->rchild = y->parent = NULL;
tree_insert(root, y);
x = tree_search(root, 15);
printf("\n\n插入结点13后\n\n");
printf("结点15的前驱结点是:%d\n", tree_predecessor(x)->data);
printf("结点15的后继结点是:%d\n", tree_successor(x)->data);
x = tree_search(root, 13);
tree_delete(root, x);
x = tree_search(root, 15);
printf("\n\n删除结点13后\n\n");
printf("结点15的前驱结点是:%d\n", tree_predecessor(x)->data);
printf("结点15的后继结点是:%d\n", tree_successor(x)->data);
x = tree_search(root, 17);
tree_delete(root, x);
x = tree_search(root, 15);
printf("\n\n删除结点17后\n\n");
printf("结点15的前驱结点是:%d\n", tree_predecessor(x)->data);
printf("结点15的后继结点是:%d\n", tree_successor(x)->data);
x = tree_search(root, 18);
tree_delete(root, x);
x = tree_search(root, 15);
printf("\n\n删除结点18后\n\n");
printf("结点15的前驱结点是:%d\n", tree_predecessor(x)->data);
printf("结点15的后继结点是:%d\n", tree_successor(x)->data);
*/
system("pause");
exit(0);
}
ltree tree_create(void)
{
ltree T;
int a;
scanf("%d", &a);
if (a == 0){
T = NULL;
}else{
T = (ltree)malloc(sizeof(tree));
T->data = a;
printf("\n请输入结点%d的左子结点:\n", T->data);
T->lchild = tree_create();
if (T->lchild != NULL)
T->lchild->parent = T;
printf("\n请输入结点%d的右子结点:\n", T->data);
T->rchild = tree_create();
if (T->rchild != NULL)
T->rchild->parent = T;
}
return T;
}
ltree tree_search(ltree T, int x)
{
ltree t = T;
if (t == NULL || t->data == x)
return t;
if (x < t->data)
return tree_search(t->lchild, x);
else
return tree_search(t->rchild, x);
}
ltree tree_maximum(ltree x)
{
while (x->rchild != NULL)
x = x->rchild;
return x;
}
ltree tree_minimum(ltree x)
{
while (x->lchild != NULL)
x = x->lchild;
return x;
}
ltree tree_predecessor(ltree x)
{
ltree y;
if (x->lchild != NULL)
return tree_maximum(x->lchild);
y = x->parent;
while (y != NULL && x == y->lchild){
x = y;
y = y->parent;
}
return y;
}
ltree tree_successor(ltree x)
{
ltree y;
if (x->rchild != NULL)
return tree_minimum(x->rchild);
y = x->parent;
while (y != NULL && x == y->rchild){
x = y;
y = y->parent;
}
return y;
}
void tree_insert(ltree T, ltree z)
{
ltree y = NULL;
ltree x;
x = T;
while (x != NULL){
y = x;
if (z->data < x->data)
x = x->lchild;
else
x = x->rchild;
}
z->parent = y;
if (y == NULL)
T = z;
else{
if (z->data < y->data)
y->lchild = z;
else
y->rchild = z;
}
}
void tree_delete(ltree T, ltree z)
{
ltree x;
int y;
if (z->lchild == NULL && z->rchild == NULL)//z has no child
{
x = z->parent;
if (x == NULL)//z is rooot
T = NULL;
else{
if (x->lchild == z)
x->lchild = NULL;
if (x->rchild == z)
x->rchild = NULL;
}
}
if ((z->lchild == NULL && z->rchild != NULL) || (z->lchild != NULL && z->rchild == NULL))//z has only one child
{
if (z->lchild != NULL){
z->lchild->parent = z->parent;
if (z->parent != NULL){//z is not root
if (z->parent->lchild == z){//z is the left child of its parent
z->parent->lchild = z->lchild;
}else{
z->parent->rchild = z->lchild;
}
}else{
T = z->lchild;
}
}else{
z->rchild->parent = z->parent;
if (z->parent != NULL){
if (z->parent->lchild == z){
z->parent->lchild = z->rchild;
}else{
z->parent->rchild = z->rchild;
}
}else{
T = z->rchild;
}
}
}
if (z->lchild != NULL && z->rchild != NULL)//z has tow children
{
x = tree_successor(z);//find z's successor and delete it
y = x->data;//get data
tree_delete(T, x);//delete the successor
z->data = y;
}
}