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

Validation exercice 3

parent 2926de72
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment