-
Notifications
You must be signed in to change notification settings - Fork 30
/
bstree.h
151 lines (134 loc) · 4.21 KB
/
bstree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @file bstree.h
*
* @author hutusi ([email protected])
*
* @brief Binary search tree.
*
* @date 2019-07-20
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#ifndef RETHINK_C_BS_TREE_H
#define RETHINK_C_BS_TREE_H
/**
* @brief The type of a value to be stored in a @ref BSTreeNode.
* (void *) can be changed to int, long, or other types if needed.
*/
typedef void *BSTreeValue;
/**
* @brief Definition of a @ref BSTreeNode.
*
*/
typedef struct _BSTreeNode {
/** Value of the node. */
BSTreeValue data;
/** Parent node pointer. */
struct _BSTreeNode *parent;
/** Left child node pointer. */
struct _BSTreeNode *left;
/** Right child node pointer. */
struct _BSTreeNode *right;
} BSTreeNode;
typedef int (*BSTreeCompareFunc)(BSTreeValue data1, BSTreeValue data2);
/**
* @brief Definition of a @ref BSTree.
*
*/
typedef struct _BSTree {
/** Root node of the @ref BSTree pointer. */
struct _BSTreeNode *root;
/** Compare two node value when do searching in BSTree. */
BSTreeCompareFunc compare_func;
/** The number of nodes of the @ref BSTree. */
unsigned int num_nodes;
} BSTree;
/**
* @brief Allcate a new BSTree.
*
* @param compare_func Compare two node value when do searching in BSTree.
* @return BSTree* The new BSTree if success, otherwise return NULL.
*/
BSTree *bs_tree_new(BSTreeCompareFunc compare_func);
/**
* @brief Delete a BSTree and free back memory.
*
* @param tree The BSTree to delete.
*/
void bs_tree_free(BSTree *tree);
/**
* @brief Get the leftmost child node of a BSTreeNode.
*
* @param node The BSTreeNode.
* @return BSTreeNode* The leftmost BSTreeNode. If the BSTreeNode has
* no left child, return itself.
*/
BSTreeNode *bs_tree_leftmost_node(BSTreeNode *node);
/**
* @brief Get the rightmost child node of a BSTreeNode.
*
* @param node The BSTreeNode.
* @return BSTreeNode* The rightmost BSTreeNode. If the BSTreeNode has
* no right child, return itself.
*/
BSTreeNode *bs_tree_rightmost_node(BSTreeNode *node);
/**
* @brief Insert a BSTreeValue to a BSTree.
*
* @param tree The BSTree.
* @param data The value to insert.
* @return BSTreeNode* The new inserted BSTreeNode if success,
* otherwise return NULL.
*/
BSTreeNode *bs_tree_insert(BSTree *tree, BSTreeValue data);
/**
* @brief Remove a BSTreeNode from a BSTree.
*
* @param tree The BSTree.
* @param node The BSTreeNode.
* @return BSTreeNode* The removed BSTreeNode if success,
* otherwise return NULL.
*/
BSTreeNode *bs_tree_remove_node(BSTree *tree, BSTreeNode *node);
/**
* @brief Find a BSTreeNode value in a BSTree.
*
* @param tree The BSTree.
* @param data The BSTreeNode value to lookup.
* @return BSTreeNode* The matched BSTreeNode if success,
* otherwise return NULL.
*/
BSTreeNode *bs_tree_lookup_data(BSTree *tree, BSTreeValue data);
typedef void (*BSTreeTraverseFunc)(BSTreeNode *node, void *args);
/**
* @brief Traverse BSTree by preorder.
*
* @param tree The BSTree.
* @param callback The callback function do to each BSTreeNode in traverse.
* @param cb_args The callback function's args.
*/
void bs_tree_preorder_traverse(BSTree *tree,
BSTreeTraverseFunc callback,
void *cb_args);
/**
* @brief Traverse BSTree by inorder.
*
* @param tree The BSTree.
* @param callback The callback function do to each BSTreeNode in traverse.
* @param cb_args The callback function's args.
*/
void bs_tree_inorder_traverse(BSTree *tree,
BSTreeTraverseFunc callback,
void *cb_args);
/**
* @brief Traverse BSTree by postorder.
*
* @param tree The BSTree.
* @param callback The callback function do to each BSTreeNode in traverse.
* @param cb_args The callback function's args.
*/
void bs_tree_postorder_traverse(BSTree *tree,
BSTreeTraverseFunc callback,
void *cb_args);
#endif /* #ifndef RETHINK_C_BS_TREE_H */