Skip to content

Commit 7ff18b7

Browse files
author
Raven G. Duran
committed
Added B-Tree In Order Print (Beta) and started Overflow Insertion
1 parent cc9ce6e commit 7ff18b7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

btree.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ void doInOrder();
2323
struct node* newNode(int value,struct node *parent);
2424
struct node* insertN(int value,struct node *root,struct node *parent);
2525

26+
// Printing
27+
void inOrder(struct node *root);
2628

2729
int main(){
2830
// Initialize
@@ -86,6 +88,8 @@ void doDelete(){
8688
}
8789

8890
void doSearch(){
91+
int toFind;
92+
8993
system("cls");
9094
printf("--- Search ---\n");
9195

@@ -100,7 +104,9 @@ void doInOrder(){
100104
system("cls");
101105
printf("--- Printing ---\n");
102106

103-
for (i = 0; i < tRoot->keyCount - 1; i++) printf("%d ", tRoot->value[i]);
107+
//for (i = 0; i < tRoot->keyCount - 1; i++) printf("%d ", tRoot->value[i]);
108+
if (tRoot != NULL) inOrder(tRoot);
109+
else printf("\nThe tree is empty!\n");
104110

105111
printf("\n\n\nPress any key to continue...\n");
106112
getch();
@@ -115,7 +121,10 @@ struct node* newNode(int value,struct node *parent){
115121
root->parent = parent;
116122

117123
int i;
118-
for (i = 1; i < MAX; i++) root->value[i] = (int)NULL;
124+
for (i = 1; i < MAX; i++) {
125+
root->value[i] = (int)NULL; // Typcast and wrap NULL into an int for equality purposes
126+
root->keys[i] = NULL;
127+
}
119128

120129
return root;
121130
}
@@ -126,7 +135,7 @@ struct node* insertN(int value,struct node *root,struct node *parent){
126135
if (root == NULL){ // If the tree doesn't have a value yet
127136
return newNode(value,parent);
128137
} else {
129-
while(1){
138+
while(1){ // Loop for inserting data in a node rect
130139
if ( (void*)root->value[i] != NULL ){ // If there is a value in the current box
131140
if (value == root->value[i]){ // If the value is already inserted
132141
printf("Data Already inserted!\n");
@@ -152,8 +161,26 @@ struct node* insertN(int value,struct node *root,struct node *parent){
152161

153162
i++;
154163
}
164+
165+
if (root->keyCount > treeOrder){ // Overflow
166+
printf("Overflow! Splitting and Promoting...\n");
167+
struct node *leftHalf = (struct node*)malloc(sizeof(struct node));
168+
struct node *rightHalf = (struct node*)malloc(sizeof(struct node));
169+
}
155170
}
156171

157172
return root;
158173
}
159174

175+
// Printing
176+
void inOrder(struct node *root){
177+
int i;
178+
if (root == NULL) return;
179+
else {
180+
for (i = 0; i < root->keyCount - 1; i++){ // -1 since left and right key of every data box
181+
inOrder(root->keys[i]);
182+
printf("~%d~\n",root->value[i]);
183+
}
184+
}
185+
}
186+

btree.exe

530 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)