From 0cd0b96b00ea035bff54a9256a25a2c825ede7d7 Mon Sep 17 00:00:00 2001 From: Miloslav Macko Date: Thu, 18 Apr 2024 15:20:29 +0200 Subject: [PATCH] 123 --- a3/binary_search_tree.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/a3/binary_search_tree.c b/a3/binary_search_tree.c index acd2e21..3a28213 100644 --- a/a3/binary_search_tree.c +++ b/a3/binary_search_tree.c @@ -1,11 +1,5 @@ -#include #include "binary_search_tree.h" - -typedef struct node { - struct node *right; - struct node *left; - int data; -} node_t; +#include node_t* insert(node_t *node, int data) { if (node == NULL) { @@ -22,6 +16,13 @@ node_t* insert(node_t *node, int data) { } } +void inorder_traversal(node_t *node, int *sorted) { + if (node == NULL) return; + inorder_traversal(node->left, sorted); + sorted[sorted_index++] = node->data; + inorder_traversal(node->right, sorted); +} + node_t* build_tree(int *tree_data, size_t tree_data_len) { node_t *root = NULL; for (size_t i = 0; i < tree_data_len; i++) { @@ -37,15 +38,6 @@ void free_tree(node_t *tree) { free(tree); } -static int sorted_index = 0; - -void inorder_traversal(node_t *node, int *sorted) { - if (node == NULL) return; - inorder_traversal(node->left, sorted); - sorted[sorted_index++] = node->data; - inorder_traversal(node->right, sorted); -} - int *sorted_data(node_t *tree) { int *sorted = (int*)malloc(sizeof(int) * 1000); // Předpokládáme, že strom má maximálně 1000 prvků sorted_index = 0;