From baa71386d2e38866ffc6443cffd7726988617822 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Sun, 18 Jan 2026 14:41:07 +0000 Subject: [PATCH] [Sync Iteration] c/binary-search-tree/1 --- .../binary-search-tree/1/binary_search_tree.c | 94 +++++++++++++++++++ .../binary-search-tree/1/binary_search_tree.h | 17 ++++ 2 files changed, 111 insertions(+) create mode 100644 solutions/c/binary-search-tree/1/binary_search_tree.c create mode 100644 solutions/c/binary-search-tree/1/binary_search_tree.h diff --git a/solutions/c/binary-search-tree/1/binary_search_tree.c b/solutions/c/binary-search-tree/1/binary_search_tree.c new file mode 100644 index 0000000..14a4dc5 --- /dev/null +++ b/solutions/c/binary-search-tree/1/binary_search_tree.c @@ -0,0 +1,94 @@ +#include "binary_search_tree.h" +#include +#include +#include +#include + +// typedef struct node node_t; +// +// struct node { +// node_t *right; +// node_t *left; +// int data; +// }; + +node_t *build_tree(int *tree_data, size_t tree_data_len){ + if (!tree_data || tree_data_len == 0) return NULL; + + node_t *tree = calloc(tree_data_len, sizeof(*tree)); + if (!tree) return NULL; + + node_t *node = tree; + + node->data = *tree_data++; + node++; + tree_data_len--; + + while (tree_data_len > 0) { + node_t *current = tree; + int val = *tree_data++; + + while (1) { + if (val > current->data) { + if (!current->right) { + current->right = node; + break; + } + current = current->right; + } else { + if (!current->left) { + current->left = node; + break; + } + current = current->left; + } + } + + node->data = val; + node++; + tree_data_len--; + } + + return tree; +} + +void free_tree(node_t *tree){ + if (tree) free(tree); +} + +static void populate_from_tree(int **arr, size_t *curr, size_t *cap, node_t *node) { + if (!node) return; + + populate_from_tree(arr, curr, cap, node->left); + + if (*curr >= *cap) { + *cap = (*cap * 2); + int *tmp = realloc(*arr, *cap * sizeof(int)); + assert(tmp); + *arr = tmp; + } + + (*arr)[*curr] = node->data; + (*curr)++; + + populate_from_tree(arr, curr, cap, node->right); +} + +int *sorted_data(node_t *tree){ + if (!tree) return NULL; + + size_t cap = 10; + int *arr = calloc(cap, sizeof(*arr)); + if (!arr) return NULL; + size_t curr = 0; + populate_from_tree(&arr, &curr, &cap, tree); + + if (curr < cap) { + int *tmp = realloc(arr, curr * sizeof(*tmp)); + if (!tmp) { free(arr); return NULL; } + arr = tmp; + } + + return arr; +} + diff --git a/solutions/c/binary-search-tree/1/binary_search_tree.h b/solutions/c/binary-search-tree/1/binary_search_tree.h new file mode 100644 index 0000000..483aa7f --- /dev/null +++ b/solutions/c/binary-search-tree/1/binary_search_tree.h @@ -0,0 +1,17 @@ +#ifndef BINARY_SEARCH_TREE_H +#define BINARY_SEARCH_TREE_H +#include + +typedef struct node node_t; + +struct node { + node_t *right; + node_t *left; + int data; +}; + +node_t *build_tree(int *tree_data, size_t tree_data_len); +void free_tree(node_t *tree); +int *sorted_data(node_t *tree); + +#endif