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