Skip to content

Commit a28833f

Browse files
author
Raven G. Duran
committed
Working on the insertion Bug
1 parent 9c7e226 commit a28833f

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

btree.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <stdio.h>
22
#include <math.h>
3-
#define MAX 100
3+
#define MAX 30
44

55
// B-Tree Node Structure
66
struct node {
@@ -146,10 +146,12 @@ struct node* insertN(int value,struct node *root,struct node *parent){
146146
if (value == root->value[i]){ // If the value is already inserted
147147
printf("Data Already inserted!\n");
148148
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
150150
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++;
153155
} else i++;
154156

155157
continue;
@@ -192,24 +194,19 @@ struct node* insertN(int value,struct node *root,struct node *parent){
192194
}
193195

194196
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);
196198
}
197199

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);
200204

201205
if (parent == NULL){ // Special case if splitted is the tRoot (The very Root)
202206
leftHalf->parent = root;
203207
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;
213210
}
214211

215212
// 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){
226223
int i;
227224
if (root == NULL) return;
228225
else {
226+
//root = root->keys[0];
229227
for (i = 0; i < root->keyCount - 1; i++){ // -1 since left and right key of every data box
230228
inOrder(root->keys[i]);
231229
printf("~%d~\n",root->value[i]);
@@ -234,6 +232,7 @@ void inOrder(struct node *root){
234232
}
235233

236234
void levelOrder(struct node *root){
235+
/**
237236
int i;
238237
if (root == NULL) return;
239238
else {
@@ -243,7 +242,7 @@ void levelOrder(struct node *root){
243242
printf(" ");
244243
if (root->parent == NULL) printf("\n");
245244
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
247246
if (root->parent->keys[i] == root){
248247
if (root->parent->keys[i+1] == NULL) printf("\n");
249248
break;
@@ -255,5 +254,6 @@ void levelOrder(struct node *root){
255254
levelOrder(root->keys[i]);
256255
}
257256
}
257+
**/
258258
}
259259

btree.exe

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)