Skip to content

Commit

Permalink
ds: Update binary tree interface
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Dec 2, 2023
1 parent 4908b0a commit 26d227b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ds/4.tree/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

add_library(linked-binary-tree
add_library(linked-binary-tree-static STATIC
linked_binary_tree.c
binary_tree.h
)
52 changes: 45 additions & 7 deletions ds/4.tree/binary_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
Expand All @@ -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_
24 changes: 21 additions & 3 deletions ds/4.tree/linked_binary_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,43 @@

#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

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;
}

0 comments on commit 26d227b

Please sign in to comment.