Skip to content

Commit

Permalink
ds: Traverse btree
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Dec 3, 2023
1 parent 26d227b commit efe2e4a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
9 changes: 8 additions & 1 deletion ds/4.tree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@
add_library(linked-binary-tree-static STATIC
linked_binary_tree.c
binary_tree.h
)
)

add_executable(binary-tree-test
binary_tree_test.c
)
target_link_libraries(binary-tree-test
linked-binary-tree-static
)
7 changes: 7 additions & 0 deletions ds/4.tree/binary_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ extern size_t binary_tree_size(binary_tree_t* tree);
*/
extern bool binary_tree_is_empty(binary_tree_t* tree);

enum tree_traverse_mode {
kTraverseInOrder = 0,
kTraversePreOrder,
kTraversePostOrder,
};

/**
* Calls |apply| function for each node in binary tree.
*
Expand All @@ -53,6 +59,7 @@ extern bool binary_tree_is_empty(binary_tree_t* tree);
* @param user_data
*/
extern void binary_tree_traverse(binary_tree_t* tree,
tree_traverse_mode mode,
void (*apply)(void* value, void* user_data),
void* user_data);

Expand Down
16 changes: 16 additions & 0 deletions ds/4.tree/binary_tree_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2023 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be
// found in the LICENSE file.

#include <stdio.h>

#include "binary_tree.h"

int main(void) {
binary_tree_t* tree = binary_tree_new();

binary_tree_free(tree);
tree = NULL;

return 0;
}
57 changes: 56 additions & 1 deletion ds/4.tree/linked_binary_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,59 @@ size_t binary_tree_size(binary_tree_t* tree) {
bool binary_tree_is_empty(binary_tree_t* tree) {
assert(tree != NULL);
return tree->size > 0;
}
}

static void tree_node_traverse_in_order(tree_node_t* node,
void (*apply)(void* value, void* user_data),
void* user_data) {
if (node != NULL) {
tree_node_traverse_in_order(node->left, apply, user_data);
apply(node->value, user_data);
tree_node_traverse_in_order(node->right, apply, user_data);
}
}

static void tree_node_traverse_pre_order(tree_node_t* node,
void (*apply)(void* value, void* user_data),
void* user_data) {
if (node != NULL) {
apply(node->value, user_data);
tree_node_traverse_pre_order(node->left, apply, user_data);
tree_node_traverse_pre_order(node->right, apply, user_data);
}
}

static void tree_node_traverse_post_order(tree_node_t* node,
void (*apply)(void* value, void* user_data),
void* user_data) {
if (node != NULL) {
tree_node_traverse_post_order(node->left, apply, user_data);
tree_node_traverse_post_order(node->right, apply, user_data);
apply(node->value, user_data);
}
}

void binary_tree_traverse(binary_tree_t* tree,
tree_traverse_mode mode,
void (*apply)(void* value, void* user_data),
void* user_data) {
assert(tree != NULL);
switch (mode) {
case kTraverseInOrder: {
tree_node_traverse_in_order(tree->root, apply, user_data);
break;
}
case kTraversePreOrder: {
tree_node_traverse_pre_order(tree->root, apply, user_data);
break;
}
case kTraversePostOrder: {
tree_node_traverse_post_order(tree->root, apply, user_data);
break;
}
default: {
fprintf(stderr, "Invalid traverse mode\n");
assert(0);
}
}
}

0 comments on commit efe2e4a

Please sign in to comment.