diff --git a/a3/binary_search_tree.c b/a3/binary_search_tree.c index 5a6ce53..0114dce 100644 --- a/a3/binary_search_tree.c +++ b/a3/binary_search_tree.c @@ -1,5 +1,6 @@ #include "binary_search_tree.h" #include +#include node_t *build_tree(int *tree_data, size_t tree_data_len) { if (tree_data_len == 0) { @@ -50,24 +51,8 @@ void free_tree(node_t *tree) { } int *sorted_data(node_t *tree) { - int *data = NULL; - size_t data_len = 0; - - 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); - + int *data = malloc(sizeof(int) * count_nodes(tree)); + int *ptr = data; + in_order_traversal(tree, &ptr); return data; -} \ No newline at end of file +}