Skip to content

Commit 0780758

Browse files
author
Raven G. Duran
committed
OK Insert 2 levels, Bug on 3+ levels
1 parent 1ccdae6 commit 0780758

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

btree.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ struct node* newNode(int value,struct node *parent){
127127
root->parent = parent;
128128

129129
int i;
130-
for (i = 1; i < MAX; i++) {
130+
for (i = 1; i <= treeOrder + 2; i++) {
131131
root->value[i] = (int)NULL; // Typcast and wrap NULL into an int for equality purposes
132132
root->keys[i] = NULL;
133133
}
@@ -147,20 +147,24 @@ struct node* insertN(int value,struct node *root,struct node *parent){
147147
printf("Data Already inserted!\n");
148148
break;
149149
} else if ( value > root->value[i] ){ // if value to be inserted is greater, then proceed to next box
150-
//if (root->keys[i+1] != NULL) insertN(value,root->keys[i],root);
151-
//else
152-
i++;
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;
153+
} else i++;
153154

154155
continue;
155156
} else { // This is if the value to be inserted is lesser which means, we can put it now
156-
//if (root->keys[i] != NULL) insertN(value,root->keys[i],root);
157-
157+
if (root->keys[i] != NULL && root->keys[i]->keyCount < treeOrder + 1) {
158+
root->keys[i] = insertN(value,root->keys[i],root);
159+
break;
160+
} else {
158161
for (j = treeOrder - 2; j >= i; j--) root->value[j+1] = root->value[j]; // move everything to the right
159162
root->value[i] = value;
160163
root->keyCount++;
161164

162165
printf("Keys: %d\n",root->keyCount);
163166
break;
167+
}
164168

165169
}
166170
} else { // else if the box is null. Insert directly
@@ -180,14 +184,14 @@ struct node* insertN(int value,struct node *root,struct node *parent){
180184
int mid = (treeOrder-1)/2;
181185

182186
printf("Overflow! Splitting and Promoting...\n");
183-
struct node *leftHalf = (struct node*)malloc(sizeof(struct node));
184-
struct node *rightHalf = (struct node*)malloc(sizeof(struct node));
187+
struct node *leftHalf = NULL;
188+
struct node *rightHalf = NULL;
185189

186190
for (i = 0; i <= left; i++){ // Move all left half data to new LeftHalf Box
187191
leftHalf = insertN(root->value[i],leftHalf,NULL); // Set the parent to NULL temporarily
188192
}
189193

190-
for (i = right; i <= treeOrder; i++){ // Move all right half data to new LeftHalf Box
194+
for (i = right; i < treeOrder; i++){ // Move all right half data to new LeftHalf Box
191195
rightHalf = insertN(root->value[i],rightHalf,NULL); // pointer arithmethic
192196
}
193197

@@ -199,6 +203,13 @@ struct node* insertN(int value,struct node *root,struct node *parent){
199203
rightHalf->parent = root;
200204
root->keys[0] = leftHalf;
201205
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+
}
202213
}
203214

204215
// To do : Non special case split and distribute... or if parent has a parent with values
@@ -227,9 +238,9 @@ void levelOrder(struct node *root){
227238
if (root == NULL) return;
228239
else {
229240
for (i = 0; i < root->keyCount; i++){ // -1 since left and right key of every data box
230-
if((void*)root->value[i] != NULL) printf("%d ",root->value[i]);
241+
if((void*)root->value[i] != NULL || root->value[i] != (int)NULL) printf("%d ",root->value[i]);
231242
}
232-
243+
printf(" ");
233244
if (root->parent == NULL) printf("\n");
234245
else {
235246
for (i = 0; i < root->parent->keyCount; i++){ // Check if had a right sibling, if none then proceed to next level

btree.exe

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)