-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.c
252 lines (184 loc) · 4.69 KB
/
binary_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
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
//@AUTHOR : Guilherme Cardoso Oliveira <[email protected]>
//@lICENSE: MIT
//@DATE : 2021-06-01
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define DATA_STRUCT_NAME "Binary Tree"
#define ERR_NO_MEMORY "Not enough memory"
//-----------------------------------------------------------
//DATA
typedef int Data;
#define DATA_FORMAT "%d"
//-----------------------------------------------------------
//NODE
typedef struct node {
Data data;
struct node *left; //child
struct node *right; //child
} * Node;
//-----------------------------------------------------------
//Binary Tree
typedef struct tree {
Node root;
} * Tree;
//-----------------------------------------------------------
//NODE METHODS
void error(char *msg) {
printf("\n[ERR] %s", msg);
exit(1);
}
bool empty(Node node) {
return node == NULL;
}
Node new_node(Data data) {
Node node = (Node) malloc(sizeof(struct node));
if(empty(node))
error(ERR_NO_MEMORY);
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
bool hasLeft(Node node) {
return !empty(node) && !empty(node->left);
}
bool hasRight(Node node) {
return !empty(node) && !empty(node->right);
}
bool hasOnlyLeft(Node node) {
return hasLeft(node) && !hasRight(node);
}
bool hasOnlyRight(Node node) {
return !hasLeft(node) && hasRight(node);
}
bool isLeaf(Node node) {
return !hasLeft(node) && !hasRight(node);
}
Node insert_node(Node current, Data data) {
if(empty(current))
return new_node(data);
if(data < current->data)
current->left = insert_node(current->left , data);
else
current->right = insert_node(current->right, data);
return current;
}
int count_node(Node current) {
if(empty(current)) return 0;
return 1 + count_node(current->left) + count_node(current->right);
}
int count_childrens(Node node) {
return hasLeft(node) + hasRight(node);
}
void print_node(Node current) {
if(empty(current)) return;
printf(DATA_FORMAT, current->data);
printf("(");
print_node(current->left);
print_node(current->right);
printf(") ");
}
bool exists_node(Node current, Data data) {
if(empty(current)) return false;
if(data < current->data)
return exists_node(current->left, data);
if(data > current->data)
return exists_node(current->right, data);
return true;
}
Node search_node(Node current, Data data, Node *parent) {
if(empty(current) || data == current->data)
return current;
*parent = current;
if(data < current->data)
return search_node(current->left , data, parent);
return search_node(current->right, data, parent);
}
Node kill_node(Node root, Node node, Node *children, Node new_children) {
free(node);
if(empty(root))
return new_children;
*children = new_children;
return root;
}
Node killNodeWithTwoChildrens(Node root, Node node, Node *children) {
Node ancestral = node;
Node new_children = ancestral->left;
while(!empty(new_children->right)) {
ancestral = new_children;
new_children = new_children->right;
}
if(ancestral != node) {
new_children->left = node->left;
ancestral->right = NULL;
}
new_children->right = node->right;
return kill_node(root, node, children, new_children);
}
Node remove_node(Node root, Data data) {
Node parent = NULL;
Node node = search_node(root, data, &parent);
Node *children;
if(node == root)
root = NULL; //flag
else if(node == parent->left)
children = &(parent->left);
else
children = &(parent->right);
if(empty(node))
return root;
if(isLeaf(node))
return kill_node(root, node, children, NULL);
if(hasOnlyLeft(node))
return kill_node(root, node, children, node->left);
if(hasOnlyRight(node))
return kill_node(root, node, children, node->right);
return killNodeWithTwoChildrens(root, node, children);
}
//-----------------------------------------------------------
//TREE METHODS
bool isEmpty(Tree tree) {
return tree == NULL;
}
Tree new_tree() {
Tree tree = (Tree) malloc(sizeof(struct tree));
if(isEmpty(tree))
error(ERR_NO_MEMORY);
tree->root = NULL;
return tree;
}
void insert(Tree tree, Data data) {
tree->root = insert_node(tree->root, data);
}
bool exists(Tree tree, Data data) {
return exists_node(tree->root, data);
}
int count(Tree tree) {
return count_node(tree->root);
}
void print(Tree tree) {
print_node(tree->root);
printf("\n");
}
void drop(Tree tree, Data data) {
tree->root = remove_node(tree->root, data);
}
//-----------------------------------------------------------
//MAIN
int main() {
Tree tree = new_tree();
insert(tree, 10);
insert(tree, 8);
insert(tree, 6);
insert(tree, 14);
insert(tree, 16);
insert(tree, 12);
insert(tree, 11);
insert(tree, 9);
print(tree);
drop(tree, 8);
print(tree);
system("pause");
return 0;
}