Repository navigation
Expand file tree
/
Copy pathsearch.c
More file actions
47 lines (37 loc) · 1.46 KB
/
Copy pathsearch.c
File metadata and controls
47 lines (37 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
btreeNode *search(btreeNode *root, int k)
{
if(root->n==0){
printf("Empty B-tree!! ");
return NULL;
}
//btreeNode *p = root; // first points to the root node
int x = root->n; // n is the number of keys in the node
int i = 0;
while (i < x && k > (root->keys[i])) // to find the first greater key than "k"
i++; // increment until the condition results in false
// if the k equals the key at position i
if (root->keys[i] == k)
return root; // then return pointer to that node
// if the node is a leaf node and the key is not found then we return NULL
if (root->leaf == 1)
return NULL;
return search((root->children[i]), k); // recursive call
}
btreeNode *searchInsert(btreeNode *root, int k)
{
if(root->n==0){
return NULL;
}
//btreeNode *p = root; // first points to the root node
int x = root->n; // n is the number of keys in the node
int i = 0;
while (i < x && k > (root->keys[i])) // to find the first greater key than "k"
i++; // increment until the condition results in false
// if the k equals the key at position i
if (root->keys[i] == k)
return root; // then return pointer to that node
// if the node is a leaf node and the key is not found then we return NULL
if (root->leaf == 1)
return NULL;
return searchInsert((root->children[i]), k); // recursive call
}