Skip to content

Commit 83beea4

Browse files
author
Raven G. Duran
committed
Corrected wrong Right half distribution
1 parent de32321 commit 83beea4

File tree

2 files changed

+30
-48
lines changed

2 files changed

+30
-48
lines changed

btree.c

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void doInOrder();
3939
struct node* newNode(int value,struct node *parent);
4040
struct node* insertN(int value,struct node *root,struct node *parent);
4141
struct nodePosition* searchNValue(int value,struct node *root);
42-
struct nodePosition* searchNBox(struct node *toFind,struct node *root);
4342

4443
// Printing
4544
void inOrder(struct node *root);
@@ -68,7 +67,8 @@ void showMenu(){
6867
printf(" _____ _____ \n");
6968
printf(" | __ | ___ |_ _|___ ___ ___ \n");
7069
printf(" | __ -| |___| | | | _| -_| -_| \n");
71-
printf(" |_____| |_| |_| |___|___| \n");
70+
printf(" |_____| |_| |_| |___|___| \n\n");
71+
printf(" A Project in Data Structures by Raven G. Duran BSIT - 2R1\n");
7272

7373
printf("\n\n");
7474
printf("(1) Insert a Node\n");
@@ -227,18 +227,14 @@ struct node* insertN(int value,struct node *root,struct node *parent){
227227
for (i = 0; i <= left; i++){ // Move all left half data to new LeftHalf Box
228228
leftHalf = insertN(root->value[i],leftHalf,NULL); // Set the parent to NULL temporarily
229229
leftHalf->keys[i] = root->keys[i];
230-
231-
if (root->keys[i] != NULL) root->keys[i]->parent = leftHalf;
232230
} leftHalf->keys[left+1] = root->keys[left+1];
233-
if (root->keys[treeOrder] != NULL) root->keys[treeOrder]->parent = leftHalf;
234231

235-
for (i = right; i < treeOrder; i++){ // Move all right half data to new LeftHalf Box
232+
int j = 0;
233+
for (i = right; i < treeOrder; i++, j++){ // Move all right half data to new LeftHalf Box
236234
rightHalf = insertN(root->value[i],rightHalf,NULL);
237-
rightHalf->keys[i] = root->keys[i];
238-
239-
if (root->keys[i] != NULL) root->keys[i]->parent = rightHalf;
240-
} rightHalf->keys[treeOrder] = root->keys[treeOrder];
241-
if (root->keys[treeOrder] != NULL) root->keys[treeOrder]->parent = rightHalf;
235+
rightHalf->keys[j] = root->keys[i];
236+
} rightHalf->keys[j] = root->keys[treeOrder];
237+
242238

243239
int pIsNull = parent == NULL ? 1 : 0;
244240
struct node *tempRoot = root;
@@ -247,37 +243,42 @@ struct node* insertN(int value,struct node *root,struct node *parent){
247243
pushS(leftHalf,rightHalf);
248244
parent = insertN(root->value[mid],parent,pParent);
249245

246+
struct splitStack *ss = popS();
247+
250248
if (pIsNull){ // Special case if splitted is the tRoot (The very Root)
251-
leftHalf->parent = parent;
252-
rightHalf->parent = parent;
253-
parent->keys[0] = leftHalf;
254-
parent->keys[1] = rightHalf;
249+
ss->leftHalf->parent = parent;
250+
ss->rightHalf->parent = parent;
251+
parent->keys[0] = ss->leftHalf;
252+
parent->keys[1] = ss->rightHalf;
255253

256254
tRoot = parent;
257255
//free(tempRoot); // Delete old root node box
258-
return parent;
259-
} else {
260256

261-
struct splitStack *ss = popS();
257+
printf("\n\n");
258+
inOrder(parent);
259+
printf("\n\n");
260+
261+
return parent;
262262

263263

264+
} else {
264265
// put left half properly
265266
if (ss != NULL){
266267
// Find the parent of the current left and right box
267-
int max = findMax(leftHalf);
268+
int max = findMax(ss->leftHalf);
268269
for (i = 0; i < parent->keyCount; i++){
269270
if (max < parent->value[i]){
270-
parent->keys[i] = leftHalf;
271-
parent->keys[i+1] = rightHalf;
271+
parent->keys[i] = ss->leftHalf;
272+
parent->keys[i+1] = ss->rightHalf;
272273

273-
printf("Left half is: %d\n",leftHalf->value[0]);
274+
printf("Left half is: %d\n",ss->leftHalf->value[0]);
275+
printf("Right half is: %d\n",ss->rightHalf->value[0]);
274276

275-
leftHalf->parent = parent;
276-
rightHalf->parent = parent;
277+
ss->leftHalf->parent = parent;
278+
ss->rightHalf->parent = parent;
279+
break;
277280
}
278281
}
279-
280-
// put right half properly
281282
}
282283

283284
/**
@@ -292,7 +293,7 @@ struct node* insertN(int value,struct node *root,struct node *parent){
292293
}
293294
**/
294295

295-
return leftHalf;
296+
return ss->leftHalf;
296297
}
297298

298299
}
@@ -320,31 +321,12 @@ struct nodePosition* searchNValue(int value,struct node *root){
320321
}
321322
}
322323

323-
struct nodePosition* searchNBox(struct node *toFind,struct node *root){
324-
int i;
325-
struct nodePosition *nodeF = NULL;
326-
if (root == NULL) return NULL;
327-
else {
328-
for (i = 0; i < root->keyCount; i++){ // -1 since left and right key of every data box
329-
if (root == toFind) {
330-
nodeF = (struct nodePosition*)malloc(sizeof(struct nodePosition));
331-
nodeF->box = root;
332-
nodeF->key = i;
333-
return nodeF;
334-
}
335-
else if (nodeF != NULL) return nodeF;
336-
else nodeF = searchNBox(toFind,root->keys[i]);
337-
}
338-
return nodeF;
339-
}
340-
}
341-
342324
// Printing
343325
void inOrder(struct node *root){
344326
int i;
345327
if (root == NULL) return;
346328
else {
347-
//root = root->keys[1]->keys[3];
329+
//root = root->keys[1]->keys[2];
348330
for (i = 0; i < root->keyCount + 2; i++){ // -1 since left and right key of every data box
349331
inOrder(root->keys[i]);
350332
if (i < root->keyCount - 1) printf("~%d~\n",root->value[i]);
@@ -357,7 +339,7 @@ void levelOrder(struct node *root){
357339
if (root == NULL) return;
358340
else {
359341
for (i = 0; i < root->keyCount - 1; i++){ // -1 since left and right key of every data box
360-
printf("%d ",i,root->value[i]);
342+
printf("%d ",root->value[i]);
361343
}
362344
printf(" ");
363345
if (root->parent == NULL) printf("\n");

btree.exe

-1.03 KB
Binary file not shown.

0 commit comments

Comments
 (0)