Skip to content

Commit de32321

Browse files
author
Raven G. Duran
committed
Starting newAlgo
1 parent 9919d4e commit de32321

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

btree.c

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ struct nodePosition* searchNBox(struct node *toFind,struct node *root);
4545
void inOrder(struct node *root);
4646
void levelOrder(struct node *root);
4747

48+
// Utils
49+
int findMax(struct node *root);
50+
int findMin(struct node *root);
51+
4852
int main(){
4953
// Initialize
5054
printf("--- Initialization ---\n");
@@ -90,7 +94,9 @@ void doInsert(){
9094
printf("Value: ");
9195
scanf("%d",&value);
9296

93-
tRoot = insertN(value,tRoot,NULL);
97+
98+
if (tRoot == NULL) tRoot = insertN(value,tRoot,NULL);
99+
else insertN(value,tRoot,NULL);
94100

95101
printf("\n\n\nPress any key to continue...\n");
96102
getch();
@@ -224,20 +230,21 @@ struct node* insertN(int value,struct node *root,struct node *parent){
224230

225231
if (root->keys[i] != NULL) root->keys[i]->parent = leftHalf;
226232
} leftHalf->keys[left+1] = root->keys[left+1];
227-
if (root->keys[treeOrder] != NULL) root->keys[treeOrder]->parent = leftHalf;
233+
if (root->keys[treeOrder] != NULL) root->keys[treeOrder]->parent = leftHalf;
228234

229235
for (i = right; i < treeOrder; i++){ // Move all right half data to new LeftHalf Box
230236
rightHalf = insertN(root->value[i],rightHalf,NULL);
231237
rightHalf->keys[i] = root->keys[i];
232238

233239
if (root->keys[i] != NULL) root->keys[i]->parent = rightHalf;
234240
} rightHalf->keys[treeOrder] = root->keys[treeOrder];
235-
if (root->keys[treeOrder] != NULL) root->keys[treeOrder]->parent = rightHalf;
241+
if (root->keys[treeOrder] != NULL) root->keys[treeOrder]->parent = rightHalf;
236242

237243
int pIsNull = parent == NULL ? 1 : 0;
238244
struct node *tempRoot = root;
239245
struct node *pParent = pIsNull ? NULL : parent->parent;
240246

247+
pushS(leftHalf,rightHalf);
241248
parent = insertN(root->value[mid],parent,pParent);
242249

243250
if (pIsNull){ // Special case if splitted is the tRoot (The very Root)
@@ -246,35 +253,48 @@ struct node* insertN(int value,struct node *root,struct node *parent){
246253
parent->keys[0] = leftHalf;
247254
parent->keys[1] = rightHalf;
248255

256+
tRoot = parent;
249257
//free(tempRoot); // Delete old root node box
250258
return parent;
251259
} else {
252-
// Find the parent of the current left and right box
253-
int found = 0;
260+
261+
struct splitStack *ss = popS();
262+
263+
264+
// put left half properly
265+
if (ss != NULL){
266+
// Find the parent of the current left and right box
267+
int max = findMax(leftHalf);
268+
for (i = 0; i < parent->keyCount; i++){
269+
if (max < parent->value[i]){
270+
parent->keys[i] = leftHalf;
271+
parent->keys[i+1] = rightHalf;
272+
273+
printf("Left half is: %d\n",leftHalf->value[0]);
274+
275+
leftHalf->parent = parent;
276+
rightHalf->parent = parent;
277+
}
278+
}
279+
280+
// put right half properly
281+
}
282+
283+
/**
254284
for (i = 0; i < parent->keyCount; i++){
255285
if (parent->keys[i] != NULL && parent->keys[i] == tempRoot){
256286
parent->keys[i] = leftHalf;
257287
parent->keys[i+1] = rightHalf;
258288
259289
leftHalf->parent = parent;
260290
rightHalf->parent = parent;
261-
found = 1;
262291
}
263292
}
293+
**/
264294

265-
266-
struct nodePosition *nodeF = searchNBox(tempRoot,tRoot);
267-
if (nodeF != NULL && !found) {
268-
printf("Beep: i is %d\n",nodeF->key);
269-
nodeF->box->keys[nodeF->key] = leftHalf;
270-
nodeF->box->keys[nodeF->key+1] = rightHalf;
271-
}
272-
273-
//free(tempRoot);
274295
return leftHalf;
275296
}
276297

277-
// To do : Non special case split and distribute... or if parent has a parent with values
278298
}
279299
}
280300

@@ -337,7 +357,7 @@ void levelOrder(struct node *root){
337357
if (root == NULL) return;
338358
else {
339359
for (i = 0; i < root->keyCount - 1; i++){ // -1 since left and right key of every data box
340-
printf("Node %d : %d ",i,root->value[i]);
360+
printf("%d ",i,root->value[i]);
341361
}
342362
printf(" ");
343363
if (root->parent == NULL) printf("\n");
@@ -372,9 +392,28 @@ void pushS(struct node *leftHalf, struct node *rightHalf){
372392
struct splitStack *popS(){
373393
if (head == NULL) {
374394
printf("Stack Underflow!!\n");
375-
getch();
376-
exit(-1);
395+
return NULL;
377396
} else {
378-
397+
struct splitStack *temp = head;
398+
head = head->next;
399+
return temp;
379400
}
380401
}
402+
403+
int findMax(struct node *root){
404+
int i, max = root->value[0];
405+
for (i = 0; i < root->keyCount; i++){
406+
if (root->value[i] > max) max = root->value[i];
407+
}
408+
409+
return max;
410+
}
411+
412+
int findMin(struct node *root){
413+
int i, min = root->value[0];
414+
for (i = 0; i < root->keyCount; i++){
415+
if (root->value[i] < min) min = root->value[i];
416+
}
417+
418+
return min;
419+
}

btree.exe

1.04 KB
Binary file not shown.

0 commit comments

Comments
 (0)