diff --git a/a3/binary_search_tree.c b/a3/binary_search_tree.c index 6d99c67..4f50c59 100644 --- a/a3/binary_search_tree.c +++ b/a3/binary_search_tree.c @@ -1,37 +1,53 @@ -#include +#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 +#include "binary_search_tree.h" #include -typedef struct node { - struct node *right; - struct node *left; - int data; -} node_t; - - -node_t *insert_node(node_t *root, int data); -void free_tree(node_t *tree); -int *sorted_data(node_t *tree, int *count); -void inorder_traversal(node_t *tree, int *arr, int *index); - - -node_t *insert_node(node_t *root, int data) { - if (root == NULL) { - node_t *new_node = malloc(sizeof(node_t)); - if (new_node == NULL) { - return NULL; - } - new_node->data = data; - new_node->left = new_node->right = NULL; - return new_node; - } - if (data <= root->data) { - root->left = insert_node(root->left, data); - } else { - root->right = insert_node(root->right, data); +node_t *new_node(int value) { + node_t *node = malloc(sizeof(node_t)); + if (node != NULL) { + node->data = value; + node->left = NULL; + node->right = NULL; } + return node; +} + +node_t *insert_node(node_t *root, int value) { + if (root == NULL) + return new_node(value); + + if (value <= root->data) + root->left = insert_node(root->left, value); + else + root->right = insert_node(root->right, value); + return root; } +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++) { + root = insert_node(root, tree_data[i]); + } + return root; +} void free_tree(node_t *tree) { if (tree != NULL) { @@ -41,49 +57,19 @@ void free_tree(node_t *tree) { } } - -void inorder_traversal(node_t *tree, int *arr, int *index) { +void tree_to_sorted_array(node_t *tree, int arr[], int *index) { if (tree != NULL) { - inorder_traversal(tree->left, arr, index); - arr[*index] = tree->data; - (*index)++; - inorder_traversal(tree->right, arr, index); + tree_to_sorted_array(tree->left, arr, index); + arr[(*index)++] = tree->data; + tree_to_sorted_array(tree->right, arr, index); } } - -int *sorted_data(node_t *tree, int *count) { - *count = 0; - inorder_traversal(tree, NULL, count); - - int *arr = malloc(sizeof(int) * (*count)); - if (arr == NULL) { +int *sorted_data(node_t *tree) { + if (tree == NULL) return NULL; - } - int index = 0; - inorder_traversal(tree, arr, &index); + + int *arr = malloc(100 * sizeof(int)); + tree_to_sorted_array(tree, arr, &index); return arr; } - -int main() { - node_t *root = NULL; - int tree_data[] = {4, 2, 6, 1, 3, 5, 7}; - int tree_data_len = sizeof(tree_data) / sizeof(tree_data[0]); - - for (int i = 0; i < tree_data_len; i++) { - root = insert_node(root, tree_data[i]); - } - - int count = 0; - int *sorted = sorted_data(root, &count); - - printf("Sorted data: "); - for (int i = 0; i < count; i++) { - printf("%d ", sorted[i]); - } - printf("\n"); - - free(sorted); - free_tree(root); - return 0; -} \ No newline at end of file