-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtree-demo.c
234 lines (191 loc) · 5.11 KB
/
tree-demo.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
/* tree-demo.c
Driver for a binary search tree container implementation.
by: Steven Skiena
begun: September 5, 2005
*/
/*
Copyright 2005 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "bool.h"
#include "tree.h"
tree *parent; /* last node visited */
tree *init_tree(void) {
return(NULL);
}
bool empty_tree(tree *t) {
if (t == NULL) {
return (TRUE);
}
return (FALSE);
}
/* [[[ search_tree_cut */
tree *search_tree(tree *l, item_type x) {
if (l == NULL) {
return(NULL);
}
if (l->item == x) {
return(l);
}
if (x < l->item) {
return(search_tree(l->left, x));
} else {
return(search_tree(l->right, x));
}
}
/* ]]] */
/* [[[ insert_tree_cut */
void insert_tree(tree **l, item_type x, tree *parent) {
tree *p; /* temporary pointer */
if (*l == NULL) {
p = malloc(sizeof(tree));
p->item = x;
p->left = p->right = NULL;
p->parent = parent;
*l = p;
return;
}
if (x < (*l)->item) {
insert_tree(&((*l)->left), x, *l);
} else {
insert_tree(&((*l)->right), x, *l);
}
}
/* ]]] */
void print_tree(tree *l) {
if (l != NULL) {
print_tree(l->left);
printf("%d ", l->item);
print_tree(l->right);
}
}
tree *successor_descendant(tree *t) {
tree *succ; /* successor pointer */
if (t->right == NULL) {
return(NULL);
}
succ = t->right;
while (succ->left != NULL) {
succ = succ->left;
}
return(succ);
}
/* [[[ find_minimum_cut */
tree *find_minimum(tree *t) {
tree *min; /* pointer to minimum */
if (t == NULL) {
return(NULL);
}
min = t;
while (min->left != NULL) {
min = min->left;
}
return(min);
}
/* ]]] */
tree *predecessor_descendant(tree *t) {
tree *pred; /* predecessor pointer */
if (t->left == NULL) {
return(NULL);
}
pred = t->left;
while (pred->right != NULL) {
pred = pred->right;
}
return(pred);
}
tree *delete_tree(tree *t, item_type x) {
tree *d; /* node with key to delete */
tree *p; /* node to be physically deleted */
item_type new_key; /* key to overwrite deleted key */
tree *child; /* d's only child, if any */
tree *search_tree();
d = search_tree(t, x);
if (d == NULL) {
printf("Warning: key to be deleted %d is not in tree.\n", x);
return(t);
}
if (d->parent == NULL) { /* if d is the root */
if ((d->left == NULL) && (d->right == NULL)) {
free(d);
return NULL; /* root-only tree */
}
if (d->left != NULL) { /* find node to physically delete */
p = predecessor_descendant(d);
} else {
p = successor_descendant(d);
}
} else {
if ((d->left == NULL) || (d->right == NULL)) {
/* d has <=1 child, so try to find non-null child */
if (d->left != NULL) {
child = d->left;
} else {
child = d->right;
}
if ((d->parent)->left == d) { /* fill null pointer */
d->parent->left = child;
} else {
d->parent->right = child;
}
if (child != NULL) {
child->parent = d->parent;
}
free(d);
return(t);
} else {
p = successor_descendant(d); /* p has 2 children */
}
}
new_key = p->item; /* deal with simpler case of deletion */
delete_tree(t, p->item);
d->item = new_key;
return (t);
}
int main(void) {
char c; /* input character */
item_type d; /* input item */
tree *l; /* tree under construction */
tree *tmp; /* returned tree from search */
tree *search_tree();
void insert_tree();
l = init_tree();
while (scanf("%c", &c) != EOF) {
if (tolower(c) == 'p') {
print_tree(l);
printf("\n");
}
if (tolower(c) == 'i') {
scanf("%d", &d);
printf("new item: %d\n", d);
insert_tree(&l, d, NULL);
}
if (tolower(c) == 's') {
scanf("%d", &d);
tmp = search_tree(l, d);
if (tmp == NULL) {
printf("item %d not found\n",d);
} else {
printf("item %d found\n",d);
}
}
if (tolower(c) == 'd') {
scanf("%d", &d);
printf(" deleting item %d\n", d);
l = delete_tree(l, d);
print_tree(l);
printf("\n");
}
}
return 0;
}