@@ -127,7 +127,7 @@ struct node* newNode(int value,struct node *parent){
127
127
root -> parent = parent ;
128
128
129
129
int i ;
130
- for (i = 1 ; i < MAX ; i ++ ) {
130
+ for (i = 1 ; i <= treeOrder + 2 ; i ++ ) {
131
131
root -> value [i ] = (int )NULL ; // Typcast and wrap NULL into an int for equality purposes
132
132
root -> keys [i ] = NULL ;
133
133
}
@@ -147,20 +147,24 @@ struct node* insertN(int value,struct node *root,struct node *parent){
147
147
printf ("Data Already inserted!\n" );
148
148
break ;
149
149
} 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 ++ ;
153
154
154
155
continue ;
155
156
} 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 {
158
161
for (j = treeOrder - 2 ; j >= i ; j -- ) root -> value [j + 1 ] = root -> value [j ]; // move everything to the right
159
162
root -> value [i ] = value ;
160
163
root -> keyCount ++ ;
161
164
162
165
printf ("Keys: %d\n" ,root -> keyCount );
163
166
break ;
167
+ }
164
168
165
169
}
166
170
} else { // else if the box is null. Insert directly
@@ -180,14 +184,14 @@ struct node* insertN(int value,struct node *root,struct node *parent){
180
184
int mid = (treeOrder - 1 )/2 ;
181
185
182
186
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 ;
185
189
186
190
for (i = 0 ; i <= left ; i ++ ){ // Move all left half data to new LeftHalf Box
187
191
leftHalf = insertN (root -> value [i ],leftHalf ,NULL ); // Set the parent to NULL temporarily
188
192
}
189
193
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
191
195
rightHalf = insertN (root -> value [i ],rightHalf ,NULL ); // pointer arithmethic
192
196
}
193
197
@@ -199,6 +203,13 @@ struct node* insertN(int value,struct node *root,struct node *parent){
199
203
rightHalf -> parent = root ;
200
204
root -> keys [0 ] = leftHalf ;
201
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
+ }
202
213
}
203
214
204
215
// 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){
227
238
if (root == NULL ) return ;
228
239
else {
229
240
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 ]);
231
242
}
232
-
243
+ printf ( " " );
233
244
if (root -> parent == NULL ) printf ("\n" );
234
245
else {
235
246
for (i = 0 ; i < root -> parent -> keyCount ; i ++ ){ // Check if had a right sibling, if none then proceed to next level
0 commit comments