From bcf92ad0705fac9f44467a7f43e9b1b8dcf1077c Mon Sep 17 00:00:00 2001 From: ak643du Date: Thu, 25 Apr 2024 21:33:23 +0200 Subject: [PATCH] Initializtion --- a3/binary_search_tree.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/a3/binary_search_tree.c b/a3/binary_search_tree.c index 0114dce..536dc91 100644 --- a/a3/binary_search_tree.c +++ b/a3/binary_search_tree.c @@ -2,6 +2,9 @@ #include #include +int count_nodes(node_t *tree); +void in_order_traversal(node_t *tree, int **ptr); + node_t *build_tree(int *tree_data, size_t tree_data_len) { if (tree_data_len == 0) { return NULL; @@ -56,3 +59,22 @@ int *sorted_data(node_t *tree) { in_order_traversal(tree, &ptr); return data; } + +int count_nodes(node_t *tree) { + if (tree == NULL) { + return 0; + } + + return 1 + count_nodes(tree->left) + count_nodes(tree->right); +} + +void in_order_traversal(node_t *tree, int **ptr) { + if (tree == NULL) { + return; + } + + in_order_traversal(tree->left, ptr); + **ptr = tree->data; + (*ptr)++; + in_order_traversal(tree->right, ptr); +} \ No newline at end of file