-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathavltree.h
215 lines (192 loc) · 6.09 KB
/
avltree.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* @file avltree.h
*
* @author hutusi ([email protected])
*
* @brief AVL Tree.
*
* AVL tree is a self balancing binary search tree, where difference of right
* subtree and left subtree height to a node is at most 1.
*
* Georgy Adelson-Velsky and Evgenii Landis introduced it in 1962.
*
* @date 2019-07-30
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#ifndef RETHINK_C_AVL_TREE_H
#define RETHINK_C_AVL_TREE_H
/**
* @brief The type of a key to be stored in a @ref AVLTree.
* (void *) can be changed to int, char *, or other types if needed.
*/
typedef void *AVLTreeKey;
/**
* @brief The type of a key to be stored in a @ref AVLTree.
* (void *) can be changed to int, char *, or other types if needed.
*/
typedef void *AVLTreeValue;
/**
* @brief Definition of a @ref AVLTreeEntity.
*
*/
typedef struct _AVLTreeEntity {
AVLTreeValue value;
struct _AVLTreeEntity *next;
} AVLTreeEntity;
/**
* @brief Definition of a @ref AVLTreeNode.
*
*/
typedef struct _AVLTreeNode {
/** Key of the node. */
AVLTreeKey key;
/** Values of the node. */
AVLTreeEntity *data;
/** Parent node pointer. */
struct _AVLTreeNode *parent;
/** Left child node pointer. */
struct _AVLTreeNode *left;
/** Right child node pointer. */
struct _AVLTreeNode *right;
/** Node's height in the AVLTree. */
unsigned int height;
} AVLTreeNode;
typedef int (*AVLTreeCompareFunc)(AVLTreeValue data1, AVLTreeValue data2);
typedef void (*AVLTreeFreeKeyFunc)(AVLTreeKey key);
typedef void (*AVLTreeFreeValueFunc)(AVLTreeValue value);
/**
* @brief Definition of a @ref AVLTree.
*
*/
typedef struct _AVLTree {
/** Root node of the @ref AVLTree pointer. */
struct _AVLTreeNode *root;
/** Compare two node value when do searching in AVLTree. */
AVLTreeCompareFunc compare_func;
AVLTreeFreeKeyFunc free_key_func;
AVLTreeFreeValueFunc free_value_func;
/** The number of nodes of the @ref AVLTree. */
unsigned int num_nodes;
} AVLTree;
/**
* @brief Allcate a new AVLTree.
*
* @param compare_func Compare two node value when do searching in AVLTree.
* @param free_key_func Free key callback function.
* @param free_value_func Free value callback function.
* @return AVLTree* The new AVLTree if success, otherwise return NULL.
*/
AVLTree *avl_tree_new(AVLTreeCompareFunc compare_func,
AVLTreeFreeKeyFunc free_key_func,
AVLTreeFreeValueFunc free_value_func);
/**
* @brief Delete a AVLTree and free back memory.
*
* @param tree The AVLTree to delete.
*/
void avl_tree_free(AVLTree *tree);
/**
* @brief Free a AVLTreeNode in a AVLTree.
*
* @param tree The AVLTree
* @param node The AVLTreeNode.
*/
void avl_tree_free_node(AVLTree *tree, AVLTreeNode *node);
/**
* @brief Get the leftmost child node of a AVLTreeNode.
*
* @param node The AVLTreeNode.
* @return AVLTreeNode* The leftmost AVLTreeNode. If the AVLTreeNode has
* no left child, return itself.
*/
AVLTreeNode *avl_tree_leftmost_node(AVLTreeNode *node);
/**
* @brief Get the rightmost child node of a AVLTreeNode.
*
* @param node The AVLTreeNode.
* @return AVLTreeNode* The rightmost AVLTreeNode. If the AVLTreeNode has
* no right child, return itself.
*/
AVLTreeNode *avl_tree_rightmost_node(AVLTreeNode *node);
/**
* @brief Insert a AVLTreeValue to a AVLTree.
*
* @param tree The AVLTree.
* @param key The key to insert.
* @param value The value to insert.
* @return AVLTreeNode* The new inserted AVLTreeNode if success,
* otherwise return NULL.
*/
AVLTreeNode *avl_tree_insert(AVLTree *tree, AVLTreeKey key, AVLTreeValue value);
/**
* @brief Remove a AVLTreeNode from a AVLTree.
*
* @param tree The AVLTree.
* @param node The AVLTreeNode.
* @return AVLTreeNode* The removed AVLTreeNode if success,
* otherwise return NULL.
*/
AVLTreeNode *avl_tree_remove_node(AVLTree *tree, AVLTreeNode *node);
/**
* @brief Find a AVLTreeNode value in a AVLTree.
*
* @param tree The AVLTree.
* @param key The AVLTreeNode value to lookup.
* @return AVLTreeNode* The matched AVLTreeNode if success, otherwise NULL.
*/
AVLTreeNode *avl_tree_find_node(AVLTree *tree, AVLTreeKey key);
/**
* @brief Traverse AVLTree callback function.
*
*/
typedef void (*AVLTreeTraverseFunc)(AVLTreeNode *node, void *args);
/**
* @brief Traverse AVLTree by preorder.
*
* @param tree The AVLTree.
* @param callback The callback function do to each AVLTreeNode in
* traverse.
* @param cb_args The callback function's args.
*/
void avl_tree_preorder_traverse(AVLTree *tree,
AVLTreeTraverseFunc callback,
void *cb_args);
/**
* @brief Traverse AVLTree by inorder.
*
* @param tree The AVLTree.
* @param callback The callback function do to each AVLTreeNode in
* traverse.
* @param cb_args The callback function's args.
*/
void avl_tree_inorder_traverse(AVLTree *tree,
AVLTreeTraverseFunc callback,
void *cb_args);
/**
* @brief Traverse AVLTree by postorder.
*
* @param tree The AVLTree.
* @param callback The callback function do to each AVLTreeNode in
* traverse.
* @param cb_args The callback function's args.
*/
void avl_tree_postorder_traverse(AVLTree *tree,
AVLTreeTraverseFunc callback,
void *cb_args);
/**
* @brief A subtree's height in a AVLTree.
*
* @param node The subtree's root node.
* @return unsigned int The height.
*/
unsigned int avl_tree_subtree_height(const AVLTreeNode *node);
/**
* @brief Print a subtree.
*
* @param node The subtree's root node.
* @param depth The subtree's depth.
*/
void avl_tree_subtree_print(const AVLTreeNode *node, int depth);
#endif /* #ifndef RETHINK_C_AVL_TREE_H */