From f644616cc4869a6b16acae21a249657e91dddbaf Mon Sep 17 00:00:00 2001 From: Michal Utlak Date: Tue, 23 Apr 2024 19:53:07 +0200 Subject: [PATCH] rwerw --- a3/binary_search_tree.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/a3/binary_search_tree.c b/a3/binary_search_tree.c index b5c381f..5528fa4 100644 --- a/a3/binary_search_tree.c +++ b/a3/binary_search_tree.c @@ -30,29 +30,21 @@ int *sorted_data(node_t *tree) { return NULL; } - size_t index = 0; - node_t *current_node = tree; - while (current_node != NULL) { - if (current_node->left == NULL) { - sorted_array[index++] = current_node->data; - current_node = current_node->right; - } else { - node_t *predecessor = current_node->left; - while (predecessor->right != NULL && predecessor->right != current_node) { - predecessor = predecessor->right; - } + node_t *stack[num_nodes]; + size_t stack_top = 0; - if (predecessor->right == NULL) { - predecessor->right = current_node; - current_node = current_node->left; - } else { - predecessor->right = NULL; - sorted_array[index++] = current_node->data; - current_node = current_node->right; - } + while (tree != NULL || stack_top > 0) { + while (tree != NULL) { + stack[stack_top++] = tree; + tree = tree->left; } + + tree = stack[--stack_top]; + sorted_array[index++] = tree->data; + + tree = tree->right; } return sorted_array;