-
Notifications
You must be signed in to change notification settings - Fork 0
/
B_Tree.c
188 lines (145 loc) · 3.23 KB
/
B_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
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
#define N 5 /* ordem da árvore B */
typedef struct Leaf{
int n_keys;
int keys[N-1];
struct Leaf *children[N];
int is_leaf;
}leaf;
leaf* create_leaf(){
leaf *new = (leaf *) malloc (sizeof(leaf));
if(new == NULL) return NULL;
int i;
new->is_leaf = 1;
new->n_keys = 0;
for (i = 0; i < N; i++)
new->children[i] = NULL;
return new;
}
int binarySearch(leaf* leaf, int key){
int b = 0, m, e = leaf->n_keys - 1;
while(b <= e){
m = (b + e)/ 2;
if(leaf->keys[m] == key) return m;
if(leaf->keys[m] >= key) e = m - 1;
if(leaf->keys[m] < key) b = m + 1;
}
return b;
}
leaf* split_page(leaf* leaf, int p){
int i;
struct Leaf *left, *right;
left = leaf->children[p];
right = create_leaf();
right->is_leaf = left->is_leaf;
int nk = round((N-1) / 2);
right->n_keys = nk;
for (i = 0; i < nk; i++){
right->keys[i] = left->keys[i + nk];
}
if (!left->is_leaf)
{
for (i = 0; i < nk; i++){
right->children[i] = left->children[i + nk];
}
}
left->n_keys = (N - 1) / 2;
for(i = leaf->n_keys; i > p; i--){
leaf->children[i + 1] = leaf->children[i];
}
leaf->children[p + 1] = right;
leaf->children[p] = left;
leaf->keys[p] = right->keys[0];
leaf->n_keys++;
for (i = 0; i < nk; i++) {
right->keys[i] = right->keys[i + 1];
}
right->n_keys--;
return leaf;
}
leaf* insertNFP(leaf* leaf, int key){
int i, p = binarySearch(leaf,key);
if(leaf->is_leaf){
for(i = leaf->n_keys; i > p; i--){
leaf->keys[i] = leaf->keys[i - 1];
}
leaf->keys[i] = key;
leaf->n_keys++;
//return leaf;
}
else{
if(leaf->children[p]->n_keys == N - 1){
leaf = split_page(leaf, p);
if(key > leaf->keys[p]) p++;
}
leaf = insertNFP(leaf->children[p], key);
}
return leaf;
}
leaf* insert(leaf* leaf, int key){
struct Leaf *aux;
struct Leaf *new;
aux = leaf;
if(aux->n_keys == N - 1){
new = create_leaf();
new->is_leaf = 0; // falso
new->children[0] = aux;
leaf = split_page(new, 0);
new = insertNFP(new,key);
}
else aux = insertNFP(aux,key);
return leaf;
}
int height(leaf *leaf){
if(leaf->children[0] != NULL) return 1 + height(leaf->children[0]);
return 0;
}
int n_page(leaf *root){
int sum = 0, i; //root
if(root != NULL){
for (i = 0; i <= root->n_keys; i++)
{
if (root->children[i] != NULL)
sum = sum + 1 + n_page(root->children[i]);
}
}
return sum;
}
void printBtree(leaf *root){
int i;
if (root != NULL){
//Imprimir todos os valores do nó
for (i = 0; i < root->n_keys; i++)
{
printf(" %d", root->keys[i]);
}
printf("\n");
//Percorrer todos os nós filhos
for (i = 0; i <= root->n_keys; i++)
{
printBtree(root->children[i]);
}
}
}
int avg(leaf *root){
int sum = 0, i;
if(root != NULL){
for(i = 0; i < root->n_keys; i++){
sum = sum + root->keys[i];
}
}
return sum/root->n_keys;
}
int main(void){
int n, i, key;
scanf(" %d", &n);
leaf *root = create_leaf();
for(i = 0; i < n; i++){
scanf(" %d",&key);
root = insert(root, key);
}
printf("%d\n",1 + n_page(root));
return 0;
}