-
Notifications
You must be signed in to change notification settings - Fork 0
/
abb.c
238 lines (179 loc) · 4.69 KB
/
abb.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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include "abb.h"
static bool elem_eq(abb_elem a, abb_elem b)
{
return a == b;
}
static bool elem_less(abb_elem a, abb_elem b)
{
return a < b;
}
//---- Invariant
static bool check_greater (abb_elem e, abb tree){
bool greater = true;
if(tree != NULL){
greater = elem_less(tree->elem, e) && check_greater(e, tree->left)
&& check_greater(e, tree->right);
}
return greater;
}
static bool check_smaller (abb_elem e, abb tree){
bool smaller = true;
if(tree != NULL){
smaller = elem_less(e, tree->elem) && check_smaller(e, tree->left)
&& check_smaller(e, tree->right);
}
return smaller;
}
static bool invrep(abb tree){
bool valid = true;
if(tree == NULL){
valid = true;
} else {
valid = check_greater(tree->elem, tree->left) &&
check_smaller(tree->elem, tree->right) &&
invrep(tree->left) && invrep(tree->right);
}
return valid;
}
abb abb_empty(void){
abb tree;
tree = NULL;
assert(invrep(tree) && abb_is_empty(tree));
return tree;
}
abb abb_add(abb tree, abb_elem e){
assert(invrep(tree));
if(tree == NULL){
abb newTree;
newTree = malloc(sizeof(struct _s_abb));
newTree->elem = e;
newTree->left = NULL;
newTree->right = NULL;
tree = newTree;
} else {
if(elem_less(e, tree->elem)){
tree->left = abb_add(tree->left, e);
} else if (elem_less(tree->elem, e)){
tree->right = abb_add(tree->right, e);
}
}
assert(invrep(tree) && abb_exists(tree, e));
return tree;
}
bool abb_is_empty(abb tree)
{
bool is_empty = false;
assert(invrep(tree));
is_empty = (tree == NULL);
return is_empty;
}
bool abb_exists(abb tree, abb_elem e)
{
bool exists = false;
assert(invrep(tree));
exists = (tree!=NULL) && ((elem_eq(e, tree->elem)) ||
(elem_less(tree->elem, e) && abb_exists(tree->right, e)) ||
(elem_less(e, tree->elem) && abb_exists(tree->left, e)));
return exists;
}
unsigned int abb_length(abb tree){
unsigned int length = 0;
assert(invrep(tree));
if(tree != NULL){
length += abb_length(tree->left);
length++;
length += abb_length(tree->right);
}
assert(invrep(tree) && (abb_is_empty(tree) || length > 0));
return length;
}
abb abb_remove(abb tree, abb_elem e)
{
assert(invrep(tree));
if(tree==NULL){
return NULL;
}
/*busco el elemento*/
if (elem_less(e, tree->elem)){
tree->left = abb_remove(tree->left, e);
} else if (elem_less(tree->elem, e)) {
tree->right = abb_remove(tree->right, e);
}
else{
//leafs
if (tree->left == NULL && tree->right == NULL){
free(tree);
tree = NULL;
return tree;
}
else if (tree->left == NULL || tree->right == NULL){
abb temp;
if (tree->left == NULL){
temp = tree->right;
} else {
temp = tree->left;
}
free(tree);
tree = NULL;
return temp;
}
else{
abb_elem sucesor = abb_min(tree->right);
tree->elem = sucesor;
tree->right = abb_remove(tree->right,sucesor);
}
}
assert(invrep(tree) && !abb_exists(tree, e));
return tree;
}
abb_elem abb_root(abb tree)
{
abb_elem root;
assert(invrep(tree) && !abb_is_empty(tree));
root = tree->elem;
assert(abb_exists(tree, root));
return root;
}
abb_elem abb_max(abb tree){
abb_elem max_e;
assert(invrep(tree) && !abb_is_empty(tree));
while(tree->right!=NULL){
tree = tree->right;
}
max_e = tree->elem;
assert(invrep(tree) && abb_exists(tree, max_e));
return max_e;
}
abb_elem abb_min(abb tree){
abb_elem min_e;
assert(invrep(tree) && !abb_is_empty(tree));
while(tree->left!=NULL){
tree = tree->left;
}
min_e = tree->elem;
assert(invrep(tree) && abb_exists(tree, min_e));
return min_e;
}
void abb_dump(abb tree){
assert(invrep(tree));
if (tree != NULL){
printf("%d ", tree->elem);
abb_dump(tree->left);
abb_dump(tree->right);
}
}
abb abb_destroy(abb tree){
assert(invrep(tree));
if(tree!=NULL){
tree->left = abb_destroy(tree->left);
tree->right = abb_destroy(tree->right);
free(tree);
tree = NULL;
}
assert(tree == NULL);
return tree;
}