-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbst.c
197 lines (181 loc) · 4.93 KB
/
bst.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
/*
TODO factor out test data with C++.
TODO create a top level object so that we can have an
empty method. currently NULL is used.
*/
#include "common.h"
typedef struct Bst Bst;
struct Bst {
Bst *right;
Bst *left;
int key;
int value;
};
int bst_add(Bst **bst, int key, int value) {
int cur_key;
int done = 0;
int present = 0;
Bst *cur = *bst;
if (cur == NULL) {
*bst = malloc(sizeof(Bst));
cur = *bst;
} else {
while (!done) {
cur_key = cur->key;
if (cur_key == key) {
done = 1;
present = 1;
} else if (cur_key < key) {
if (cur->left == NULL) {
cur->left = malloc(sizeof(Bst));
done = 1;
}
cur = cur->left;
} else {
if (cur->right == NULL) {
cur->right = malloc(sizeof(Bst));
done = 1;
}
cur = cur->right;
}
}
}
cur->key = key;
cur->value = value;
return present;
}
int bst_find(const Bst *bst, int key, int *value) {
while (bst != NULL) {
if (bst->key == key) {
*value = bst->value;
return 1;
} else if (bst->key < key) {
bst = bst->left;
} else {
bst = bst->right;
}
}
return 0;
}
void bst_free(Bst **bst) {
Bst *cur = *bst;
if (cur != NULL) {
bst_free(&(cur->left));
bst_free(&(cur->right));
free(cur);
*bst = NULL;
}
}
int bst_remove(Bst **bst, int key) {
int cur_key;
int has_left, has_right;
int is_left_child;
int value;
Bst *cur, *parent, *left, *right, *next;
cur = *bst;
parent = NULL;
while (cur != NULL) {
cur_key = cur->key;
if (cur_key == key) {
left = cur->left;
right = cur->right;
has_left = left != NULL;
has_right = right != NULL;
if (has_left && has_right) {
parent = cur;
next = right;
while (next->left != NULL) {
parent = next;
next = next->left;
}
value = next->value;
bst_remove(&parent, value);
cur->value = value;
} else if (has_left) {
if (parent == NULL) {
*bst = parent->left;
} else {
if (is_left_child)
parent->left = left;
else
parent->right = left;
}
free(cur);
} else if (has_right) {
if (parent == NULL) {
*bst = parent->right;
} else {
if (is_left_child)
parent->left = right;
else
parent->right = right;
}
free(cur);
} else {
if (parent == NULL) {
*bst = NULL;
} else {
if (is_left_child)
parent->left = NULL;
else
parent->right = NULL;
}
free(cur);
}
return 1;
} else if (cur_key < key) {
parent = cur;
is_left_child = 1;
cur = cur->left;
} else {
parent = cur;
is_left_child = 0;
cur = cur->right;
}
}
return 0;
}
int main(void) {
Bst *bst = NULL;
int value, i;
/* find on empty bst. */
assert(!bst_find(bst, 0, &value));
/* Add to empty bst. */
assert(!bst_add(&bst, 0, 1));
/* Find on BST that contains a single element. */
assert(bst_find(bst, 0, &value));
assert(value == 1);
/* Update existing value. */
assert(bst_add(&bst, 0, 2));
assert(bst_find(bst, 0, &value));
assert(value == 2);
/*
Operations on a more complex tree:
0
-2 2
-3 -1 1 3
*/
bst_free(&bst);
bst = NULL;
assert(!bst_add(&bst, 0, 1));
assert(!bst_add(&bst, -2, -1));
assert(!bst_add(&bst, 2, 3));
assert(!bst_add(&bst, -3, -2));
assert(!bst_add(&bst, -1, 0));
assert(!bst_add(&bst, 1, 2));
assert(!bst_add(&bst, 3, 4));
for (i = -3; i < 4; ++i) {
assert(bst_find(bst, i, &value));
assert(value == i + 1);
}
assert(!bst_find(bst, -4, &value));
assert(!bst_find(bst, 4, &value));
/* remove. TODO. This is too hard for interviews. */
/*bst_remove(&bst, 0);*/
/*assert(!bst_find(bst, 0, &value));*/
/*assert(bst_find(bst, -1, &value));*/
/*assert(value == 0);*/
/*assert(bst_find(bst, 1, &value));*/
/*assert(value == 2);*/
return EXIT_SUCCESS;
}