-
Notifications
You must be signed in to change notification settings - Fork 0
/
B_tree.c
443 lines (392 loc) · 9.91 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
NAME - RAJENDRA SINGH
ROLL NO.- 111601017
PROGRAM: B-Tree(Insert and search)
DATE : 19 -03-18
*/
//----------------------------------------header-------------------------------------------------------//
#include<stdio.h>
#include<stdlib.h>///for malloc
#include<string.h>
#include <limits.h>//for INT_MAX
//----------------------------------------GLOBAL VARIABLE-------------------------------------------------------//
int ofset = 0;//for printing
//------------------------------------all structures and there creating function-------------------------//
typedef struct BTnode// a structure to represent a b-tnode
{
int n;
int t; //degree parameter
int leaf;//laef = 1 if node is leaf, else 0.
int *key; //ptr to headptr of key array in b-tree node
struct BTnode **c; //ptr to headptr of child array of b-tree node
}BTnode;
BTnode* newBTnode(int t)
{
BTnode *x = (BTnode *)malloc(sizeof(BTnode));
x->t = t;
x->n = 0;
x->leaf = 0;
x->key = (int *)malloc((2*t-1)*sizeof(int));
for (int i = 0; i <(2*t-1); i++)
{
//*(x->key[i]) = (int *)malloc(sizeof(int));
x->key[i] = -INT_MAX;
}
x->c = (struct BTnode **)malloc((2*t)*sizeof(struct BTnode *));
for (int i = 0; i <(2*t); i++)
{
x->c[i] = NULL;
}
return(x);
}
typedef struct BT// a structure to represent a BT
{
BTnode *root; //root
int t;
int height;
}BT;
BT* newBT(int t)
{
BT *T = (BT *)malloc(sizeof(BT));
T->root = NULL;
T->t = t;
T->height = 0;
return(T);
}
typedef struct stack//structure of a stack
{
int data;
struct stack *next;
}stack;
stack *top=NULL;
void push(int data)//////////////////
{
if(top==NULL)
{
top=(stack *) malloc(sizeof(stack));
top->data = data;
top->next = NULL;
}
else
{
stack *temp=(stack *) malloc(sizeof(stack));
temp->data = data;
temp->next=top;
top=temp;
}
return;
}
int pop()////////////////////
{
int y;
struct stack* temp;
if(top==NULL)
return(-INT_MAX);
else
{
temp=(struct stack*) malloc(sizeof(struct stack));
temp=top;
top=top->next;
y=temp->data;
//printf("%d",y);
free(temp);
}
return y;
}
//--------------------------------------------------------FUNCTION DECLARATION---------------------------------------//
BT *readInsertSearch();
void InsertKey(BT*, int);
BTnode* split(BT *, BTnode *, BTnode *, int);
BTnode* SearchKey(BT*, int);
void PrintTree(BTnode*, int);
//------------------------------------------------------------main function--------------------------------------------//
int main()
{
int choice,key;
BT *T = readInsertSearch();
}
//----------------------------------------------------readInsertSearch function--------------------------------------------//
BT* readInsertSearch()
{
int n, key,i,t;
// printf("Enter degree parameter of BT : ");
// scanf("%d", &t);
t = 3;
BT *T= newBT(t);
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &key);
InsertKey(T, key);
printf("PRINTING B-TREE :\nHeight = %d\n\n", T->height);
PrintTree(T->root, 0);
}
while(key != -1)
{
scanf("%d", &key);
if(key != -1)
{
BTnode *s = SearchKey(T, key);
}
}
return(T);
}
//---------------------------------------------------------------INSERT FUNCTION------------------------------------------//
void InsertKey(BT *T, int key)
{
printf("\n\n\n\n\ninsert of %d\n", key);
int t = T->t;
int i;
BTnode *curr = T->root;
BTnode *parent = NULL;
if(curr == NULL)// if there no BTnode in T i.e BT is empty
{
curr = newBTnode(t);
T->root = curr;
curr->key[0] = key;
//p->n = 1;
curr->leaf = 1;
T->height = 1;
}
else//i.e curr != NULL
{
while(curr->leaf != 1)//travel down to leaf node as new node is always added leaf node
{
if(curr->n == 2*t-1) //checking if node is full,split
{
curr = split(T, curr, parent,key);//split curr
}
for (i = 0; i < curr->n; i++)//checking through which children to travel down the tree
{
if(curr->key[i] > key)
{
break;
}
}
parent = curr;
curr = curr->c[i];
}
//now we had reach leaf node
if(curr->n == 2*t-1)//checking if node is full,if then split
{
curr = split(T, curr, parent,key);//split curr
}
for (i = 0; i < curr->n; i++)//finding the index, where new key should go
{
if(curr->key[i] > key)
{
break;
}
}
int index = i;
for (i = curr->n-1; i > index-1; i--)//shifting all keys(index <----> n-1) after index(including) rightward
{
curr->key[i+1] = curr->key[i];
}
curr->key[index] = key;
}
curr->n++;
}
//----------------------------------------------------------------SPLIT FUNCTION------------------------------------------//
BTnode* split(BT *T, BTnode *curr, BTnode *parent, int key)
{
int t = T->t;
int i;
if(parent == NULL)//this means curr is root(i.e we are spliting the root node, hence have to add a new node)
{
parent = newBTnode(t);//parent equal to new node
T->root = parent;//updating root address
T->height++;
}
int midkey = curr->key[t-1];
for (i = 0; i < parent->n; i++)//finding index of parent where midkey should put
{
if(midkey < parent->key[i])
{
break;
}
}
int index = i;
for (i = parent->n-1; i >= index; i--)//shifting all keys(index <------> n-1) after index(including) rightward
{
parent->key[i+1] = parent->key[i];
}
for (i = parent->n; i > index; i--)//shifting all children(index+1 <----------> n) after index(excluding) rightward
{
parent->c[i+1] = parent->c[i];
}
parent->key[index] = midkey;
parent->n++;
//updating left(parent->c[index]) child of parent
parent->c[index] = curr;
BTnode *l = parent->c[index]; //parent->c[index]; //left child //curr(= parent->c[index],was already unchanged)
l->n = t-1;//no of keys reduce from 2t-1 to t-1
//updating right(parent->c[index+1]) child of parent
parent->c[index+1] = newBTnode(t);//newly created node address
BTnode *r = parent->c[index+1];
r->n = t-1;
r->leaf = curr->leaf;
for (i = 0; i < t-1; i++)//updating first t-1 keys of 2nd child with last t-1 keys of curr
{
r->key[i] = curr->key[t+i];//t+i key
}
for (i = 0; i < t; i++)//updating first t children of 2nd child with last t children of curr
{
if(r->leaf == 0)
{
r->c[i] = curr->c[t+i];//t+i key
}
}
if (key < midkey)
{
return(l); //return l as curr node(since key will be left child)
}
else
return(r);//return r as curr node(since key will be right child)
}
//---------------------------------------------------------------SEARCH FUNCTION------------------------------------------//
BTnode* SearchKey(BT *T, int key)
{
printf("\n\n\nsearch of key = %d\n", key);
int i;
BTnode *curr = T->root;
BTnode *parent = NULL;
if(curr == NULL)// if there no BTnode in T i.e BT is empty
{
printf("\nBT is empty\n");
return(curr);
}
else
{
while(curr != NULL)
{
for(i=0; i < curr->n; i++)
{
if(curr->key[i] == key)//key is Present at curr
{
push(i+1);
int popchild = pop();
printf("Key = %d is Present!!!\n\n", key);
while(popchild != -INT_MAX)
{
printf("child %d <-", popchild);
popchild = pop();
}
printf("<-root");
return(curr);
//since key is present, so printing path--------------------//
// printf("Key = %d is Present!!!\n", key);
// printf("Path = root");
// curr = T->root;
// int j;
// while(curr != NULL)
// {
// for(j=0; j < curr->n; j++)
// {
// if(curr->key[j] == key)//key is Present at curr
// {
// printf("->child %d!!! ", j+1);
// return(curr);
// }
// else if(curr->key[j] > key)//key might Present at curr->c[i]
// {
// //printf("->child %d", j);
// //curr = curr->c[j];
// break;
// }
// }
// printf("->child %d", j+1);
// if(curr != NULL)
// curr = curr->c[j];
// }
//---------------------------------------------------------------//
}
else if(curr->key[i] > key)//key might Present at curr->c[i]
{
//curr = curr->c[i];
break;
}
}
push(i+1);
if(curr != NULL)
curr = curr->c[i];
}
printf("Key = %d, is absent\n", key);
top = NULL;
return(curr);
}
}
//----------------------------------------------------PRINT BT IN TREE FUNCTION------------------------------------------//
void PrintTree(BTnode *currentNode, int ofset)
{
int i;
if(currentNode == NULL)
{
printf("\nBT is empty\n");
return;
}
else if(currentNode->leaf == 1)
{
for (i = currentNode->n-1; i > -1; i--)
{
for (int j = 0; j < ofset; j++)
{
printf("__");
}
printf("%d\n", currentNode->key[i]);
}
return;
}
else
{
//printf("flag,((%d))\n", currentNode->key);
//printf("\nPRINTING BT : \n\n\n");
PrintTree(currentNode->c[currentNode->n],ofset+2);
for (i = currentNode->n-1; i >= 0; i--)
{
for (int j = 0; j < ofset; j++)
{
printf("__");
}
printf("%d\n", currentNode->key[i]);
PrintTree(currentNode->c[i],ofset+2);
}
return;
}
}
/*
SAMPLE INPUT :
27
25
50
75
10
15
35
40
45
55
65
85
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
40
55
83
43
-1
*/