-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.c
207 lines (163 loc) · 4.63 KB
/
btree.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
#include <stdio.h>
#include <errno.h>
struct pkBtreeKey {
long key; // This is the key which create difference
};
struct pkBtreeEntry {
pkBtreeKey entry;
int value ;
};
struct pkBtree {
struct pkBtree *parent;
// struct pkBtree *root;
int n; // No of nodes
struct pkBtreeEntry *keyList; // need to calloc the memory to size of n. Wrapping key with information
bool leaf;
struct pkBtree *children; // n + 1 children
uint8_t depth; // Just to keey an information at which depth the node is.
uint8_t t; // minimum number of children - degree
};
struct pkBtreeLoc {
struct pkBtree *loc;
int index;
};
struct pkBtreeLoc* searchBTree( struct pkBtree *root, struct pkBtreeKey *k ) {
struct pkBtreeLoc *loc = NULL;
int i = 0;
if ( root && pkBtreeKey )
{
while (( i <= root->n ) && (k->key > root->keyList[i].entry.key )) {
i++;
}
if ((i < root->n) && (k->key == root->keyList[i].entry.key )) {
loc->loc = root;
loc->index = i;
return loc;
} else if ( root->leaf ) {
return NULL;
} else {
// Disk read operation in actuals but we are not going to do for this program
return searchBTree (root->child[i], k);
}
}
return loc;
}
static long keyGenerator = 0;
static inline long getGeneratedKey () {
return keyGenerator++;
}
struct pkBtree* allocAndInitNode(struct pkBtree *parent, int degree) {
struct pkBtree *temp = NULL;
temp = (struct pkBtree *) malloc (sizeof(struct pkBtree));
if ( temp ) {
temp->parent = parent;
temp->keyList = (struct pkBtreeEntry *) calloc ( degree-1, sizeof ( pkBtreeEntry ) );
temp->leaf = true;
temp->children = (struct pkBtree *) calloc ( 2 * degree, sizeof( struct pkBtree *) ); // Note we have pointers not the whole node
temp->depth = 0;
temp->t = degree;
}
return temp;
}
void deleteNode (struct pkBtree *node) {
}
uint8_t splitBtreeNode (struct pkBtreeLoc *loc) {
struct pkBtree *toSplit = loc->loc->children [loc->index];
struct pkBtree *temp = NULL;
int j;
int t;
if ( !toSplit )
return -1;
temp = allocAndInitNode (toSplit->parent, toSplit->t );
int j = 0;
temp->leaf = toSplit->leaf;
temp->n = toSplit->t - 1; // Since wei 2*t -1 keys, and we are dividing into nodes with t-1, 1, t-1 keys
// Copying the t+1 till end to new splitted node
memcpy (temp->keyList, toSplit->keyList[toSplit->t + 1], toSplit->t - 1);
bzero (toSplit->keyList[toSplit->t + 1], toSplit->t - 1);
if ( !toSplit->leaf ) {
t = toSplit->t;
for ( j = 1; j <= t ; j++) {
temp->children[j] = toSplit->children[t+j];
toSplit->children[t+j] = NULL;
}
}
// Correcting the number of keys in toSplit
toSplit->n = toSplit->t - 1;
// Update the parent entry to accomodate the median key.
{
// Move the children by 1 to right from loc->index+1
for ( j = loc->loc->n ; j > loc->index; j-- ) {
loc->loc->children[j+1] = loc->loc->children[j];
}
loc->loc->children[index + 1] = temp;
// Shifting the element towards right
memmove( loc->loc->keyList[loc->index+1], loc->loc->keyList[loc->index], loc->loc->n - loc->index + 1);
// copying the data from toSplit to parent
memcpy ( loc->loc->keyList[loc->index], toSplit->keyList[toSplit->t], sizeof (struct pkBtree) );
loc->loc->n++;
}
return 0;
}
uint8_t insertValueNonFull ( struct pkBtree *node, struct pkBtreeKey *k ) {
int n;
if ( !node )
return -1;
n = node->n;
while ( n > 0 && k->entry->key < node->keyList[n]->entry->key )
n--;
n = n + 1;
if ( node->leaf ) {
// memmove the entry
memmove (node->keyList[n+1], node->keyList[n], node->t - n - 1);
memcpy (node->keyList[n], k, sizeof ( struct pkBtreeKey));
node->n++;
return 0;
} else {
if (node->children[n]->n == (2 * node->t - 1) ) {
struct pkBtreeLoc loc = { node, n };
splitBtreeNode (&loc);
if (k->entry->key > node->keyList[n]->entry->key)
n = n + 1;
}
}
return insertValueNonFull ( node->children[n], k );
}
uint8_t insertValue ( struct pkBtree *root, int value) {
struct pkBtreeKey *key = NULL;
if ( !root )
return -1;
key = (struct pkBtreeKey *)malloc (sizeof (struct pkBtreeKey));
key->entry.key = getGeneratedKey();
key->value = value;
if ( root->n == (2 * root->t - 1) ) {
struct pkBtreeLoc loc;
struct pkTree *temp = allocAndInitNode ( root->parent, root->t );
if ( !temp )
return -1;
temp->leaf = false;
temp->n = 0;
temp->children[0] = root;
loc.loc = temp;
loc.index = 0;
splitBtreeNode (&loc);
// Keeping the root address to start
rootBtree = temp;
insertValueNonFull ( temp, &key);
} else {
insertValueNonFull ( root, &key);
}
return 0;
}
/* Just a crude way to start */
static struct pkBtree *rootBtree = NULL;
/*{
NULL,
// &rootBtree,
0,
NULL,
true,
NULL,
0,
2
};*/