-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbitree.c
304 lines (269 loc) · 6.8 KB
/
bitree.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* @filename: bitree.c
*
* @author: QinYUN575
*
* @create: 2019/12/01
*
*
*/
#include "bitree.h"
#include <stdlib.h>
#include <string.h>
/**
* @brief initialize binary tree.
*
* @attention None
*
* @param[in] tree pointer to initialize of the binary tree
* @param[in] destroy pointer to the destroy handler of the bitree
*
* @return None
*
*/
void bitree_init(BiTree *tree, void (*destroy)(void *data))
{
/* Initalize the binary tree. */
tree->size = 0;
tree->destroy = destroy;
tree->root = NULL;
return;
}
/**
* @brief destroy binary tree.
*
* @attention None
*
* @param[in] tree pointer to destroy of the binary tree
*
* @return None
*/
void bitree_destroy(BiTree *tree)
{
/* Remove all the nodes from the tree. */
bitree_rem_left(tree, NULL);
/* No operations are allowed now, but clear the structure as a precaution. */
memset(tree, 0, sizeof(BiTree));
}
/**
* @brief insert node into binary tree as left node
*
* @attention None
*
* @param[in] tree pointer to left to the binary tree
* @param[in] node pointer to left node
* @param[in] data pointer to data as left node data
*
* @return errcode
* @retval 0, insertion successful
* -1, insertion fail
*
*/
int bitree_ins_left(BiTree *tree, BiTreeNode *node, const void *data)
{
BiTreeNode *new_node, **position;
/* Determine where to insert the node. */
if (node == NULL)
{
/* Allow insertion at the root only in an empty tree. */
if (bitree_size(tree) > 0)
{
return -1;
}
position = &tree->root;
}
else
{
/* Normally allow insertion only at the end of a branch. */
if (bitree_left(node) != NULL)
{
return -1;
}
position = &node->left;
}
/* Allocate storage for the node. */
if ((new_node = (BiTreeNode *)malloc(sizeof(BiTreeNode))) == NULL)
{
return -1;
}
/* Insert the node into the tree. */
new_node->data = (void *)data;
new_node->left = NULL;
new_node->right = NULL;
*position = new_node;
/* Adjust the size of the tree to account for the inserted node. */
tree->size++;
return 0;
}
/**
* @brief insert node into binary tree as right node
*
* @attention None
*
* @param[in] tree pointer to right to the binary tree
* @param[in] node pointer to right node
* @param[in] data pointer to data as right node data
*
* @return None
*
* @return errcode
* @retval 0, insertion successful
* -1, insertion fail
*
*/
void bitree_ins_right(BiTree *tree, BiTreeNode *node, const void *data)
{
BiTreeNode *new_node, **position;
/* Determine where to insert the node. */
if (node == NULL)
{
/* Allow insertion at the root only in an empty tree. */
if (bitree_size(tree) > 0)
{
return -1;
}
position = &tree->root;
}
else
{
if (bitree_right(node) != NULL)
{
return -1;
}
position = &node->right;
}
if ((new_node = (BiTreeNode *)malloc(sizeof(BiTreeNode))) == NULL)
{
return -1;
}
/* Insert the node into the tree. */
new_node->data = (void *)data;
new_node->left = NULL;
new_node->right = NULL;
*position = new_node;
/* Adujst the size of the tree to account for the inserted node. */
tree->size++;
return 0;
}
/**
* @brief remove left for binray tree
*
* @attention None
*
* @param[in] tree pointer to right to the binary node
* @param[in] node pointer to righr node
*
* @return None
*
*/
void bitree_rem_left(BiTree *tree, BiTreeNode *node)
{
BiTreeNode **position;
/* Do not allow removal from an empty tree. */
if (bitree_size(tree) == 0)
{
return;
}
/* Determine where to remove nodes. */
if (node == NULL)
{
position = &tree->root;
}
else
{
position = &node->left;
}
/* Remove the nodes. */
if (*position != NULL)
{
bitree_rem_left(tree, node);
bitree_rem_right(tree, node);
if (tree->destroy != NULL)
{
/* Call a user-define function to free dynamically allocated data. */
tree->destroy((*position)->data);
}
free(*position);
*position = NULL;
/* Adjust the size of the tree to account for the removed node. */
tree->size--;
}
return;
}
/**
* @brief remove node for binary tree
*
* @param[in] tree pointer to right to the binary tree
* @param[in] node pointer to right node
*
* @return None
*
*/
void bitree_rem_right(BiTree *tree, BiTreeNode *node)
{
BiTreeNode **position;
/* Do not allow remove form an empty tree. */
if (bitree_size(tree) == 0)
{
return;
}
/* Determine where to remove nodes. */
if (node == NULL)
{
position = &tree->root;
}
else
{
position = &node->right;
}
if (*position != NULL)
{
bitree_rem_left(tree, *position);
bitree_rem_right(tree, *position);
if (tree->destroy != NULL)
{
/* Call a user-defined function to free dynamically allocated data. */
tree->destroy((*position)->data);
}
free(*position);
*position = NULL;
/* Adjust the size of the tree to account for the removed node. */
tree->size--;
}
return;
}
/**
* @brief merge left tree and right tree to new tree
*
* @param[in] tree pointer with new binary tree
* @param[in] tree pointer of left tree
* @param[in] tree pointer of tight tree
*
* @return result code
*
* @retval 0, merge successful
* -1, merge fail
*
*/
int bitree_merge(BiTree *merge, BiTree *left, BiTree *right, const void *data)
{
/* Initialize the merge tree. */
bitree_init(merge, left->destroy);
/* Insert the data for the root node of the merged tree. */
if (bitree_ins_left(merge, NULL, data) != 0)
{
bitree_destroy(merge);
return -1;
}
/* Merge the two binary trees into a single binary tree. */
bitree_root(merge)->left = bitree_root(left);
bitree_root(merge)->right = bitree_root(right);
/* Adjust the size of the new binary tree. */
merge->size = merge->size + bitree_size(left) + bitree_size(right);
/* Do not let the original trees access the merged nodes. */
left->root = NULL;
left->size = 0;
right->root = NULL;
right->size = 0;
return 0;
}