Skip to content

Commit d36c118

Browse files
author
Raven G. Duran
committed
Updated Insertion Again
1 parent bc44b58 commit d36c118

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

btree.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ struct node* insertN(int value,struct node *root,struct node *parent){
158158
root->keys[i] = insertN(value,root->keys[i],root);
159159
break;
160160
} else {
161-
for (j = treeOrder - 2; j >= i; j--) root->value[j+1] = root->value[j]; // move everything to the right
161+
for (j = treeOrder - 2; j >= i; j--) {
162+
root->value[j+1] = root->value[j]; // move everything to the right
163+
root->keys[j+2] = root->keys[j+1];
164+
}
162165
root->value[i] = value;
163166
root->keyCount++;
164167

@@ -167,7 +170,7 @@ struct node* insertN(int value,struct node *root,struct node *parent){
167170
}
168171

169172
}
170-
} else { // else if the box is null. Insert directly
173+
} else { // else if the box is null or the last box. Insert directly
171174
root->value[i] = value;
172175
root->keyCount++;
173176

@@ -188,11 +191,13 @@ struct node* insertN(int value,struct node *root,struct node *parent){
188191
struct node *rightHalf = NULL;
189192

190193
for (i = 0; i <= left; i++){ // Move all left half data to new LeftHalf Box
191-
leftHalf = insertN(root->value[i],leftHalf,NULL); // Set the parent to NULL temporarily
194+
leftHalf = insertN(root->value[i],leftHalf,parent); // Set the parent to NULL temporarily
195+
leftHalf->keys[i] = root->keys[i];
192196
}
193197

194198
for (i = right; i < treeOrder; i++){ // Move all right half data to new LeftHalf Box
195-
rightHalf = insertN(root->value[i],rightHalf,NULL);
199+
rightHalf = insertN(root->value[i],rightHalf,parent);
200+
rightHalf->keys[i] = root->keys[i];
196201
}
197202

198203
int pIsNull = parent == NULL ? 1 : 0;

btree.exe

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)