|
4 | 4 | * @brief Binary search tree implementation |
5 | 5 | * @version 0.1 |
6 | 6 | * @date 2025-06-08 |
7 | | - * |
| 7 | + * |
8 | 8 | * @copyright Copyright (c) 2025 |
9 | | - * |
| 9 | + * |
10 | 10 | */ |
11 | 11 |
|
| 12 | +#include <stdbool.h> |
12 | 13 | #include <stdio.h> |
13 | 14 | #include <stdlib.h> |
14 | | -#include <stdbool.h> |
15 | 15 |
|
16 | 16 | typedef struct node { |
17 | 17 | int value; |
18 | 18 | struct node* left; |
19 | 19 | struct node* right; |
20 | 20 | } node; |
21 | 21 |
|
| 22 | +node* create_node(int value); |
| 23 | + |
| 24 | +bool insert_node(node** root, int value); |
| 25 | +bool search_binary_tree(const node* root, int target); |
| 26 | +bool delete_node(node** root, int value); |
| 27 | +void inorder_traversal(const node* root); |
| 28 | +void preorder_traversal(const node* root); |
| 29 | +void postorder_traversal(const node* root); |
| 30 | +void free_tree(node* root); |
| 31 | +int compute_height(const node* root); |
| 32 | + |
| 33 | +int main(void) { |
| 34 | + node* root = NULL; |
| 35 | + |
| 36 | + // Insertion |
| 37 | + insert_node(&root, 50); |
| 38 | + insert_node(&root, 30); |
| 39 | + insert_node(&root, 70); |
| 40 | + insert_node(&root, 20); |
| 41 | + insert_node(&root, 40); |
| 42 | + insert_node(&root, 60); |
| 43 | + insert_node(&root, 80); |
| 44 | + |
| 45 | + // Traversals |
| 46 | + printf("In-order Traversal : "); |
| 47 | + inorder_traversal(root); |
| 48 | + printf("\n"); |
| 49 | + |
| 50 | + printf("Pre-order Traversal : "); |
| 51 | + preorder_traversal(root); |
| 52 | + printf("\n"); |
| 53 | + |
| 54 | + printf("Post-order Traversal : "); |
| 55 | + postorder_traversal(root); |
| 56 | + printf("\n"); |
| 57 | + |
| 58 | + // Search |
| 59 | + printf("Search 60: %s\n", search_binary_tree(root, 60) ? "Found" : "Not Found"); |
| 60 | + printf("Search 99: %s\n", search_binary_tree(root, 99) ? "Found" : "Not Found"); |
| 61 | + |
| 62 | + // Deletion |
| 63 | + delete_node(&root, 70); |
| 64 | + printf("After deleting 70, In-order Traversal: "); |
| 65 | + inorder_traversal(root); |
| 66 | + printf("\n"); |
| 67 | + |
| 68 | + // Cleanup |
| 69 | + free_tree(root); |
| 70 | + return EXIT_SUCCESS; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @brief Create a node object |
| 75 | + * |
| 76 | + * @param value The value to assign to the new node. |
| 77 | + * @return node* A pointer to the newly created node. |
| 78 | + */ |
| 79 | +node* create_node(int value) { |
| 80 | + node* new_node = malloc(sizeof(node)); |
| 81 | + if (new_node == NULL) { |
| 82 | + perror("Failed to allocate memory for new node"); |
| 83 | + exit(EXIT_FAILURE); |
| 84 | + } |
| 85 | + new_node->value = value; |
| 86 | + new_node->left = NULL; |
| 87 | + new_node->right = NULL; |
| 88 | + return new_node; |
| 89 | +} |
| 90 | + |
22 | 91 | /** |
23 | | - * @brief Recursively searches for a value in a binary search tree (BST). |
24 | | - * Assumes the BST property: left < root < right. |
| 92 | + * @brief Inserts a value into the binary search tree (BST). |
| 93 | + * |
| 94 | + * @param root Pointer to the root node of the tree. |
| 95 | + * @param value The value to insert. |
| 96 | + * @return true if the value was inserted successfully, false otherwise. |
| 97 | + */ |
| 98 | +bool insert_node(node** root, int value) { |
| 99 | + if (root == NULL) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + if (*root == NULL) { |
| 104 | + *root = create_node(value); |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + if (value < (*root)->value) { |
| 109 | + return insert_node(&((*root)->left), value); |
| 110 | + } else if (value > (*root)->value) { |
| 111 | + return insert_node(&((*root)->right), value); |
| 112 | + } |
| 113 | + |
| 114 | + return false; // Duplicate value, do not insert |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief Searches for a target value in a binary search tree (BST). |
25 | 119 | * |
26 | 120 | * @param root Pointer to the root node of the tree. |
27 | 121 | * @param target The value to search for. |
28 | 122 | * @return true if the value is found, false otherwise. |
29 | 123 | */ |
30 | | -bool search_binary_tree(node* tree, const int value) { |
31 | | - if (tree == NULL) { |
| 124 | +bool search_binary_tree(const node* root, int target) { |
| 125 | + if (root == NULL) { |
32 | 126 | return false; |
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) { |
38 | | - return true; |
39 | | - } else { |
| 127 | + } |
| 128 | + |
| 129 | + if (target < root->value) { |
| 130 | + return search_binary_tree(root->left, target); |
| 131 | + } else if (target > root->value) { |
| 132 | + return search_binary_tree(root->right, target); |
| 133 | + } |
| 134 | + |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * @brief Finds the node with the minimum value in a binary search tree (BST). |
| 140 | + * |
| 141 | + * @param root Pointer to the root node of the tree. |
| 142 | + * @return node* Pointer to the node with the minimum value, or NULL if the tree is empty. |
| 143 | + */ |
| 144 | +node* find_min(node* root) { |
| 145 | + while (root && root->left != NULL) { |
| 146 | + root = root->left; |
| 147 | + } |
| 148 | + return root; |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * @brief Deletes a node with the specified value from the binary search tree (BST). |
| 153 | + * |
| 154 | + * @param root Pointer to the root node of the tree. |
| 155 | + * @param value The value to delete. |
| 156 | + * @return true if the node was deleted successfully, false otherwise. |
| 157 | + */ |
| 158 | +bool delete_node(node** root, int value) { |
| 159 | + if (root == NULL || *root == NULL) { |
40 | 160 | return false; |
41 | 161 | } |
| 162 | + |
| 163 | + if (value < (*root)->value) { |
| 164 | + return delete_node(&((*root)->left), value); |
| 165 | + } else if (value > (*root)->value) { |
| 166 | + return delete_node(&((*root)->right), value); |
| 167 | + } else { |
| 168 | + // Node found |
| 169 | + node* temp; |
| 170 | + if ((*root)->left == NULL && (*root)->right == NULL) { |
| 171 | + free(*root); |
| 172 | + *root = NULL; |
| 173 | + } else if ((*root)->left == NULL) { |
| 174 | + temp = *root; |
| 175 | + *root = (*root)->right; |
| 176 | + free(temp); |
| 177 | + } else if ((*root)->right == NULL) { |
| 178 | + temp = *root; |
| 179 | + *root = (*root)->left; |
| 180 | + free(temp); |
| 181 | + } else { |
| 182 | + temp = find_min((*root)->right); |
| 183 | + (*root)->value = temp->value; |
| 184 | + delete_node(&((*root)->right), temp->value); |
| 185 | + } |
| 186 | + return true; |
| 187 | + } |
42 | 188 | } |
43 | 189 |
|
44 | | -int main(void) { |
45 | | - return 0; |
| 190 | +/** |
| 191 | + * @brief Performs an inorder traversal of the binary search tree (BST). |
| 192 | + * |
| 193 | + * @param root Pointer to the root node of the tree. |
| 194 | + */ |
| 195 | +void inorder_traversal(const node* root) { |
| 196 | + if (root == NULL) { |
| 197 | + return; |
| 198 | + } |
| 199 | + inorder_traversal(root->left); |
| 200 | + printf("%d ", root->value); |
| 201 | + inorder_traversal(root->right); |
| 202 | +} |
| 203 | + |
| 204 | +/** |
| 205 | + * @brief Performs a preorder traversal of the binary search tree (BST). |
| 206 | + * |
| 207 | + * @param root Pointer to the root node of the tree. |
| 208 | + */ |
| 209 | +void preorder_traversal(const node* root) { |
| 210 | + if (root == NULL) { |
| 211 | + return; |
| 212 | + } |
| 213 | + printf("%d ", root->value); |
| 214 | + preorder_traversal(root->left); |
| 215 | + preorder_traversal(root->right); |
| 216 | +} |
| 217 | + |
| 218 | +/** |
| 219 | + * @brief Performs a postorder traversal of the binary search tree (BST). |
| 220 | + * |
| 221 | + * @param root Pointer to the root node of the tree. |
| 222 | + */ |
| 223 | +void postorder_traversal(const node* root) { |
| 224 | + if (root == NULL) { |
| 225 | + return; |
| 226 | + } |
| 227 | + postorder_traversal(root->left); |
| 228 | + postorder_traversal(root->right); |
| 229 | + printf("%d ", root->value); |
| 230 | +} |
| 231 | + |
| 232 | +/** |
| 233 | + * @brief Frees all nodes in the binary search tree (BST). |
| 234 | + * |
| 235 | + * @param root Pointer to the root node of the tree. |
| 236 | + */ |
| 237 | +void free_tree(node* root) { |
| 238 | + if (root == NULL) { |
| 239 | + return; |
| 240 | + } |
| 241 | + free_tree(root->left); |
| 242 | + free_tree(root->right); |
| 243 | + free(root); |
| 244 | +} |
| 245 | + |
| 246 | +/** |
| 247 | + * @brief Compute the height of a binary tree. |
| 248 | + * |
| 249 | + * @param root Pointer to the root of the tree. |
| 250 | + * @return Height of the tree in number of edges (−1 for empty tree). |
| 251 | + */ |
| 252 | +int compute_height(const node* root) { |
| 253 | + if (root == NULL) { |
| 254 | + return -1; |
| 255 | + } |
| 256 | + |
| 257 | + int left_height = compute_height(root->left); |
| 258 | + int right_height = compute_height(root->right); |
| 259 | + |
| 260 | + return (left_height > right_height ? left_height : right_height) + 1; |
46 | 261 | } |
0 commit comments