Repository navigation
Expand file tree
/
Copy pathbinarysearchtree.h
More file actions
330 lines (304 loc) · 9.07 KB
/
Copy pathbinarysearchtree.h
File metadata and controls
330 lines (304 loc) · 9.07 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
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
#ifndef BINARYSEARCHTREE_JL
#define BINARYSEARCHTREE_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// Define how our tree node is structured.
typedef struct TreeNode{
int data; // The value of the node that we need to store.
struct TreeNode* left; // The pointer to the left child (smaller values).
struct TreeNode* right; // The pointer to the right child (greater values).
} TreeNode;
// Define how our (non linear and dynamic) binary search tree is structured.
typedef struct{
TreeNode* root; // The pointer to the first node (root) of our tree.
int size; // The number of elements (nodes) in our tree.
} BinarySearchTree;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Binary Search Tree. We start setting the root pointer value to null and size to 0.
void initBST(BinarySearchTree *tree){
tree->root = NULL;
tree->size = 0;
}
// Create a New Tree Node. Returns a newNode value.
TreeNode* createTreeNode(int value){
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
if(!newNode){
return NULL;
}
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Check if the Tree is Empty. Returns 1 if it's empty or 0 if it has elements.
int isEmptyBST(BinarySearchTree *tree){
if(tree->root == NULL){
return 1;
}
else{
return 0;
}
}
// Insert a Value in the Tree. Returns 1 if successful or 0 if the value already exists or the new node is not created.
int insertBST(BinarySearchTree *tree, int value){
TreeNode* temp = tree->root;
TreeNode* parent = NULL;
while(temp != NULL){
if(value == temp->data){
return 0;
}
parent = temp;
if(value < temp->data){
temp = temp->left;
}
else{
temp = temp->right;
}
}
TreeNode* newNode = createTreeNode(value);
if(!newNode){
return 0;
}
else{
if(parent == NULL){
tree->root = newNode;
}
else if(value < parent->data){
parent->left = newNode;
}
else{
parent->right = newNode;
}
tree->size++;
return 1;
}
}
// Search for a Value in the Tree. Returns 1 if found or 0 if it's not.
int searchBST(BinarySearchTree *tree, int value){
TreeNode* temp = tree->root;
while(temp != NULL){
if(value == temp->data){
return 1;
}
if(value < temp->data){
temp = temp->left;
}
else{
temp = temp->right;
}
}
return 0;
}
// Get the Minimum Value of the Tree (the leftmost node). Returns 1 if successful or 0 if the tree is empty.
int minBST(BinarySearchTree *tree, int *value){
if(isEmptyBST(tree)){
return 0;
}
else{
TreeNode* temp = tree->root;
while(temp->left != NULL){
temp = temp->left;
}
*value = temp->data;
return 1;
}
}
// Get the Maximum Value of the Tree (the rightmost node). Returns 1 if successful or 0 if the tree is empty.
int maxBST(BinarySearchTree *tree, int *value){
if(isEmptyBST(tree)){
return 0;
}
else{
TreeNode* temp = tree->root;
while(temp->right != NULL){
temp = temp->right;
}
*value = temp->data;
return 1;
}
}
// Calculate the Height of a Node. Returns the number of levels below it (0 if the node is null).
int heightNode(TreeNode *node){
if(node == NULL){
return 0;
}
else{
int leftHeight = heightNode(node->left);
int rightHeight = heightNode(node->right);
if(leftHeight > rightHeight){
return leftHeight+1;
}
else{
return rightHeight+1;
}
}
}
// Calculate the Height of the Tree. Returns the number of levels (0 if the tree is empty).
int heightBST(BinarySearchTree *tree){
return heightNode(tree->root);
}
// Delete a Node by Value. Returns 1 if successful or 0 if the value is not found.
int deleteBST(BinarySearchTree *tree, int value){
TreeNode* temp = tree->root;
TreeNode* parent = NULL;
while(temp != NULL && temp->data != value){
parent = temp;
if(value < temp->data){
temp = temp->left;
}
else{
temp = temp->right;
}
}
if(temp == NULL){
return 0;
}
else{
// If the node has two children, we copy the next value (in-order successor) and delete that node instead.
if(temp->left != NULL && temp->right != NULL){
TreeNode* successor = temp->right;
TreeNode* successorParent = temp;
while(successor->left != NULL){
successorParent = successor;
successor = successor->left;
}
temp->data = successor->data;
temp = successor;
parent = successorParent;
}
// At this point the node has one child at most.
TreeNode* child = NULL;
if(temp->left != NULL){
child = temp->left;
}
else{
child = temp->right;
}
if(parent == NULL){
tree->root = child;
}
else if(parent->left == temp){
parent->left = child;
}
else{
parent->right = child;
}
free(temp);
tree->size--;
return 1;
}
}
// Print a Value followed by a Comma if it's not the Last One.
void printTreeValue(int value, int *printed, int total){
printf("%d", value);
(*printed)++;
if(*printed < total){
printf(", ");
}
}
// Visit the Nodes In-Order (left, node, right).
void inOrderNode(TreeNode *node, int *printed, int total){
if(node != NULL){
inOrderNode(node->left, printed, total);
printTreeValue(node->data, printed, total);
inOrderNode(node->right, printed, total);
}
}
// Visit the Nodes Pre-Order (node, left, right).
void preOrderNode(TreeNode *node, int *printed, int total){
if(node != NULL){
printTreeValue(node->data, printed, total);
preOrderNode(node->left, printed, total);
preOrderNode(node->right, printed, total);
}
}
// Visit the Nodes Post-Order (left, right, node).
void postOrderNode(TreeNode *node, int *printed, int total){
if(node != NULL){
postOrderNode(node->left, printed, total);
postOrderNode(node->right, printed, total);
printTreeValue(node->data, printed, total);
}
}
// Print all Elements of the Tree In-Order. Displays elements from the smallest to the greatest one.
void printInOrder(BinarySearchTree *tree){
int printed = 0;
printf("[");
inOrderNode(tree->root, &printed, tree->size);
printf("]\n");
}
// Print all Elements of the Tree Pre-Order. Displays the root first and then its children.
void printPreOrder(BinarySearchTree *tree){
int printed = 0;
printf("[");
preOrderNode(tree->root, &printed, tree->size);
printf("]\n");
}
// Print all Elements of the Tree Post-Order. Displays the children first and then the root.
void printPostOrder(BinarySearchTree *tree){
int printed = 0;
printf("[");
postOrderNode(tree->root, &printed, tree->size);
printf("]\n");
}
// Copy the Values of the Tree Level by Level (from top to bottom, left to right) into an Array.
// It works like the Queue (front and rear indexes), but the queue stores node pointers instead of int values.
// Returns 1 if successful or 0 if there's no memory for the queue. The array must have space for tree->size values.
int levelOrderBST(BinarySearchTree *tree, int *output){
if(isEmptyBST(tree)){
return 1;
}
TreeNode** queue = (TreeNode**)malloc(tree->size * sizeof(TreeNode*));
if(!queue){
return 0;
}
int front = 0;
int rear = 0;
queue[rear++] = tree->root;
while(front < rear){
TreeNode* node = queue[front++];
output[front-1] = node->data;
if(node->left != NULL){
queue[rear++] = node->left;
}
if(node->right != NULL){
queue[rear++] = node->right;
}
}
free(queue);
return 1;
}
// Print all Elements of the Tree Level by Level. Displays the root first, then its children, then its grandchildren...
void printLevelOrder(BinarySearchTree *tree){
int* values = (int*)malloc((tree->size > 0 ? tree->size : 1) * sizeof(int));
printf("[");
if(values && levelOrderBST(tree, values)){
for(int i = 0; i < tree->size; i++){
printf("%d", values[i]);
if(i < tree->size-1){
printf(", ");
}
}
}
printf("]\n");
free(values);
}
// Free a Node and all the Nodes Below It.
void freeTreeNode(TreeNode *node){
if(node != NULL){
freeTreeNode(node->left);
freeTreeNode(node->right);
free(node);
}
}
// Free all the Nodes and Reset the Tree.
void freeBST(BinarySearchTree *tree){
freeTreeNode(tree->root);
tree->root = NULL;
tree->size = 0;
}
#endif //BINARYSEARCHTREE_JL