diff --git a/ex3/ex3.c b/ex3/ex3.c index aa630d2571b9beb07d53d1420b43c759e9885395..6d9b32e200750be66eeec00a668ee6b199e66edf 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,12 +9,10 @@ * */ -#include <math.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> // LinkedListNode.h @@ -206,14 +204,10 @@ typedef struct Node { Node *node_init(); void node_destroy(Node **node); - Node *binary_search_tree_init(); void binary_search_tree_destroy(Node **root); Node *binary_search_tree_insert(Node *root, int32_t key); -Node *binary_search_tree_search(Node *root, int32_t key); -Node *binary_search_tree_find_smallest_node(Node *root); -Node *binary_search_tree_delete(Node *root, int32_t key); -void binary_search_tree_print(Node *root, int32_t depth); +bool binary_search_tree_is_leaf(Node *root); // BinarySearchTree.c @@ -257,73 +251,23 @@ Node *binary_search_tree_insert(Node *root, int32_t key) { return root; } -Node *binary_search_tree_search(Node *root, int32_t key) { - if (root == NULL) { - return NULL; - } - - if (key < root->key) { - return binary_search_tree_search(root->left, key); - } else if (key > root->key) { - return binary_search_tree_search(root->right, key); - } - - return root; -} - -Node *binary_search_tree_find_smallest_node(Node *root) { - while (root->left != NULL) { - root = root->left; - } - - return root; -} - -Node *binary_search_tree_delete(Node *root, int32_t key) { - if (root == NULL) { - return NULL; - } - - if (key < root->key) { - root->left = binary_search_tree_delete(root->left, key); - } else if (key > root->key) { - root->right = binary_search_tree_delete(root->right, key); - } else { - if (root->left == NULL && root->right == NULL) { - node_destroy(&root); - return NULL; - } - - if (root->left == NULL) { - Node *right = root->right; - node_destroy(&root); - return right; - } - - if (root->right == NULL) { - Node *left = root->left; - node_destroy(&root); - return left; - } - - Node *smallest = binary_search_tree_find_smallest_node(root->right); - root->key = smallest->key; - root->right = binary_search_tree_delete(root->right, smallest->key); - } - - return root; +bool binary_search_tree_is_leaf(Node *root) { + return root->left == NULL && root->right == NULL; } // --- -bool node_is_leaf(Node *node) { - return node->left == NULL && node->right == NULL; -} - +/** + * @brief Finds a key and stores the path in a stack. + * + * @param root + * @param key + * @return Stack* + */ Stack *search_path(Node *root, int key) { Stack *s = stack_init(); - while (root != NULL && !node_is_leaf(root) && root->key != key) { + while (root != NULL && !binary_search_tree_is_leaf(root) && root->key != key) { if (key < root->key) { root = root->left; stack_push(s, 'g'); @@ -337,19 +281,26 @@ Stack *search_path(Node *root, int key) { } int main() { - Node *root = binary_search_tree_init(); + // !!! + const int KEY_1 = 24; + const int KEY_2 = 7; + int numbers_length = 9; int numbers[] = {10, 0, 20, -5, 5, 15, 25, 22, 24}; + Node *root = binary_search_tree_init(); + for (int i = 0; i < numbers_length; i += 1) { root = binary_search_tree_insert(root, numbers[i]); } - Stack *s = search_path(root, 24); + Stack *s = search_path(root, KEY_1); + printf("Path for %d\n", KEY_1); stack_print(s); stack_destroy(&s); - s = search_path(root, 7); + s = search_path(root, KEY_2); + printf("Path for %d\n", KEY_2); stack_print(s); stack_destroy(&s);