Skip to content

Commit 1f90180

Browse files
author
Raven G. Duran
committed
Minor update to printing
1 parent a28833f commit 1f90180

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

btree.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <stdio.h>
22
#include <math.h>
3-
#define MAX 30
3+
#define MAX 100
44

55
// B-Tree Node Structure
66
struct node {
@@ -146,12 +146,10 @@ struct node* insertN(int value,struct node *root,struct node *parent){
146146
if (value == root->value[i]){ // If the value is already inserted
147147
printf("Data Already inserted!\n");
148148
break;
149-
} else if ( value > root->value[i]){ // if value to be inserted is greater, then proceed to next box
149+
} else if ( value > root->value[i] ){ // if value to be inserted is greater, then proceed to next box
150150
if (root->keys[i+1] != NULL && root->keys[i+1]->keyCount < treeOrder + 1) {
151-
if ( ( (void*)root->value[i+1] != NULL && value < root->value[i+1] ) || root == tRoot){
152-
root->keys[i+1] = insertN(value,root->keys[i+1],root);
153-
break;
154-
} else i++;
151+
root->keys[i+1] = insertN(value,root->keys[i+1],root);
152+
break;
155153
} else i++;
156154

157155
continue;
@@ -194,19 +192,24 @@ struct node* insertN(int value,struct node *root,struct node *parent){
194192
}
195193

196194
for (i = right; i < treeOrder; i++){ // Move all right half data to new LeftHalf Box
197-
rightHalf = insertN(root->value[i],rightHalf,NULL);
195+
rightHalf = insertN(root->value[i],rightHalf,NULL); // pointer arithmethic
198196
}
199197

200-
struct node *tempRoot = root; // Temporarily hold old root box
201-
struct node *parentP = root->parent == NULL ? NULL : root->parent->parent; // Parent of the parents
202-
203-
root->parent = insertN(root->value[mid],root->parent,parentP);
198+
struct node *tempRoot = root;
199+
root = insertN(root->value[mid],parent,parent);
204200

205201
if (parent == NULL){ // Special case if splitted is the tRoot (The very Root)
206202
leftHalf->parent = root;
207203
rightHalf->parent = root;
208-
parent->keys[0] = leftHalf;
209-
parent->keys[1] = rightHalf;
204+
root->keys[0] = leftHalf;
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+
}
210213
}
211214

212215
// To do : Non special case split and distribute... or if parent has a parent with values
@@ -223,16 +226,15 @@ void inOrder(struct node *root){
223226
int i;
224227
if (root == NULL) return;
225228
else {
226-
//root = root->keys[0];
227-
for (i = 0; i < root->keyCount - 1; i++){ // -1 since left and right key of every data box
229+
//root = root->keys[1];
230+
for (i = 0; i < root->keyCount; i++){ // -1 since left and right key of every data box
228231
inOrder(root->keys[i]);
229-
printf("~%d~\n",root->value[i]);
232+
if (i < root->keyCount - 1) printf("~%d~\n",root->value[i]);
230233
}
231234
}
232235
}
233236

234237
void levelOrder(struct node *root){
235-
/**
236238
int i;
237239
if (root == NULL) return;
238240
else {
@@ -242,7 +244,7 @@ void levelOrder(struct node *root){
242244
printf(" ");
243245
if (root->parent == NULL) printf("\n");
244246
else {
245-
for (i = 0; i < root->parent->keyCount - 1; i++){ // Check if had a right sibling, if none then proceed to next level
247+
for (i = 0; i < root->parent->keyCount; i++){ // Check if had a right sibling, if none then proceed to next level
246248
if (root->parent->keys[i] == root){
247249
if (root->parent->keys[i+1] == NULL) printf("\n");
248250
break;
@@ -254,6 +256,4 @@ void levelOrder(struct node *root){
254256
levelOrder(root->keys[i]);
255257
}
256258
}
257-
**/
258259
}
259-

btree.exe

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)