From 1d07f03672d6daf894267292c1496a1618456aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tokar=C4=8D=C3=ADk?= Date: Sun, 28 Apr 2024 19:58:27 +0000 Subject: [PATCH] Update 'a3/binary_search_tree.c' --- a3/binary_search_tree.c | 128 ++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/a3/binary_search_tree.c b/a3/binary_search_tree.c index 2500079..1f51071 100644 --- a/a3/binary_search_tree.c +++ b/a3/binary_search_tree.c @@ -1,64 +1,66 @@ -#include -#include -#include -#include - - -#include "binary_search_tree.h" -#include - -node_t *create_node(int data) ; - -node_t *create_node(int data) { - node_t *new_node = (node_t *)malloc(sizeof(node_t)); - - new_node->data = data; - new_node->left = NULL; - new_node->right = NULL; - return new_node; -} - - -node_t *build_tree(int *tree_data, size_t tree_data_len) { - - - node_t *root = create_node(tree_data[0]); - - size_t i = 1; -while (i < tree_data_len) { - node_t *new_node = create_node(tree_data[i]); - node_t *current = root; - - while (1) { - if (new_node->data <= current->data) { //porovnanie novy uzol a aktualny - if (current->left == NULL) { - current->left = new_node; - break; - } - current = current->left; - } else { - if (current->right == NULL) { - current->right = new_node; - break; - } - current = current->right; - } - } - - i++; -} - return root; -} - - -// Function to free the memory allocated for the binary search tree -void free_tree(node_t *tree) { - - free(tree); -} - -// Function to return sorted data from the binary search tree -int *sorted_data(node_t *tree) { - - return NULL; +#include +#include +#include +#include + + +#include "binary_search_tree.h" +#include + +node_t *create_node(int data) ; + +node_t *create_node(int data) { + node_t *new_node = (node_t *)malloc(sizeof(node_t)); + + new_node->data = data; + new_node->left = NULL; + new_node->right = NULL; + return new_node; +} + + +node_t *build_tree(int *tree_data, size_t tree_data_len) { + + + node_t *root = create_node(tree_data[0]); + + size_t i = 1; +while (i < tree_data_len) { + node_t *new_node = create_node(tree_data[i]); + node_t *current = root; + + while (1) { + if (new_node->data <= current->data) { //porovnanie novy uzol a aktualny + if (current->left == NULL) { + current->left = new_node; + break; + } + current = current->left; + } else { + if (current->right == NULL) { + current->right = new_node; + break; + } + current = current->right; + } + } + + i++; +} + return root; +} + + +// Function to free the memory allocated for the binary search tree +void free_tree(node_t *tree) { + + free(tree); +} + +// Function to return sorted data from the binary search tree +int *sorted_data(node_t *tree) { +if(tree==NULL){ + return NULL; +} + return NULL; } \ No newline at end of file