-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113-bst_search.c
More file actions
42 lines (35 loc) · 834 Bytes
/
113-bst_search.c
File metadata and controls
42 lines (35 loc) · 834 Bytes
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
#include "binary_trees.h"
/**
* find_node - Helper function to find a node
* @tree: Tree (current node) to be checked.
* @node: Pointer to point to found node.
* @value: Value to be searched for
*
*/
void find_node(const bst_t *tree, bst_t **node, int value)
{
if (!tree)
return;
if (tree->n > value)
find_node(tree->left, node, value);
else if (tree->n < value)
find_node(tree->right, node, value);
else if (tree->n == value)
{
*node = (bst_t *)tree;
return;
}
}
/**
* bst_search - Function to search for node in a BST.
* @tree: Pointer to root node of tree to be searched.
* @value: Value to be searched for in tree
* Return: Pointer to new node containing desired value
*
*/
bst_t *bst_search(const bst_t *tree, int value)
{
bst_t *node = NULL;
find_node(tree, &node, value);
return (node);
}