@@ -10,6 +10,11 @@ struct node {
10
10
int keyCount ;
11
11
} * tRoot = NULL ;
12
12
13
+ struct nodePosition {
14
+ int key ;
15
+ struct node * box ;
16
+ };
17
+
13
18
// Globals
14
19
int treeOrder ;
15
20
@@ -23,6 +28,8 @@ void doInOrder();
23
28
// Tree Operations
24
29
struct node * newNode (int value ,struct node * parent );
25
30
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 );
26
33
27
34
// Printing
28
35
void inOrder (struct node * root );
@@ -94,6 +101,11 @@ void doSearch(){
94
101
95
102
system ("cls" );
96
103
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" );
97
109
98
110
printf ("\n\n\nPress any key to continue...\n" );
99
111
getch ();
@@ -221,6 +233,14 @@ struct node* insertN(int value,struct node *root,struct node *parent){
221
233
//free(tempRoot); // Delete old root node box
222
234
return parent ;
223
235
} 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
+ /*
224
244
for (i = 0; i < parent->keyCount; i++){
225
245
if (parent->keys[i] == tempRoot){
226
246
parent->keys[i] = leftHalf;
@@ -234,6 +254,8 @@ struct node* insertN(int value,struct node *root,struct node *parent){
234
254
}
235
255
}
236
256
}
257
+ */
258
+
237
259
238
260
free (tempRoot );
239
261
return leftHalf ;
@@ -246,6 +268,38 @@ struct node* insertN(int value,struct node *root,struct node *parent){
246
268
return root ;
247
269
}
248
270
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
+
249
303
// Printing
250
304
void inOrder (struct node * root ){
251
305
int i ;
0 commit comments