-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathbistree.c
508 lines (344 loc) · 10.8 KB
/
bistree.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
//
// bistree.c
// Algorithms - binary search tree AVL树(Adel'son-Vel'skii and Landis)
//
// Created by YourtionGuo on 04/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <stdlib.h>
#include <string.h>
#include "bistree.h"
#pragma mark - Private
static void destroy_right(BisTree *tree, BiTreeNode *node);
/**
执行左旋转
@param node 二叉树结点
*/
static void rotate_left(BiTreeNode **node)
{
BiTreeNode *left, *grandchild;
left = bitree_left(*node);
if (((AvlNode *)bitree_data(left))->factor == AVL_LFT_HEAVY) {
/// 执行 LL ( Left-Left ) 旋转操作
bitree_left(*node) = bitree_right(left);
bitree_right(left) = *node;
((AvlNode *)bitree_data(*node))->factor = AVL_BALANCED;
((AvlNode *)bitree_data(left))->factor = AVL_BALANCED;
*node = left;
} else {
/// 执行 LR ( Left-Right ) 旋转操作
grandchild = bitree_right(left);
bitree_right(left) = bitree_left(grandchild);
bitree_left(grandchild) = left;
bitree_left(*node) = bitree_right(grandchild);
bitree_right(grandchild) = *node;
switch (((AvlNode *)bitree_data(grandchild))->factor) {
case AVL_LFT_HEAVY:
((AvlNode *)bitree_data(*node))->factor = AVL_RGT_HEAVY;
((AvlNode *)bitree_data(left))->factor = AVL_BALANCED;
break;
case AVL_BALANCED:
((AvlNode *)bitree_data(*node))->factor = AVL_BALANCED;
((AvlNode *)bitree_data(left))->factor = AVL_BALANCED;
break;
case AVL_RGT_HEAVY:
((AvlNode *)bitree_data(*node))->factor = AVL_BALANCED;
((AvlNode *)bitree_data(left))->factor = AVL_LFT_HEAVY;
break;
}
((AvlNode *)bitree_data(grandchild))->factor = AVL_BALANCED;
*node = grandchild;
}
return;
}
/**
执行右旋转
@param node 二叉树结点
*/
static void rotate_right(BiTreeNode **node)
{
BiTreeNode *right, *grandchild;
right = bitree_right(*node);
if (((AvlNode *)bitree_data(right))->factor == AVL_RGT_HEAVY) {
/// 执行 RR ( Right-Right ) 旋转操作
bitree_right(*node) = bitree_left(right);
bitree_left(right) = *node;
((AvlNode *)bitree_data(*node))->factor = AVL_BALANCED;
((AvlNode *)bitree_data(right))->factor = AVL_BALANCED;
*node = right;
} else {
/// 执行 RR ( Right-Right ) 旋转操作
grandchild = bitree_left(right);
bitree_left(right) = bitree_right(grandchild);
bitree_right(grandchild) = right;
bitree_right(*node) = bitree_left(grandchild);
bitree_left(grandchild) = *node;
switch (((AvlNode *)bitree_data(grandchild))->factor) {
case AVL_LFT_HEAVY:
((AvlNode *)bitree_data(*node))->factor = AVL_BALANCED;
((AvlNode *)bitree_data(right))->factor = AVL_RGT_HEAVY;
break;
case AVL_BALANCED:
((AvlNode *)bitree_data(*node))->factor = AVL_BALANCED;
((AvlNode *)bitree_data(right))->factor = AVL_BALANCED;
break;
case AVL_RGT_HEAVY:
((AvlNode *)bitree_data(*node))->factor = AVL_LFT_HEAVY;
((AvlNode *)bitree_data(right))->factor = AVL_BALANCED;
break;
}
((AvlNode *)bitree_data(grandchild))->factor = AVL_BALANCED;
*node = grandchild;
}
return;
}
/**
移除由 tree 指定二叉树中 node 的左子结点为根的子树
@param tree 搜索二叉树
@param node 指定结点
*/
static void destroy_left(BisTree *tree, BiTreeNode *node)
{
BiTreeNode **position;
/// 不允许在空树中执行移除
if (bitree_size(tree) == 0) return;
/// 确定移除结点的位置
if (node == NULL) {
position = &tree->root;
} else {
position = &node->left;
}
/// 移除结点
if (*position != NULL) {
destroy_left(tree, *position);
destroy_right(tree, *position);
if (tree->destroy != NULL) {
/// 执行用户指定的 destroy 函数
tree->destroy(((AvlNode *)(*position)->data)->data);
}
/// 清理 AVL 数据结构和结点数据
free((*position)->data);
free(*position);
*position = NULL;
/// 更新树的 size
tree->size--;
}
return;
}
/**
移除由 tree 指定二叉树中 node 的右子结点为根的子树
@param tree 搜索二叉树
@param node 指定结点
*/
static void destroy_right(BisTree *tree, BiTreeNode *node)
{
BiTreeNode **position;
/// 不允许在空树中执行移除
if (bitree_size(tree) == 0) return;
/// 确定移除结点的位置
if (node == NULL) {
position = &tree->root;
} else {
position = &node->right;
}
/// 移除结点
if (*position != NULL) {
destroy_left(tree, *position);
destroy_right(tree, *position);
if (tree->destroy != NULL) {
/// 执行用户指定的 destroy 函数
tree->destroy(((AvlNode *)(*position)->data)->data);
}
/// 清理 AVL 数据结构和结点数据
free((*position)->data);
free(*position);
*position = NULL;
/// 更新树的 size
tree->size--;
}
return;
}
/**
在 tree 所指定二叉树中插入一个 node 所指定结点
@param tree 二叉搜索树
@param node 指定的结点
@param data 结点数据
@param balanced 是否已经平衡(已平衡为 1,未平衡为 0)
@return 插入成功,返回0;已存在,返回1;否则返回-1
*/
static int insert(BisTree *tree, BiTreeNode **node, const void *data, int *balanced)
{
AvlNode *avl_data;
int cmpval, retval;
/// 将数据插入到树中
if (bitree_is_eob(*node)) {
/// 处理插入空树的情况
if ((avl_data = (AvlNode *)malloc(sizeof(AvlNode))) == NULL) return -1;
avl_data->factor = AVL_BALANCED;
avl_data->hidden = 0;
avl_data->data = (void *)data;
return bitree_ins_left(tree, *node, avl_data);
} else {
/// 处理非空树情况
cmpval = tree->compare(data, ((AvlNode *)bitree_data(*node))->data);
if (cmpval < 0) {
/// 处理左子树
if (bitree_is_eob(bitree_left(*node))) {
if ((avl_data = (AvlNode *)malloc(sizeof(AvlNode))) == NULL) return -1;
avl_data->factor = AVL_BALANCED;
avl_data->hidden = 0;
avl_data->data = (void *)data;
if (bitree_ins_left(tree, *node, avl_data) != 0) return -1;
*balanced = 0;
} else {
if ((retval = insert(tree, &bitree_left(*node), data, balanced)) != 0) return retval;
}
/// 确保树依然保持平衡
if (!(*balanced)) {
switch (((AvlNode *)bitree_data(*node))->factor) {
case AVL_LFT_HEAVY:
rotate_left(node);
*balanced = 1;
break;
case AVL_BALANCED:
((AvlNode *)bitree_data(*node))->factor = AVL_LFT_HEAVY;
break;
case AVL_RGT_HEAVY:
((AvlNode *)bitree_data(*node))->factor = AVL_BALANCED;
*balanced = 1;
}
}
} else if (cmpval > 0) {
/// 处理右子树
if (bitree_is_eob(bitree_right(*node))) {
if ((avl_data = (AvlNode *)malloc(sizeof(AvlNode))) == NULL)
return -1;
avl_data->factor = AVL_BALANCED;
avl_data->hidden = 0;
avl_data->data = (void *)data;
if (bitree_ins_right(tree, *node, avl_data) != 0)
return -1;
*balanced = 0;
} else {
if ((retval = insert(tree, &bitree_right(*node), data, balanced)) != 0) return retval;
}
/// 确保树依然保持平衡
if (!(*balanced)) {
switch (((AvlNode *)bitree_data(*node))->factor) {
case AVL_LFT_HEAVY:
((AvlNode *)bitree_data(*node))->factor = AVL_BALANCED;
*balanced = 1;
break;
case AVL_BALANCED:
((AvlNode *)bitree_data(*node))->factor = AVL_RGT_HEAVY;
break;
case AVL_RGT_HEAVY:
rotate_right(node);
*balanced = 1;
}
}
} else {
/// 处理找到节点的情况
///如果找到非隐藏节点直接返回 1
if (!((AvlNode *)bitree_data(*node))->hidden) return 1;
///插入新数据并取消隐藏
if (tree->destroy != NULL) {
/// 销毁隐藏节点的数据
tree->destroy(((AvlNode *)bitree_data(*node))->data);
}
((AvlNode *)bitree_data(*node))->data = (void *)data;
((AvlNode *)bitree_data(*node))->hidden = 0;
///如果是隐藏节点数据替换,则不需要再次平衡树
*balanced = 1;
}
}
return 0;
}
/**
在 tree 所指定二叉树中隐藏一个 node 所指定结点
@param tree 二叉搜索树
@param node 指定的结点
@param data 结点数据
@return 成功返回0;否则返回-1
*/
static int hide(BisTree *tree, BiTreeNode *node, const void *data)
{
int cmpval, retval;
/// 没找到节点返回 -1
if (bitree_is_eob(node)) return -1;
cmpval = tree->compare(data, ((AvlNode *)bitree_data(node))->data);
if (cmpval < 0) {
/// 处理左子树
retval = hide(tree, bitree_left(node), data);
} else if (cmpval > 0) {
/// 处理右子树
retval = hide(tree, bitree_right(node), data);
} else {
/// 将节点标记为隐藏
((AvlNode *)bitree_data(node))->hidden = 1;
retval = 0;
}
return retval;
}
/**
在 tree 所指定二叉搜索树中查找 node 指定的节点
@param tree 二叉搜索树
@param node 指定的结点
@param data 找到的结点数据
@return 找到返回0;否则返回-1
*/
static int lookup(BisTree *tree, BiTreeNode *node, void **data)
{
int cmpval, retval;
/// 没找到节点返回 -1
if (bitree_is_eob(node)) return -1;
cmpval = tree->compare(*data, ((AvlNode *)bitree_data(node))->data);
if (cmpval < 0) {
/// 处理左子树
retval = lookup(tree, bitree_left(node), data);
} else if (cmpval > 0) {
/// 处理右子树
retval = lookup(tree, bitree_right(node), data);
} else {
if (!((AvlNode *)bitree_data(node))->hidden) {
/// 将找到的节点数据返回
*data = ((AvlNode *)bitree_data(node))->data;
retval = 0;
} else {
/// 没找到节点返回 -1
return -1;
}
}
return retval;
}
#pragma mark - Public
void bistree_init(BisTree *tree,
int (*compare)(const void *key1, const void *key2),
void (*destroy)(void *data))
{
/// 初始化搜索二叉树
bitree_init(tree, destroy);
tree->compare = compare;
return;
}
void bistree_destroy(BisTree *tree)
{
/// 删除所有树的结点
destroy_left(tree, NULL);
/// 清理二叉树数据结构
memset(tree, 0, sizeof(BisTree));
return;
}
int bistree_insert(BisTree *tree, const void *data)
{
int balanced = 0;
return insert(tree, &bitree_root(tree), data, &balanced);
}
int bistree_remove(BisTree *tree, const void *data)
{
return hide(tree, bitree_root(tree), data);
}
int bistree_lookup(BisTree *tree, void **data)
{
return lookup(tree, bitree_root(tree), data);
}