Initializtion

This commit is contained in:
Kozar 2024-04-25 21:31:24 +02:00
parent a171e83985
commit a2f279f2e3

View File

@ -1,5 +1,6 @@
#include "binary_search_tree.h" #include "binary_search_tree.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
node_t *build_tree(int *tree_data, size_t tree_data_len) { node_t *build_tree(int *tree_data, size_t tree_data_len) {
if (tree_data_len == 0) { if (tree_data_len == 0) {
@ -50,24 +51,8 @@ void free_tree(node_t *tree) {
} }
int *sorted_data(node_t *tree) { int *sorted_data(node_t *tree) {
int *data = NULL; int *data = malloc(sizeof(int) * count_nodes(tree));
size_t data_len = 0; int *ptr = data;
in_order_traversal(tree, &ptr);
void in_order_traversal(node_t *node) {
if (node == NULL) {
return;
}
in_order_traversal(node->left);
data = realloc(data, (data_len + 1) * sizeof(int));
data[data_len] = node->data;
data_len++;
in_order_traversal(node->right);
}
in_order_traversal(tree);
return data; return data;
} }