-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.c
201 lines (170 loc) · 4.53 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
/**
* \file bst.c
* \author Richard Degenne
* \date 17-09-15
*
* \brief Implementation file for the bst module
*
* This file defines the implementation of a binary search tree
* library.
*
* \see bst.h
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h> // For memcpy()
#include <assert.h>
#include "bst.h"
bst* bst_new(size_t data_size, orderFun compare, freeFun free_fun) {
assert(data_size > 0);
assert(compare);
bst* tree = malloc(sizeof(bst));
if(!tree) {
perror("malloc");
exit(EXIT_FAILURE);
}
tree->data_size = data_size;
tree->compare = compare;
tree->free = free_fun;
tree->root = NULL;
return tree;
}
void bst_destroy(bst* tree) {
assert(tree);
if(tree->root)
bst_destroy_rec(tree, tree->root);
free(tree);
}
static void bst_destroy_rec(bst* tree, bst_node* current) {
if(current->left)
bst_destroy_rec(tree, current->left);
if(current->right)
bst_destroy_rec(tree, current->right);
bst_destroy_node(tree, current);
}
static void bst_destroy_node(bst* tree, bst_node* node) {
if(tree->free)
tree->free(node->data);
free(node->data);
free(node);
}
void bst_add(bst* tree, void* element) {
assert(tree);
assert(element);
// Creating the new node
bst_node* node = bst_create_node(tree->data_size, element);
if(tree->root)
bst_add_rec(tree->compare, tree->root, node); // Start recursion
else {
tree->root = node; // Setting the new node as the tree's root
}
}
static void bst_add_rec(orderFun compare, bst_node* current, bst_node* new) {
if(compare(current->data, new->data) >= 0) // current >= new
if(!current->left) // current->left == NULL
current->left = new;
else
bst_add_rec(compare, current->left, new);
else // current < new
if(!current->right)
current->right = new;
else
bst_add_rec(compare, current->right, new);
}
static bst_node* bst_create_node(int data_size, void* element) {
bst_node* node = malloc(sizeof(bst_node)); // Allocating a node
if(!node) {
perror("malloc");
exit(EXIT_FAILURE);
}
node->data = malloc(data_size); // Allocating data
if(!(node->data)) {
perror("malloc");
exit(EXIT_FAILURE);
}
memcpy(node->data, element, data_size); // Setting data
node->left = NULL;
node->right = NULL;
return node;
}
int bst_size(bst* tree) {
assert(tree);
if(tree->root)
return bst_size_rec(tree->root);
else
return 0;
}
static int bst_size_rec(bst_node* current) {
if(!current) // current == NULL
return 0;
return 1 + bst_size_rec(current->left) + bst_size_rec(current->right);
}
bst_node* bst_search(bst* tree, void* element) {
assert(tree);
if(tree->root)
return bst_search_rec(tree->compare, tree->root, element);
return NULL;
}
static bst_node* bst_search_rec(orderFun compare, bst_node* current, void* element) {
if(!current)
return NULL;
if(compare(current->data, element) > 0) // current > element
bst_search_rec(compare, current->left, element);
else if(compare(current->data, element) < 0) // current < element
bst_search_rec(compare, current->right, element);
else
return current;
}
void bst_iter(bst* tree, iterFun function) {
assert(tree);
assert(function);
if(tree->root)
bst_iter_rec(tree->root, function);
}
static void bst_iter_rec(bst_node* current, iterFun function) {
if(!current)
return;
bst_iter_rec(current->left, function);
function(current->data);
bst_iter_rec(current->right, function);
}
void bst_remove(bst* tree, void* element) {
assert(tree);
if(tree->root)
bst_remove_rec(tree, tree->root, element);
}
static bst_node* bst_remove_rec(bst* tree, bst_node* current, void* element) {
if(!current)
return NULL;
if(tree->compare(current->data, element) > 0)
current->left = bst_remove_rec(tree, current->left, element);
else if(tree->compare(current->data, element) < 0)
current->right = bst_remove_rec(tree, current->right, element);
else {
if(!current->left && !current->right) { // No children
bst_destroy_node(tree, current);
return NULL;
}
else if(!current->right) { // Only a left child
bst_node* temp = current->left;
bst_destroy_node(tree, current);
return temp;
}
else if(!current->left) { // Only a right child
bst_node* temp = current->right;
bst_destroy_node(tree, current);
return temp;
}
else { // Left and right children
bst_node* successor = find_max(current->left);
memcpy(current->data, successor->data, tree->data_size);
current->left = bst_remove_rec(tree, current->left, successor->data);
}
}
return current;
}
static bst_node* find_max(bst_node* current) {
if(!current->right)
return current;
return find_max(current->right);
}