Skip to content

Commit ec4709c

Browse files
author
Raven G. Duran
committed
Added SearchNValue and SearchNBox
1 parent b84b724 commit ec4709c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

btree.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ struct node {
1010
int keyCount;
1111
} *tRoot = NULL;
1212

13+
struct nodePosition {
14+
int key;
15+
struct node* box;
16+
};
17+
1318
// Globals
1419
int treeOrder;
1520

@@ -23,6 +28,8 @@ void doInOrder();
2328
// Tree Operations
2429
struct node* newNode(int value,struct node *parent);
2530
struct node* insertN(int value,struct node *root,struct node *parent);
31+
struct node* searchNValue(int value,struct node *root);
32+
struct nodePosition* searchNBox(struct node *toFind,struct node *root);
2633

2734
// Printing
2835
void inOrder(struct node *root);
@@ -94,6 +101,11 @@ void doSearch(){
94101

95102
system("cls");
96103
printf("--- Search ---\n");
104+
printf("To Find: ");
105+
scanf("%d",&toFind);
106+
107+
if (searchNValue(toFind,tRoot) == NULL) printf("Not found!\n");
108+
else printf("found!\n");
97109

98110
printf("\n\n\nPress any key to continue...\n");
99111
getch();
@@ -221,6 +233,14 @@ struct node* insertN(int value,struct node *root,struct node *parent){
221233
//free(tempRoot); // Delete old root node box
222234
return parent;
223235
} else {
236+
// Find the parent of the current left and right box
237+
struct nodePosition *nodeF = searchNBox(tempRoot,parent);
238+
if (nodeF != NULL){
239+
nodeF->box->keys[nodeF->key] = leftHalf;
240+
nodeF->box->keys[nodeF->key+1] = rightHalf;
241+
}
242+
243+
/*
224244
for (i = 0; i < parent->keyCount; i++){
225245
if (parent->keys[i] == tempRoot){
226246
parent->keys[i] = leftHalf;
@@ -234,6 +254,8 @@ struct node* insertN(int value,struct node *root,struct node *parent){
234254
}
235255
}
236256
}
257+
*/
258+
237259

238260
free(tempRoot);
239261
return leftHalf;
@@ -246,6 +268,38 @@ struct node* insertN(int value,struct node *root,struct node *parent){
246268
return root;
247269
}
248270

271+
struct node* searchNValue(int value,struct node *root){
272+
int i;
273+
struct node *nodeF = NULL;
274+
if (root == NULL) return NULL;
275+
else {
276+
for (i = 0; i < root->keyCount; i++){ // -1 since left and right key of every data box
277+
if (root->value[i] == value) return root;
278+
else if (nodeF != NULL) return nodeF;
279+
else nodeF = searchNValue(value,root->keys[i]);
280+
}
281+
return nodeF;
282+
}
283+
}
284+
285+
struct nodePosition* searchNBox(struct node *toFind,struct node *root){
286+
int i;
287+
struct nodePosition *nodeF = NULL;
288+
if (root == NULL) return NULL;
289+
else {
290+
for (i = 0; i < root->keyCount; i++){ // -1 since left and right key of every data box
291+
if (root == toFind) {
292+
nodeF->box = root;
293+
nodeF->key = i;
294+
return nodeF;
295+
}
296+
else if (nodeF != NULL) return nodeF;
297+
else nodeF = searchNBox(toFind,root->keys[i]);
298+
}
299+
return nodeF;
300+
}
301+
}
302+
249303
// Printing
250304
void inOrder(struct node *root){
251305
int i;

btree.exe

574 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)