-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.c
173 lines (162 loc) · 4.84 KB
/
binary_search_tree.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
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "binary_search_tree.h"
/* this returns a bool because it should return false if there is an
error allocating more memory.. but i didnt actually implement that
*/
bool insertRecursive_BinarySearchTree(BSTNode **tree, int value) {
if (*tree == NULL) {
BSTNode *newNode = malloc(sizeof(BSTNode));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
*tree = newNode;
return true;
}
if (value < (*tree)->item) {
insertRecursive_BinarySearchTree(&(*tree)->left, value);
} else {
insertRecursive_BinarySearchTree(&(*tree)->right, value);
}
return true;
}
bool insert_BinarySearchTree(BSTNode **tree, int value) {
BSTNode *treePtr = *tree;
BSTNode *newNode = malloc(sizeof(BSTNode));
BSTNode *newNodeParent = NULL;
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
while (treePtr != NULL) {
newNodeParent = treePtr;
if ((treePtr)->item > value) {
treePtr = (treePtr)->left;
} else {
treePtr = (treePtr)->right;
}
}
if (newNodeParent == NULL) {
*tree = newNode;
} else {
if (newNodeParent->item > value) {
newNodeParent->left = newNode;
} else {
newNodeParent->right = newNode;
}
}
return true;
}
BSTNode* search_BinarySearchTree(BSTNode *tree, int value) {
if (tree == NULL) {
return NULL;
}
if (tree->item == value) {
return tree;
}
if (value < tree->item) {
return search_BinarySearchTree(tree->left, value);
}
return search_BinarySearchTree(tree->right, value);
}
BSTNode* minimum_BinarySearchTree(BSTNode *tree){
BSTNode* minimum;
if (tree == NULL) {
return NULL;
}
minimum = tree;
while (minimum->left != NULL) {
minimum = minimum->left;
}
return minimum;
}
BSTNode* maximum_BinarySearchTree(BSTNode *tree){
BSTNode* maximum;
if (tree == NULL) {
return NULL;
}
maximum = tree;
while (maximum->right != NULL) {
maximum = maximum->right;
}
return maximum;
}
/* Returns the node that was deleted */
BSTNode* deleteValue_BinarySearchTree(BSTNode *tree, int value) {
if (tree == NULL) {
return tree;
}
if (value < tree->item) {
tree->left = deleteValue_BinarySearchTree(tree->left, value);
}
else if (value > tree->item) {
tree->right = deleteValue_BinarySearchTree(tree->right, value);
}
else {
if (tree->left == NULL) {
BSTNode* temp = tree->left;
free(tree);
return temp;
}
else if (tree->right == NULL) {
BSTNode* temp = tree->right;
free(tree);
return temp;
}
BSTNode* temp = minimum_BinarySearchTree(tree->right);
tree->item = temp->item;
tree->right = deleteValue_BinarySearchTree(tree->right, temp->item);
}
return tree;
}
static int heightIter_BinarySearchTree(BSTNode *tree, int currentHeight, int maxHeight) {
if (tree == NULL) {
return maxHeight;
}
currentHeight++;
if (currentHeight > maxHeight) {
return heightIter_BinarySearchTree(tree->left, currentHeight, currentHeight),
heightIter_BinarySearchTree(tree->right, currentHeight, currentHeight);
} else {
return heightIter_BinarySearchTree(tree->left, currentHeight, maxHeight),
heightIter_BinarySearchTree(tree->right, currentHeight, maxHeight);
}
return maxHeight;
}
int height_BinarySearchTree(BSTNode *tree) {
return heightIter_BinarySearchTree(tree, 0, 0);
}
void print_BinarySearchTree(BSTNode *tree, int row) {
if (tree == NULL) {
return;
}
print_BinarySearchTree(tree->right, row+2);
printf("\n ");
for (int i = 0 ; i < row; i ++) {
printf(" ");
}
printf("%d", tree->item);
print_BinarySearchTree(tree->left, row+2);
}
void inOrderTraversal_BinarySearchTree(BSTNode* tree, void (*callback)(BSTNode node)) {
if (tree != NULL) {
inOrderTraversal_BinarySearchTree(tree->left, callback);
callback(*tree);
inOrderTraversal_BinarySearchTree(tree->right, callback);
}
}
void preOrderTraversal_BinarySearchTree(BSTNode* tree, void (*callback)(BSTNode node)) {
if (tree != NULL) {
callback(*tree);
preOrderTraversal_BinarySearchTree(tree->left, callback);
preOrderTraversal_BinarySearchTree(tree->right, callback);
}
}
void postOrderTraversal_BinarySearchTree(BSTNode* tree, void (*callback)(BSTNode node)) {
if (tree != NULL) {
postOrderTraversal_BinarySearchTree(tree->left, callback);
postOrderTraversal_BinarySearchTree(tree->right, callback);
callback(*tree);
}
}