Skip to content
Snippets Groups Projects
Commit 6b00b587 authored by florian.burgener's avatar florian.burgener
Browse files

Validation exercice 3

parent 2926de72
Branches
No related tags found
No related merge requests found
...@@ -9,12 +9,10 @@ ...@@ -9,12 +9,10 @@
* *
*/ */
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
// LinkedListNode.h // LinkedListNode.h
...@@ -206,14 +204,10 @@ typedef struct Node { ...@@ -206,14 +204,10 @@ typedef struct Node {
Node *node_init(); Node *node_init();
void node_destroy(Node **node); void node_destroy(Node **node);
Node *binary_search_tree_init(); Node *binary_search_tree_init();
void binary_search_tree_destroy(Node **root); void binary_search_tree_destroy(Node **root);
Node *binary_search_tree_insert(Node *root, int32_t key); Node *binary_search_tree_insert(Node *root, int32_t key);
Node *binary_search_tree_search(Node *root, int32_t key); bool binary_search_tree_is_leaf(Node *root);
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);
// BinarySearchTree.c // BinarySearchTree.c
...@@ -257,73 +251,23 @@ Node *binary_search_tree_insert(Node *root, int32_t key) { ...@@ -257,73 +251,23 @@ Node *binary_search_tree_insert(Node *root, int32_t key) {
return root; return root;
} }
Node *binary_search_tree_search(Node *root, int32_t key) { bool binary_search_tree_is_leaf(Node *root) {
if (root == NULL) { return root->left == NULL && root->right == 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 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 *search_path(Node *root, int key) {
Stack *s = stack_init(); 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) { if (key < root->key) {
root = root->left; root = root->left;
stack_push(s, 'g'); stack_push(s, 'g');
...@@ -337,19 +281,26 @@ Stack *search_path(Node *root, int key) { ...@@ -337,19 +281,26 @@ Stack *search_path(Node *root, int key) {
} }
int main() { int main() {
Node *root = binary_search_tree_init(); // !!!
const int KEY_1 = 24;
const int KEY_2 = 7;
int numbers_length = 9; int numbers_length = 9;
int numbers[] = {10, 0, 20, -5, 5, 15, 25, 22, 24}; 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) { for (int i = 0; i < numbers_length; i += 1) {
root = binary_search_tree_insert(root, numbers[i]); 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_print(s);
stack_destroy(&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_print(s);
stack_destroy(&s); stack_destroy(&s);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment