-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_functions.c
More file actions
295 lines (257 loc) · 7.04 KB
/
basic_functions.c
File metadata and controls
295 lines (257 loc) · 7.04 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
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
#include <stdio.h>
#include <stdlib.h>
/* DEFINITIONS */
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
// binary tree struct
typedef struct node
{
int n;
struct node* left;
struct node* right;
} node;
/* FUNCTIONS */
/**
* binary tree search function (returns 0 for failure, 1 for success)
*/
int search(int value, node* treepointer)
{
if (treepointer == NULL)
{
return 0;
}
else if (value < treepointer->n)
{
return search(value, treepointer->left);
}
else if (value > treepointer->n)
{
return search(value, treepointer->right);
}
else
{
return 1;
}
}
/**
* binary tree add element function (returns tree root)
*/
node* insert(int value, node** treepointer)
{
if (*treepointer == NULL)
{
*treepointer = malloc(sizeof(node));
(*treepointer)->n = value;
}
else if (value <= (*treepointer)->n)
{
(*treepointer)->left = insert(value, &(*treepointer)->left);
}
else
{
(*treepointer)->right = insert(value, &(*treepointer)->right);
}
return *treepointer;
}
// binary tree remove element helper prototype
node* deletHelper(node* deletThis);
/**
* binary tree remove element function (returns 0 for failure, 1 for success)
*/
int delet(int value, node** treepointer)
{
// declare pointer which will identify the node to delete
node* child = (*treepointer);
// declare pointer which will track parent node (makes tree easier to fix after deletion)
node* parent = (*treepointer);
// declare integer which will track the path of said parent to child (left or right)
int isLeft = 0;
// while value undiscovered
while (child->n != value)
{
// update parent to child
parent = child;
// update child to next descendant and update path taken
if (value <= child->n)
{
isLeft = 1;
child = child->left;
}
else
{
isLeft = 0;
child = child->right;
}
// if NULL is reached, node with given value does not exist in tree
if (child == NULL)
{
return 0;
}
}
// if while loop exits then node is discovered and needs to be deleted
// if node is a leaf...
if (child->left == NULL && child->right == NULL)
{
// if node is the root then delete tree
if (child == (*treepointer))
{
// no action required
;
}
// else delete edge pointing to node
else
{
if (isLeft == 1)
{
parent->left = NULL;
}
else
{
parent->right = NULL;
}
}
}
// else if node has one child, update parent to point to descendant
else if (child->right == NULL)
{
if (child == (*treepointer))
{
(*treepointer) = child->left;
}
else
{
if (isLeft == 1)
{
parent->left = child->left;
}
else
{
parent->right = child->left;
}
}
}
else if (child->left == NULL)
{
if (child == (*treepointer))
{
(*treepointer) = child->right;
}
else
{
if (isLeft == 1)
{
parent->left = child->right;
}
else
{
parent->right = child->right;
}
}
}
// else if node to delete has two children
else
{
// identify a suitable replacement node
node* replacement = deletHelper(child);
// make root replacement or update parent to point to replacement
if (child == (*treepointer))
{
(*treepointer) = replacement;
}
else
{
if (isLeft == 1)
{
parent->left = replacement;
}
else
{
parent->right = replacement;
}
}
// update replacement to point to left subtree (helper takes care of right subtree)
replacement->left = child->left;
}
// free memory
free(child);
// return success
return 1;
}
/**
* binary tree remove element helper function (returns replacement node)
*/
node* deletHelper(node* deletThis)
{
// declare replacement ("child") pointer
node* replacement = NULL;
// declare pointer to track parent
node* parent = NULL;
// declare pointer to search grandchildren
node* grandchild = deletThis->right;
// while there are descendants to search...
while (grandchild != NULL)
{
// update parent to child
parent = replacement;
// update child to grandchild
replacement = grandchild;
// search left grandchild
grandchild = grandchild->left;
}
/**
* note: the decision to go right and then left to find a replacement is
* arbitrary; going left and then right also works and may have produced
* a more 'suitable' replacement depending on the size of both branches
*/
// if replacement is a node other than the node to delete's immediate child...
if (replacement != deletThis->right)
{
// update parent->left to replacement->right (there are no left children as per above loop)
parent->left = replacement->right;
// update replacement->right to deletThis->right (preserves subtree)
replacement->right = deletThis->right;
}
// return replacement to main function
return replacement;
}
/**
* tree height function (returns 0 for empty tree, else the number of levels)
*/
int height(node* treepointer)
{
if (treepointer == NULL)
{
return 0;
}
else
{
return MAX(height(treepointer->right), height(treepointer->left)) + 1;
}
}
/**
* free memory function (post order traversal to visit all branches before the root)
*/
void freetree(node* treepointer)
{
if (treepointer != NULL)
{
freetree(treepointer->left);
freetree(treepointer->right);
free(treepointer);
}
}
/**
* A note on function design for node insertion and deletion:
*
* In C, arguments are passed by value, which means functions receive copies
* of variables rather than the variables themselves. If an argument is changed
* inside a function, the change is lost unless it is passed back to the caller.
*
* This makes the implementation of node insertion and deletion more difficult
* when we want to alter the address of the node passed to the function (which
* is a copy of the real address).
*
* This problem is overcome (as in the functions above) by designing the functions
* to accept a pointer to a pointer (**) and passing the address (&) of the declared
* node* pointer in main. The code looks a little messier, but editing the address
* of the node (node*) is made much easier.
*/