From 26d227b2a38880ac920882efa4a49322745f6c50 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Sat, 2 Dec 2023 22:48:40 +0800 Subject: [PATCH] ds: Update binary tree interface --- ds/4.tree/CMakeLists.txt | 2 +- ds/4.tree/binary_tree.h | 52 +++++++++++++++++++++++++++++----- ds/4.tree/linked_binary_tree.c | 24 ++++++++++++++-- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/ds/4.tree/CMakeLists.txt b/ds/4.tree/CMakeLists.txt index 8e70e747..665882e6 100644 --- a/ds/4.tree/CMakeLists.txt +++ b/ds/4.tree/CMakeLists.txt @@ -1,5 +1,5 @@ -add_library(linked-binary-tree +add_library(linked-binary-tree-static STATIC linked_binary_tree.c binary_tree.h ) \ No newline at end of file diff --git a/ds/4.tree/binary_tree.h b/ds/4.tree/binary_tree.h index 7ad7311f..eaa9d7e1 100644 --- a/ds/4.tree/binary_tree.h +++ b/ds/4.tree/binary_tree.h @@ -2,9 +2,11 @@ // Use of this source is governed by GNU General Public License // that can be found in the LICENSE file. -#ifndef DATA_STRUCTURES_3_TREE_BINARY_TREE_H_ -#define DATA_STRUCTURES_3_TREE_BINARY_TREE_H_ +#ifndef BINARY_TREE_H_ +#define BINARY_TREE_H_ +#include +#include #include #ifdef __cplusplus @@ -13,13 +15,49 @@ extern "C" { typedef struct binary_tree_s binary_tree_t; -#define element_type int32_t - -binary_tree_t* binary_tree_new(); -void binary_tree_free(binary_tree_t* tree); +/** + * Create an empty binary tree. + * + * @return + */ +extern binary_tree_t* binary_tree_new(); + +/** + * Deallocate tree nodes in binary tree and free the binary tree descriptor. + * + * @param tree + */ +extern void binary_tree_free(binary_tree_t* tree); + +/** + * Get number of elements in binary tree. + * + * @param tree + * @return + */ +extern size_t binary_tree_size(binary_tree_t* tree); + +/** + * Returns true if binary tree has no node. + * + * @param tree + * @return + */ +extern bool binary_tree_is_empty(binary_tree_t* tree); + +/** + * Calls |apply| function for each node in binary tree. + * + * @param tree + * @param apply + * @param user_data + */ +extern void binary_tree_traverse(binary_tree_t* tree, + void (*apply)(void* value, void* user_data), + void* user_data); #ifdef __cplusplus } #endif -#endif // DATA_STRUCTURES_3_TREE_BINARY_TREE_H_ +#endif // BINARY_TREE_H_ diff --git a/ds/4.tree/linked_binary_tree.c b/ds/4.tree/linked_binary_tree.c index 0ccf3111..cf7f3701 100644 --- a/ds/4.tree/linked_binary_tree.c +++ b/ds/4.tree/linked_binary_tree.c @@ -6,6 +6,7 @@ #include #include +#include #include typedef struct tree_node_s tree_node_t; @@ -13,18 +14,35 @@ typedef struct tree_node_s tree_node_t; struct tree_node_s { tree_node_t* left; tree_node_t* right; - element_type value; + void* value; }; struct binary_tree_s { - tree_node_t* root; size_t size; + tree_node_t* root; }; binary_tree_t* binary_tree_new() { + binary_tree_t* tree = malloc(sizeof(binary_tree_t)); + assert(tree != NULL); + tree->size = 0; + tree->root = NULL; + return tree; } void binary_tree_free(binary_tree_t* tree) { - + assert(tree != NULL); + // TODO(Shaohua): deallocate tree nodes. + free(tree); +} + +size_t binary_tree_size(binary_tree_t* tree) { + assert(tree != NULL); + return tree->size; +} + +bool binary_tree_is_empty(binary_tree_t* tree) { + assert(tree != NULL); + return tree->size > 0; } \ No newline at end of file