-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathcstl_avl_tree_aux.c
552 lines (468 loc) · 17.7 KB
/
cstl_avl_tree_aux.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* The implementation of avl tree auxiliary functions.
* Copyright (C) 2008 - 2012 Wangbo
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author e-mail: activesys.wb@gmail.com
* activesys@sina.com.cn
*/
/** include section **/
#include <cstl/cstl_def.h>
#include <cstl/cstl_alloc.h>
#include <cstl/cstl_types.h>
#include <cstl/citerator.h>
#include <cstl/cstring.h>
#include <cstl/cstl_avl_tree_iterator.h>
#include <cstl/cstl_avl_tree_private.h>
#include <cstl/cstl_avl_tree.h>
#include "cstl_avl_tree_aux.h"
/** local constant declaration and local macro section **/
/** local function prototype section **/
/** exported global variable definition section **/
/** local global variable definition section **/
/** exported function implementation section **/
#ifndef NDEBUG
/**
* Test avl tree is created by _create_avl tree.
*/
bool_t _avl_tree_is_created(const _avl_tree_t* cpt_avl_tree)
{
assert(cpt_avl_tree != NULL);
if (cpt_avl_tree->_t_typeinfo._t_style != _TYPE_C_BUILTIN &&
cpt_avl_tree->_t_typeinfo._t_style != _TYPE_CSTL_BUILTIN &&
cpt_avl_tree->_t_typeinfo._t_style != _TYPE_USER_DEFINE) {
return false;
}
if (cpt_avl_tree->_t_typeinfo._pt_type == NULL) {
return false;
}
if (cpt_avl_tree->_t_avlroot._pt_parent != NULL || cpt_avl_tree->_t_avlroot._pt_left != NULL ||
cpt_avl_tree->_t_avlroot._pt_right != NULL || cpt_avl_tree->_t_avlroot._un_height != 0 ||
cpt_avl_tree->_t_nodecount != 0 || cpt_avl_tree->_t_compare != NULL) {
return false;
}
return _alloc_is_inited(&cpt_avl_tree->_t_allocator);
}
/**
* Test avl tree is initialized by avl tree initialization functions.
*/
bool_t _avl_tree_is_inited(const _avl_tree_t* cpt_avl_tree)
{
assert(cpt_avl_tree != NULL);
if (cpt_avl_tree->_t_typeinfo._t_style != _TYPE_C_BUILTIN &&
cpt_avl_tree->_t_typeinfo._t_style != _TYPE_CSTL_BUILTIN &&
cpt_avl_tree->_t_typeinfo._t_style != _TYPE_USER_DEFINE) {
return false;
}
if (cpt_avl_tree->_t_typeinfo._pt_type == NULL) {
return false;
}
if (cpt_avl_tree->_t_avlroot._pt_left == NULL || cpt_avl_tree->_t_avlroot._pt_right == NULL ||
cpt_avl_tree->_t_avlroot._un_height != 0 || cpt_avl_tree->_t_compare == NULL) {
return false;
}
return true;
}
/**
* Test iterator referenced data is within the avl tree.
*/
bool_t _avl_tree_iterator_belong_to_avl_tree(const _avl_tree_t* cpt_avl_tree, _avl_tree_iterator_t it_iter)
{
assert(cpt_avl_tree != NULL);
assert(_avl_tree_is_inited(cpt_avl_tree));
assert(_AVL_TREE_ITERATOR_COREPOS(it_iter) != NULL);
assert(_AVL_TREE_ITERATOR_TREE(it_iter) == cpt_avl_tree);
/* if iterator is end */
if (_AVL_TREE_ITERATOR_COREPOS(it_iter) == (_byte_t*)&cpt_avl_tree->_t_avlroot) {
return true;
} else {
/* travel avl tree for search the pointer */
return _avl_tree_avlnode_belong_to_avl_tree(
cpt_avl_tree->_t_avlroot._pt_parent, (_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_iter));
}
}
/**
* Test the type that saved in the avl tree container and referenced by it_iter are same.
*/
bool_t _avl_tree_same_avl_tree_iterator_type(const _avl_tree_t* cpt_avl_tree, _avl_tree_iterator_t it_iter)
{
assert(cpt_avl_tree != NULL);
assert( _AVL_TREE_ITERATOR_TREE(it_iter) != NULL);
return _avl_tree_same_type(cpt_avl_tree, _AVL_TREE_ITERATOR_TREE(it_iter));
}
/**
* Test the type that saved in the avl tree container and referenced by it_iter are same.
*/
bool_t _avl_tree_same_iterator_type(const _avl_tree_t* cpt_avl_tree, iterator_t it_iter)
{
assert(cpt_avl_tree != NULL);
assert(_avl_tree_is_inited(cpt_avl_tree) || _avl_tree_is_created(cpt_avl_tree));
assert(_iterator_is_valid(it_iter));
return _type_is_same_ex(&cpt_avl_tree->_t_typeinfo, _iterator_get_typeinfo(it_iter));
}
/**
* Test the type and compare function that saved in the avl tree container and referenced by it_iter are same.
*/
bool_t _avl_tree_same_avl_tree_iterator_type_ex(const _avl_tree_t* cpt_avl_tree, _avl_tree_iterator_t it_iter)
{
assert(cpt_avl_tree != NULL);
assert( _AVL_TREE_ITERATOR_TREE(it_iter) != NULL);
return _avl_tree_same_type_ex(cpt_avl_tree, _AVL_TREE_ITERATOR_TREE(it_iter));
}
/**
* Test avl node is within the sub avl tree.
*/
bool_t _avl_tree_avlnode_belong_to_avl_tree(const _avlnode_t* cpt_root, const _avlnode_t* cpt_pos)
{
if (cpt_root == NULL || cpt_pos == NULL) {
return false;
} else if (cpt_root == cpt_pos) {
return true;
} else {
return _avl_tree_avlnode_belong_to_avl_tree(cpt_root->_pt_left, cpt_pos) ||
_avl_tree_avlnode_belong_to_avl_tree(cpt_root->_pt_right, cpt_pos);
}
}
#endif /* NDEBUG */
/**
* Test the type that saved in the avl tree container is same.
*/
bool_t _avl_tree_same_type(const _avl_tree_t* cpt_first, const _avl_tree_t* cpt_second)
{
assert(cpt_first != NULL);
assert(cpt_second != NULL);
assert(_avl_tree_is_inited(cpt_first) || _avl_tree_is_created(cpt_first));
assert(_avl_tree_is_inited(cpt_second) || _avl_tree_is_created(cpt_second));
if (cpt_first == cpt_second) {
return true;
}
return (cpt_first->_t_typeinfo._t_style == cpt_second->_t_typeinfo._t_style) &&
(cpt_first->_t_typeinfo._pt_type == cpt_second->_t_typeinfo._pt_type) &&
_type_is_same(_GET_AVL_TREE_TYPE_NAME(cpt_first), _GET_AVL_TREE_TYPE_NAME(cpt_second));
}
/**
* Test the type and compare function that saved in the avl tree container is same.
*/
bool_t _avl_tree_same_type_ex(const _avl_tree_t* cpt_first, const _avl_tree_t* cpt_second)
{
assert(cpt_first != NULL);
assert(cpt_second != NULL);
assert(_avl_tree_is_inited(cpt_first) || _avl_tree_is_created(cpt_first));
assert(_avl_tree_is_inited(cpt_second) || _avl_tree_is_created(cpt_second));
if (cpt_first == cpt_second) {
return true;
}
return (cpt_first->_t_compare == cpt_second->_t_compare) && _avl_tree_same_type(cpt_first, cpt_second);
}
/**
* Travel subtree for find the value in preorder.
*/
_avlnode_t* _avl_tree_find_value(const _avl_tree_t* cpt_avl_tree, const _avlnode_t* cpt_root, const void* cpv_value)
{
bool_t b_result = false;
assert(cpt_avl_tree != NULL);
assert(cpv_value != NULL);
assert(_avl_tree_is_inited(cpt_avl_tree));
if (cpt_root == NULL) {
return NULL;
}
b_result = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
_avl_tree_elem_compare_auxiliary(cpt_avl_tree, cpv_value, cpt_root->_pby_data, &b_result);
if (b_result) {
return _avl_tree_find_value(cpt_avl_tree, cpt_root->_pt_left, cpv_value);
}
b_result = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
_avl_tree_elem_compare_auxiliary(cpt_avl_tree, cpt_root->_pby_data, cpv_value, &b_result);
if (b_result) {
return _avl_tree_find_value(cpt_avl_tree, cpt_root->_pt_right, cpv_value);
} else {
return (_avlnode_t*)cpt_root;
}
}
/**
* Destroy the subtree with postorder traverse.
*/
_avlnode_t* _avl_tree_destroy_subtree(_avl_tree_t* pt_avl_tree, _avlnode_t* pt_root)
{
bool_t b_result = false;
assert(pt_avl_tree != NULL);
assert(_avl_tree_is_inited(pt_avl_tree) || _avl_tree_is_created(pt_avl_tree));
if (pt_root != NULL) {
pt_root->_pt_left = _avl_tree_destroy_subtree(pt_avl_tree, pt_root->_pt_left);
pt_root->_pt_right = _avl_tree_destroy_subtree(pt_avl_tree, pt_root->_pt_right);
assert(pt_root->_pt_left == NULL && pt_root->_pt_right == NULL);
b_result = _GET_AVL_TREE_TYPE_SIZE(pt_avl_tree);
_GET_AVL_TREE_TYPE_DESTROY_FUNCTION(pt_avl_tree)(pt_root->_pby_data, &b_result);
assert(b_result);
_alloc_deallocate(&pt_avl_tree->_t_allocator, pt_root,_AVL_TREE_NODE_SIZE(_GET_AVL_TREE_TYPE_SIZE(pt_avl_tree)), 1);
}
return NULL;
}
/**
* ll Rotate.
*/
_avlnode_t* _avl_tree_left_signal_rotate(_avlnode_t* pt_root)
{
/*
* A B
* / / \
* B => C A
* / \ /
* C D D
*/
_avlnode_t* pt_left = NULL;
assert(pt_root != NULL);
assert(pt_root->_pt_left != NULL);
/* rotate */
pt_left = pt_root->_pt_left;
pt_root->_pt_left = pt_left->_pt_right;
pt_left->_pt_right = pt_root;
/* change parent */
pt_root->_pt_parent = pt_left;
if (pt_root->_pt_left != NULL) {
pt_root->_pt_left->_pt_parent = pt_root;
}
/* update height */
pt_root->_un_height = (unsigned int)(
(_avl_tree_get_height(pt_root->_pt_left) > _avl_tree_get_height(pt_root->_pt_right) ?
_avl_tree_get_height(pt_root->_pt_left) : _avl_tree_get_height(pt_root->_pt_right)) + 1);
pt_left->_un_height = (unsigned int)(
(_avl_tree_get_height(pt_left->_pt_left) > _avl_tree_get_height(pt_left->_pt_right) ?
_avl_tree_get_height(pt_left->_pt_left) : _avl_tree_get_height(pt_left->_pt_right)) + 1);
return pt_left;
}
/**
* rr Rotate.
*/
_avlnode_t* _avl_tree_right_signal_rotate(_avlnode_t* pt_root)
{
/*
* A B
* \ / \
* B => A D
* / \ \
* C D C
*/
_avlnode_t* pt_right = NULL;
assert(pt_root != NULL);
assert(pt_root->_pt_right != NULL);
/* rotate */
pt_right = pt_root->_pt_right;
pt_root->_pt_right = pt_right->_pt_left;
pt_right->_pt_left = pt_root;
/* change parent */
pt_root->_pt_parent = pt_right;
if (pt_root->_pt_right != NULL) {
pt_root->_pt_right->_pt_parent = pt_root;
}
/* update height */
pt_root->_un_height = (unsigned int)(
(_avl_tree_get_height(pt_root->_pt_left) > _avl_tree_get_height(pt_root->_pt_right) ?
_avl_tree_get_height(pt_root->_pt_left) : _avl_tree_get_height(pt_root->_pt_right)) + 1);
pt_right->_un_height = (unsigned int)(
(_avl_tree_get_height(pt_right->_pt_left) > _avl_tree_get_height(pt_right->_pt_right) ?
_avl_tree_get_height(pt_right->_pt_left) : _avl_tree_get_height(pt_right->_pt_right)) + 1);
return pt_right;
}
/**
* lr Rotate.
*/
_avlnode_t* _avl_tree_left_double_rotate(_avlnode_t* pt_root)
{
/*
* A A E
* / \ / \ / \
* B C E C B A
* / \ => / \ => / \ / \
* D E B G D F G C
* / \ / \
* F G D F
*/
assert(pt_root != NULL);
pt_root->_pt_left = _avl_tree_right_signal_rotate(pt_root->_pt_left);
pt_root->_pt_left->_pt_parent = pt_root;
return _avl_tree_left_signal_rotate(pt_root);
}
/**
* rl Rotate.
*/
_avlnode_t* _avl_tree_right_double_rotate(_avlnode_t* pt_root)
{
/*
* A A D
* / \ / \ / \
* B C B D A C
* / \ => / \ => / \ / \
* D E F C B F G E
* / \ / \
* F G G E
*/
assert(pt_root != NULL);
pt_root->_pt_right = _avl_tree_left_signal_rotate(pt_root->_pt_right);
pt_root->_pt_right->_pt_parent = pt_root;
return _avl_tree_right_signal_rotate(pt_root);
}
/**
* Get avl node height.
*/
int _avl_tree_get_height(const _avlnode_t* cpt_root)
{
return cpt_root == NULL ? -1 : (int)cpt_root->_un_height;
}
/**
* Get minimum avlnode.
*/
_avlnode_t* _avl_tree_get_min_avlnode(const _avlnode_t* cpt_root)
{
_avlnode_t* pt_min = (_avlnode_t*)cpt_root;
assert(cpt_root != NULL);
while (pt_min->_pt_left != NULL) {
pt_min = pt_min->_pt_left;
}
return pt_min;
}
/**
* Get maximum avlnode.
*/
_avlnode_t* _avl_tree_get_max_avlnode(const _avlnode_t* cpt_root)
{
_avlnode_t* pt_max = (_avlnode_t*)cpt_root;
assert(cpt_root != NULL);
while (pt_max->_pt_right != NULL) {
pt_max = pt_max->_pt_right;
}
return pt_max;
}
/**
* Insert the value into subtree.
*/
_avl_tree_insert_result_t _avl_tree_insert_avlnode(
const _avl_tree_t* cpt_avl_tree, _avlnode_t* pt_root, const void* cpv_value)
{
_avl_tree_insert_result_t t_insert_result;
bool_t b_result = false;
assert(cpt_avl_tree != NULL);
assert(cpv_value != NULL);
assert(_avl_tree_is_inited(cpt_avl_tree));
/* if root is NULL then allocate memory */
if (pt_root == NULL) {
pt_root = _alloc_allocate(
(_alloc_t*)&cpt_avl_tree->_t_allocator, _AVL_TREE_NODE_SIZE(_GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree)), 1);
assert(pt_root != NULL);
_avl_tree_init_elem_auxiliary((_avl_tree_t*)cpt_avl_tree, pt_root);
pt_root->_pt_left = pt_root->_pt_right = NULL;
pt_root->_un_height = 0;
t_insert_result._pt_adjust = pt_root;
t_insert_result._pt_new = pt_root;
b_result = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
_GET_AVL_TREE_TYPE_COPY_FUNCTION(cpt_avl_tree)(pt_root->_pby_data, cpv_value, &b_result);
assert(b_result);
return t_insert_result;
}
/* compare the value and current node */
/* if value < current node then insert into left subtree */
b_result = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
_avl_tree_elem_compare_auxiliary(cpt_avl_tree, cpv_value, pt_root->_pby_data, &b_result);
if (b_result) {
t_insert_result = _avl_tree_insert_avlnode(cpt_avl_tree, pt_root->_pt_left, cpv_value);
pt_root->_pt_left = t_insert_result._pt_adjust;
pt_root->_pt_left->_pt_parent = pt_root;
pt_root = _avl_tree_rebalance(pt_root);
t_insert_result._pt_adjust = pt_root;
return t_insert_result;
} else {
/* insert into right subtree */
t_insert_result = _avl_tree_insert_avlnode(cpt_avl_tree, pt_root->_pt_right, cpv_value);
pt_root->_pt_right = t_insert_result._pt_adjust;
pt_root->_pt_right->_pt_parent = pt_root;
pt_root = _avl_tree_rebalance(pt_root);
t_insert_result._pt_adjust = pt_root;
return t_insert_result;
}
}
/**
* Rebalance the subtree and update the root height.
*/
_avlnode_t* _avl_tree_rebalance(_avlnode_t* pt_root)
{
if (pt_root != NULL) {
/* if left - right > 1 */
if (_avl_tree_get_height(pt_root->_pt_left) - _avl_tree_get_height(pt_root->_pt_right) > 1) {
/* ll */
if (_avl_tree_get_height(pt_root->_pt_left->_pt_left) >=
_avl_tree_get_height(pt_root->_pt_left->_pt_right)) {
pt_root = _avl_tree_left_signal_rotate(pt_root);
} else {
/* lr */
pt_root = _avl_tree_left_double_rotate(pt_root);
}
} else if (_avl_tree_get_height(pt_root->_pt_right) - _avl_tree_get_height(pt_root->_pt_left) > 1) {
/* else if right - left > 1 */
/* rr */
if (_avl_tree_get_height(pt_root->_pt_right->_pt_right) >=
_avl_tree_get_height(pt_root->_pt_right->_pt_left)) {
pt_root = _avl_tree_right_signal_rotate(pt_root);
} else {
/* rl */
pt_root = _avl_tree_right_double_rotate(pt_root);
}
}
pt_root->_un_height = (unsigned int)(
(_avl_tree_get_height(pt_root->_pt_left) > _avl_tree_get_height(pt_root->_pt_right) ?
_avl_tree_get_height(pt_root->_pt_left) : _avl_tree_get_height(pt_root->_pt_right)) + 1);
}
return pt_root;
}
/**
* Initialize element auxiliary function
*/
void _avl_tree_init_elem_auxiliary(_avl_tree_t* pt_avl_tree, _avlnode_t* pt_node)
{
assert(pt_avl_tree != NULL);
assert(pt_node != NULL);
assert(_avl_tree_is_inited(pt_avl_tree) || _avl_tree_is_created(pt_avl_tree));
/* initialize new elements */
if (_GET_AVL_TREE_TYPE_STYLE(pt_avl_tree) == _TYPE_CSTL_BUILTIN) {
/* get element type name */
char s_elemtypename[_TYPE_NAME_SIZE + 1];
_type_get_elem_typename(_GET_AVL_TREE_TYPE_NAME(pt_avl_tree), s_elemtypename);
_GET_AVL_TREE_TYPE_INIT_FUNCTION(pt_avl_tree)(pt_node->_pby_data, s_elemtypename);
} else {
bool_t b_result = _GET_AVL_TREE_TYPE_SIZE(pt_avl_tree);
_GET_AVL_TREE_TYPE_INIT_FUNCTION(pt_avl_tree)(pt_node->_pby_data, &b_result);
assert(b_result);
}
}
/**
* Element compare function auxiliary
*/
void _avl_tree_elem_compare_auxiliary(
const _avl_tree_t* cpt_avl_tree, const void* cpv_first, const void* cpv_second, void* pv_output)
{
assert(cpt_avl_tree != NULL);
assert(cpv_first != NULL);
assert(cpv_second != NULL);
assert(pv_output != NULL);
assert(_avl_tree_is_inited(cpt_avl_tree));
if (strncmp(_GET_AVL_TREE_TYPE_BASENAME(cpt_avl_tree), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0 &&
cpt_avl_tree->_t_compare != _GET_AVL_TREE_TYPE_LESS_FUNCTION(cpt_avl_tree)) {
cpt_avl_tree->_t_compare(string_c_str((string_t*)cpv_first), string_c_str((string_t*)cpv_second), pv_output);
} else {
cpt_avl_tree->_t_compare(cpv_first, cpv_second, pv_output);
}
}
/** local function implementation section **/
/** eof **/