Skip to content

Commit bc44b58

Browse files
author
Raven G. Duran
committed
Partially corrected insertion recurception problem
1 parent 1f90180 commit bc44b58

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

btree.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,35 @@ struct node* insertN(int value,struct node *root,struct node *parent){
192192
}
193193

194194
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
195+
rightHalf = insertN(root->value[i],rightHalf,NULL);
196196
}
197197

198+
int pIsNull = parent == NULL ? 1 : 0;
198199
struct node *tempRoot = root;
199-
root = insertN(root->value[mid],parent,parent);
200+
struct node *pParent = pIsNull ? NULL : parent->parent;
200201

201-
if (parent == NULL){ // Special case if splitted is the tRoot (The very Root)
202-
leftHalf->parent = root;
203-
rightHalf->parent = root;
204-
root->keys[0] = leftHalf;
205-
root->keys[1] = rightHalf;
202+
parent = insertN(root->value[mid],parent,pParent);
203+
204+
if (pIsNull){ // Special case if splitted is the tRoot (The very Root)
205+
leftHalf->parent = parent;
206+
rightHalf->parent = parent;
207+
parent->keys[0] = leftHalf;
208+
parent->keys[1] = rightHalf;
209+
210+
free(tempRoot); // Delete old root node box
211+
return parent;
206212
} 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;
213+
for (i = 0; i < parent->keyCount; i++){
214+
if (parent->keys[i] == tempRoot){
215+
parent->keys[i] = leftHalf;
216+
parent->keys[i+1] = rightHalf;
211217
}
212218
}
219+
free(tempRoot);
220+
return leftHalf;
213221
}
214222

215223
// To do : Non special case split and distribute... or if parent has a parent with values
216-
217-
free(tempRoot); // Delete old root node box
218224
}
219225
}
220226

@@ -226,7 +232,7 @@ void inOrder(struct node *root){
226232
int i;
227233
if (root == NULL) return;
228234
else {
229-
//root = root->keys[1];
235+
// root = root->keys[2];
230236
for (i = 0; i < root->keyCount; i++){ // -1 since left and right key of every data box
231237
inOrder(root->keys[i]);
232238
if (i < root->keyCount - 1) printf("~%d~\n",root->value[i]);

btree.exe

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)