1
1
#include <stdio.h>
2
2
#include <math.h>
3
- #define MAX 30
3
+ #define MAX 100
4
4
5
5
// B-Tree Node Structure
6
6
struct node {
@@ -146,12 +146,10 @@ struct node* insertN(int value,struct node *root,struct node *parent){
146
146
if (value == root -> value [i ]){ // If the value is already inserted
147
147
printf ("Data Already inserted!\n" );
148
148
break ;
149
- } else if ( value > root -> value [i ]){ // if value to be inserted is greater, then proceed to next box
149
+ } else if ( value > root -> value [i ] ){ // if value to be inserted is greater, then proceed to next box
150
150
if (root -> keys [i + 1 ] != NULL && root -> keys [i + 1 ]-> keyCount < treeOrder + 1 ) {
151
- if ( ( (void * )root -> value [i + 1 ] != NULL && value < root -> value [i + 1 ] ) || root == tRoot ){
152
- root -> keys [i + 1 ] = insertN (value ,root -> keys [i + 1 ],root );
153
- break ;
154
- } else i ++ ;
151
+ root -> keys [i + 1 ] = insertN (value ,root -> keys [i + 1 ],root );
152
+ break ;
155
153
} else i ++ ;
156
154
157
155
continue ;
@@ -194,19 +192,24 @@ struct node* insertN(int value,struct node *root,struct node *parent){
194
192
}
195
193
196
194
for (i = right ; i < treeOrder ; i ++ ){ // Move all right half data to new LeftHalf Box
197
- rightHalf = insertN (root -> value [i ],rightHalf ,NULL );
195
+ rightHalf = insertN (root -> value [i ],rightHalf ,NULL ); // pointer arithmethic
198
196
}
199
197
200
- struct node * tempRoot = root ; // Temporarily hold old root box
201
- struct node * parentP = root -> parent == NULL ? NULL : root -> parent -> parent ; // Parent of the parents
202
-
203
- root -> parent = insertN (root -> value [mid ],root -> parent ,parentP );
198
+ struct node * tempRoot = root ;
199
+ root = insertN (root -> value [mid ],parent ,parent );
204
200
205
201
if (parent == NULL ){ // Special case if splitted is the tRoot (The very Root)
206
202
leftHalf -> parent = root ;
207
203
rightHalf -> parent = root ;
208
- parent -> keys [0 ] = leftHalf ;
209
- parent -> keys [1 ] = rightHalf ;
204
+ root -> keys [0 ] = leftHalf ;
205
+ root -> keys [1 ] = rightHalf ;
206
+ } else {
207
+ for (i = 0 ; i < tempRoot -> parent -> keyCount ; i ++ ){
208
+ if (tempRoot -> parent -> keys [i ] == tempRoot ){
209
+ tempRoot -> parent -> keys [i ] = leftHalf ;
210
+ tempRoot -> parent -> keys [i + 1 ] = rightHalf ;
211
+ }
212
+ }
210
213
}
211
214
212
215
// To do : Non special case split and distribute... or if parent has a parent with values
@@ -223,16 +226,15 @@ void inOrder(struct node *root){
223
226
int i ;
224
227
if (root == NULL ) return ;
225
228
else {
226
- //root = root->keys[0 ];
227
- for (i = 0 ; i < root -> keyCount - 1 ; i ++ ){ // -1 since left and right key of every data box
229
+ //root = root->keys[1 ];
230
+ for (i = 0 ; i < root -> keyCount ; i ++ ){ // -1 since left and right key of every data box
228
231
inOrder (root -> keys [i ]);
229
- printf ("~%d~\n" ,root -> value [i ]);
232
+ if ( i < root -> keyCount - 1 ) printf ("~%d~\n" ,root -> value [i ]);
230
233
}
231
234
}
232
235
}
233
236
234
237
void levelOrder (struct node * root ){
235
- /**
236
238
int i ;
237
239
if (root == NULL ) return ;
238
240
else {
@@ -242,7 +244,7 @@ void levelOrder(struct node *root){
242
244
printf (" " );
243
245
if (root -> parent == NULL ) printf ("\n" );
244
246
else {
245
- for (i = 0; i < root->parent->keyCount - 1 ; i++){ // Check if had a right sibling, if none then proceed to next level
247
+ for (i = 0 ; i < root -> parent -> keyCount ; i ++ ){ // Check if had a right sibling, if none then proceed to next level
246
248
if (root -> parent -> keys [i ] == root ){
247
249
if (root -> parent -> keys [i + 1 ] == NULL ) printf ("\n" );
248
250
break ;
@@ -254,6 +256,4 @@ void levelOrder(struct node *root){
254
256
levelOrder (root -> keys [i ]);
255
257
}
256
258
}
257
- **/
258
259
}
259
-
0 commit comments