@@ -158,7 +158,10 @@ struct node* insertN(int value,struct node *root,struct node *parent){
158
158
root -> keys [i ] = insertN (value ,root -> keys [i ],root );
159
159
break ;
160
160
} 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
+ }
162
165
root -> value [i ] = value ;
163
166
root -> keyCount ++ ;
164
167
@@ -167,7 +170,7 @@ struct node* insertN(int value,struct node *root,struct node *parent){
167
170
}
168
171
169
172
}
170
- } else { // else if the box is null. Insert directly
173
+ } else { // else if the box is null or the last box . Insert directly
171
174
root -> value [i ] = value ;
172
175
root -> keyCount ++ ;
173
176
@@ -188,11 +191,13 @@ struct node* insertN(int value,struct node *root,struct node *parent){
188
191
struct node * rightHalf = NULL ;
189
192
190
193
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 ];
192
196
}
193
197
194
198
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 ];
196
201
}
197
202
198
203
int pIsNull = parent == NULL ? 1 : 0 ;
0 commit comments