From 2a72097c202e0b57f7b88b4620779a489c053bf1 Mon Sep 17 00:00:00 2001 From: Michal Utlak Date: Tue, 23 Apr 2024 19:51:08 +0200 Subject: [PATCH] rwererewrw --- a3/binary_search_tree.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/a3/binary_search_tree.c b/a3/binary_search_tree.c index 4cfa7af..b5c381f 100644 --- a/a3/binary_search_tree.c +++ b/a3/binary_search_tree.c @@ -58,18 +58,22 @@ int *sorted_data(node_t *tree) { return sorted_array; } -node_t *build_tree(int *tree_data, size_t tree_data_len){ - - node_t *koren = (node_t *)malloc(sizeof(node_t)); - - if(tree_data == NULL || tree_data_len == 0){ - return 0; +node_t *build_tree(int *tree_data, size_t tree_data_len) { + if (tree_data == NULL || tree_data_len == 0) { + return NULL; } - if(tree_data != NULL && tree_data_len != 0){ - koren->data = tree_data[0]; - koren->left = (node_t *)malloc(sizeof(node_t)); - koren->left->data = tree_data[0]; + node_t *koren = (node_t *)malloc(sizeof(node_t)); + if (koren == NULL) { + return NULL; + } + + koren->data = tree_data[0]; + koren->left = NULL; + koren->right = NULL; + + if (tree_data_len > 1) { + koren->left = build_tree(tree_data + 1, tree_data_len - 1); } return koren;