Skip to content

Commit 070042a

Browse files
committed
Update search_tree.c
1 parent 5df8478 commit 070042a

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

Tree/search_tree.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
1+
/**
2+
* @file search_tree.c
3+
* @author Xuhua Huang
4+
* @brief Binary search tree implementation
5+
* @version 0.1
6+
* @date 2025-06-08
7+
*
8+
* @copyright Copyright (c) 2025
9+
*
10+
*/
11+
112
#include <stdio.h>
213
#include <stdlib.h>
14+
#include <stdbool.h>
315

4-
typedef struct node
5-
{
16+
typedef struct node {
617
int value;
718
struct node* left;
819
struct node* right;
920
} node;
1021

11-
bool search_binary_tree(node*, const int);
12-
13-
bool search_binary_tree(node* tree, const int value)
14-
{
15-
if (tree == NULL)
16-
{
22+
/**
23+
* @brief Recursively searches for a value in a binary search tree (BST).
24+
* Assumes the BST property: left < root < right.
25+
*
26+
* @param root Pointer to the root node of the tree.
27+
* @param target The value to search for.
28+
* @return true if the value is found, false otherwise.
29+
*/
30+
bool search_binary_tree(node* tree, const int value) {
31+
if (tree == NULL) {
1732
return false;
18-
}
19-
else if (value < tree->value)
20-
{
21-
return search(tree->left, value);
22-
}
23-
else if (value > tree->value)
24-
{
25-
return search(tree->right, value);
26-
}
27-
else if (value == tree->value)
28-
{
33+
} else if (value < tree->value) {
34+
return search_binary_tree(tree->left, value);
35+
} else if (value > tree->value) {
36+
return search_binary_tree(tree->right, value);
37+
} else if (value == tree->value) {
2938
return true;
30-
}
31-
else
32-
{
39+
} else {
3340
return false;
3441
}
3542
}
3643

37-
int main(void)
38-
{
44+
int main(void) {
3945
return 0;
4046
}

0 commit comments

Comments
 (0)