|
| 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 | + |
1 | 12 | #include<stdio.h> |
2 | 13 | #include<stdlib.h> |
| 14 | +#include<stdbool.h> |
3 | 15 |
|
4 | | -typedefstructnode |
5 | | -{ |
| 16 | +typedefstructnode { |
6 | 17 | intvalue; |
7 | 18 | structnode*left; |
8 | 19 | structnode*right; |
9 | 20 | }node; |
10 | 21 |
|
11 | | -boolsearch_binary_tree(node*,constint); |
12 | | - |
13 | | -boolsearch_binary_tree(node*tree,constintvalue) |
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 | +boolsearch_binary_tree(node*tree,constintvalue) { |
| 31 | +if (tree==NULL) { |
17 | 32 | return false; |
18 | | - } |
19 | | -elseif (value<tree->value) |
20 | | - { |
21 | | -returnsearch(tree->left,value); |
22 | | - } |
23 | | -elseif (value>tree->value) |
24 | | - { |
25 | | -returnsearch(tree->right,value); |
26 | | - } |
27 | | -elseif (value==tree->value) |
28 | | - { |
| 33 | + }elseif (value<tree->value) { |
| 34 | +returnsearch_binary_tree(tree->left,value); |
| 35 | + }elseif (value>tree->value) { |
| 36 | +returnsearch_binary_tree(tree->right,value); |
| 37 | + }elseif (value==tree->value) { |
29 | 38 | return true; |
30 | | - } |
31 | | -else |
32 | | - { |
| 39 | + }else { |
33 | 40 | return false; |
34 | 41 | } |
35 | 42 | } |
36 | 43 |
|
37 | | -intmain(void) |
38 | | -{ |
| 44 | +intmain(void) { |
39 | 45 | return0; |
40 | 46 | } |