-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.c
521 lines (419 loc) · 12.2 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* This file contains an implementation of the basic BST functions prototyped
* in bst.h. At the bottom of this file is where you will implement your
* functions for this assignment. Make sure to add your name and
* @oregonstate.edu email address below:
*
* Name:
* Email:
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "bst.h"
/*
* This structure represents a single node in a BST.
*/
struct bst_node {
int val;
struct bst_node* left;
struct bst_node* right;
};
/*
* This structure represents an entire BST. Note that we only need a
* reference to the root node of the tree.
*/
struct bst {
struct bst_node* root;
};
struct bst* bst_create() {
struct bst* bst = malloc(sizeof(struct bst));
assert(bst);
bst->root = NULL;
return bst;
}
void bst_free(struct bst* bst) {
assert(bst);
/*
* Assume that bst_remove() frees each node it removes and use it to free
* all of the nodes in the tree.
*/
while (!bst_isempty(bst)) {
bst_remove(bst->root->val, bst);
}
free(bst);
}
int bst_isempty(struct bst* bst) {
assert(bst);
return bst->root == NULL;
}
/*
* Helper function to generate a single BST node containing a given value.
*/
struct bst_node* _bst_node_create(int val) {
struct bst_node* n = malloc(sizeof(struct bst_node));
assert(n);
n->val = val;
n->left = n->right = NULL;
return n;
}
/*
* Helper function to insert a given value into a subtree of a BST rooted at
* a given node. Operates recursively by determining into which subtree (left
* or right) under the given node the value should be inserted and performing
* the insertion on that subtree.
*
* Returns the root of the given subtree, modified to contain a new node with
* the specified value.
*/
struct bst_node* _bst_subtree_insert(int val, struct bst_node* n) {
if (n == NULL) {
/*
* If n is NULL, we know we've reached a place to insert val, so we
* create a new node holding val and return it.
*/
return _bst_node_create(val);
} else if (val < n->val) {
/*
* If val is less than the value at n, we insert val in n's left subtree
* (somewhere) and update n->left to point to the modified subtree (with
* val inserted).
*/
n->left = _bst_subtree_insert(val, n->left);
} else {
/*
* If val is greater than or equal to the value at n, we insert val in n's
* right subtree (somewhere) and update n->right to point to the modified
* subtree (with val inserted).
*/
n->right = _bst_subtree_insert(val, n->right);
}
/*
* For the else if and else conditions, the subtree rooted at n has already
* been modified (by setting n->left or n->right above), so we can just
* return n here.
*/
return n;
}
void bst_insert(int val, struct bst* bst) {
assert(bst);
/*
* We insert val by using our subtree insertion function starting with the
* subtree rooted at bst->root (i.e. the whole tree).
*/
bst->root = _bst_subtree_insert(val, bst->root);
}
/*
* Helper function to return the minimum value in a subtree of a BST.
*/
int _bst_subtree_min_val(struct bst_node* n) {
/*
* The minimum value in any subtree is just the leftmost value. Keep going
* left till we get there.
*/
while (n->left != NULL) {
n = n->left;
}
return n->val;
}
/*
* Helper function to remove a given value from a subtree of a BST rooted at
* a specified node. Operates recursively by figuring out whether val is in
* the left or the right subtree of the specified node and performing the
* remove operation on that subtree.
*
* Returns the potentially new root of the given subtree, modified to have
* the specified value removed.
*/
struct bst_node* _bst_subtree_remove(int val, struct bst_node* n) {
if (n == NULL) {
/*
* If n is NULL, that means we've reached a leaf node without finding
* the value we wanted to remove. The tree won't be modified.
*/
return NULL;
} else if (val < n->val) {
/*
* If val is less than n, remove val from n's left subtree and update
* n->left to point to the modified subtree (with val removed). Return n,
* whose subtree itself has now been modified.
*/
n->left = _bst_subtree_remove(val, n->left);
return n;
} else if (val > n->val) {
/*
* If val is greater than n, remove val from n's right subtree and update
* n->right to point to the modified subtree (with val removed). Return n,
* whose subtree itself has now been modified.
*/
n->right = _bst_subtree_remove(val, n->right);
return n;
} else {
/*
* If we've reached this point, we've found a node with value val. We
* need to remove this node from the tree, and the way we do that will
* differ based on whether the node has 0, 1, or 2 children.
*/
if (n->left != NULL && n->right != NULL) {
/*
* If n has 2 children, we replace the value at n with the value at n's
* in-order successor node, which is the minimum value in n's right
* subtree. Then we recursively remove n's in-order successor node from
* the tree (specifically from n's right subtree).
*/
n->val = _bst_subtree_min_val(n->right);
n->right = _bst_subtree_remove(n->val, n->right);
return n;
} else if (n->left != NULL) {
/*
* If n has only a left child, we simply delete n by freeing it and
* returning the left child node so that it becomes the new child of
* n's parent via the recursion.
*/
struct bst_node* left_child = n->left;
free(n);
return left_child;
} else if (n->right != NULL) {
/*
* If n has only a right child, we simply delete n by freeing it and
* returning the right child node so that it becomes the new child of
* n's parent via the recursion.
*/
struct bst_node* right_child = n->right;
free(n);
return right_child;
} else {
/*
* Otherwise, n has no children, and we can simply free it and return
* NULL so that n's parent will lose n as a child via the recursion.
*/
free(n);
return NULL;
}
}
}
void bst_remove(int val, struct bst* bst) {
assert(bst);
/*
* We remove val by using our subtree removal function starting with the
* subtree rooted at bst->root (i.e. the whole tree).
*/
bst->root = _bst_subtree_remove(val, bst->root);
}
int bst_contains(int val, struct bst* bst) {
assert(bst);
// Iteratively search for val in bst.
struct bst_node* cur = bst->root;
while (cur != NULL) {
if (val == cur->val) {
// We found the value we're looking for in cur.
return 1;
} else if (val < cur->val) {
/*
* The value we're looking for is less than the value at cur, so we
* branch left.
*/
cur = cur->left;
} else {
/*
* The value we're looking for is greater than or equal to the value at
* cur, so we branch right.
*/
cur = cur->right;
}
}
/*
* If we make it to a leaf node (i.e. cur is NULL), we didn't find what we
* were looking for.
*/
return 0;
}
/*****************************************************************************
*
* Below are the functions and structures you'll implement in this assignment.
*
*****************************************************************************/
/*
* This is the structure you will use to create an in-order BST iterator. It=============================================================================================
* is up to you how to define this structure.
*/
struct ll_node{
int val;
struct ll_node* next;
};
struct bst_iterator{
struct ll_node* head;
struct ll_node* curr;
};
/*
* This function should return the total number of elements stored in a given
* BST.
*
* Params:
* bst - the BST whose elements are to be counted
*
* Return:
* Should return the total number of elements stored in bst.
*/
int bst_size(struct bst* bst){
printf("errorhere");
return bst_size2(bst->root);
}
int bst_size2(struct bst_node* bst){
int count = 1;
if(bst->left){
count += bst_size2(bst->left);
}
if(bst->right){
count += bst_size2(bst->right);
}
return count;
}
/*
* This function should return the height of a given BST, which is the maximum
* depth of any node in the tree (i.e. the number of edges in the path from
* the root to that node). Note that the height of an empty tree is -1 by
* convention.
*
* Params:
* bst - the BST whose height is to be computed
*
* Return:
* Should return the height of bst.
*/
int bst_height(struct bst* bst){
return bst_height2(bst->root);
}
int bst_height2(struct bst_node* bst){
int countl = 1;
int countr = 1;
if(bst->left){
countl += bst_height2(bst->left);
}
if(bst->right){
countr += bst_height2(bst->right);
}
if(countl > countr){
countr = countl;
}
return countr;
}
/*
* This function should determine whether a given BST contains a path from the
* root to a leaf in which the node values sum to a specified value.
*
* Params:
* sum - the value to search for among the path sums of bst
* bst - the BST whose paths sums to search
*
* Return:
* Should return 1 if bst contains a path from the root to a leaf in which
* the values of the nodes add up to sum. Should return 0 otherwise.
*/
int bst_path_sum(int sum, struct bst* bst) {
return bst_path_sum2(sum, bst->root);
}
int bst_path_sum2(int sum, struct bst_node* bst){
int valFound = 0;
sum -= bst->val;
if(bst->left){
valFound += bst_path_sum2(sum, bst->left);
}
if(bst->right){
valFound += bst_path_sum2(sum, bst->right);
}
if(!bst->left && !bst->right && sum == 0){
valFound++;
}
return valFound;
}
/*
* This function should allocate and initialize a new in-order BST iterator
* given a specific BST over which to iterate.
*
* Params:
* bst - the BST over which to perform in-order iteration. May not be NULL.
*
* Return:
* Should return a pointer to a new in-order BST iterator, initialized so
* that the first value returned by bst_iterator_next() is the first in-order
* value in bst (i.e. the leftmost value in the tree).
*/
struct bst_iterator* bst_iterator_create(struct bst* bst) {
struct bst_iterator* list = malloc(sizeof(struct bst_iterator));
struct ll_node* temp = malloc(sizeof(struct ll_node));
list->head = temp;
ll_fill(bst->root, list->head);
list->curr = list->head;
while(list->curr->next != NULL){
list->curr = list->curr->next;
}
free(list->curr);
list->curr = NULL;
list->curr = list->head;
return list;
}
void ll_fill(struct bst_node* copyFrom, struct ll_node* copyTo){
if(copyFrom->left != NULL){
ll_fill(copyFrom->left, copyTo);
while(copyTo->next != NULL){
copyTo = copyTo->next;
}
}
copyTo->val = copyFrom->val;
struct ll_node* temp = malloc(sizeof(struct ll_node));
temp->next = NULL;
copyTo->next = temp;
copyTo = copyTo->next;
if(copyFrom->right != NULL){
ll_fill(copyFrom->right, copyTo);
while(copyTo->next != NULL){
copyTo = copyTo->next;
}
}
return;
}
/*
* This function should free all memory allocated to a BST iterator.
*
* Params:
* iter - the iterator whose memory is to be freed. May not be NULL.
*/
void bst_iterator_free(struct bst_iterator* iter){
struct ll_node* curr = iter->head;
struct ll_node* prev;
while(curr != NULL){
prev = curr;
curr = curr->next;
free(prev);
}
free(iter);
}
/*
* This function should return 1 if there is at least one more node to visit
* in the in-order iteration of the BST represented by a given iterator. If
* there are no more nodes to visit, it should return 0.
*
* Params:
* iter - the iterator to be checked for more values. May not be NULL.
*/
int bst_iterator_has_next(struct bst_iterator* iter){
int temp = 0;
if(iter->curr->next != NULL){
temp = 1;
}
return temp;
}
/*
* This function should return the next value in the in-order iteration of the
* BST represented by a given iterator.
*
* Params:
* iter - the iterator whose next value is to be returned. May not be NULL
* and must have at least one more value to be returned.
*/
int bst_iterator_next(struct bst_iterator* iter){
int temp = iter->curr->val;
iter->curr = iter->curr->next;
return temp;
}