1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
Code for handling red-black (balanced) binary trees.
18
key in tree is allocated accrding to following:
20
1) If size < 0 then tree will not allocate keys and only a pointer to
21
each key is saved in tree.
22
compare and search functions uses and returns key-pointer
24
2) If size == 0 then there are two options:
25
- key_size != 0 to tree_insert: The key will be stored in the tree.
26
- key_size == 0 to tree_insert: A pointer to the key is stored.
27
compare and search functions uses and returns key-pointer.
29
3) if key_size is given to init_tree then each node will continue the
30
key and calls to insert_key may increase length of key.
31
if key_size > sizeof(pointer) and key_size is a multiple of 8 (double
32
allign) then key will be put on a 8 alligned adress. Else
33
the key will be on adress (element+1). This is transparent for user
34
compare and search functions uses a pointer to given key-argument.
36
- If you use a free function for tree-elements and you are freeing
37
the element itself, you should use key_size = 0 to init_tree and
40
The actual key in TREE_ELEMENT is saved as a pointer or after the
42
If one uses only pointers in tree one can use tree_set_pointer() to
43
change address of data.
50
tree->compare function should be ALWAYS called as
51
(*tree->compare)(custom_arg, ELEMENT_KEY(tree,element), key)
52
and not other way around, as
53
(*tree->compare)(custom_arg, key, ELEMENT_KEY(tree,element))
55
ft_boolean_search.c (at least) relies on that.
58
#include "mysys_priv.h"
65
#define DEFAULT_ALLOC_SIZE 8192
66
#define DEFAULT_ALIGN_SIZE 8192
68
static void delete_tree_element(TREE *,TREE_ELEMENT *);
69
static int tree_walk_left_root_right(TREE *,TREE_ELEMENT *,
70
tree_walk_action,void *);
71
static int tree_walk_right_root_left(TREE *,TREE_ELEMENT *,
72
tree_walk_action,void *);
73
static void left_rotate(TREE_ELEMENT **parent,TREE_ELEMENT *leaf);
74
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf);
75
static void rb_insert(TREE *tree,TREE_ELEMENT ***parent,
77
static void rb_delete_fixup(TREE *tree,TREE_ELEMENT ***parent);
80
/* The actuall code for handling binary trees */
83
static int test_rb_tree(TREE_ELEMENT *element);
86
void init_tree(TREE *tree, ulong default_alloc_size, ulong memory_limit,
87
int size, qsort_cmp2 compare, my_bool with_delete,
88
tree_element_free free_element, void *custom_arg)
90
DBUG_ENTER("init_tree");
91
DBUG_PRINT("enter",("tree: 0x%lx size: %d", (long) tree, size));
93
if (default_alloc_size < DEFAULT_ALLOC_SIZE)
94
default_alloc_size= DEFAULT_ALLOC_SIZE;
95
default_alloc_size= MY_ALIGN(default_alloc_size, DEFAULT_ALIGN_SIZE);
96
bzero((uchar*) &tree->null_element,sizeof(tree->null_element));
97
tree->root= &tree->null_element;
98
tree->compare=compare;
99
tree->size_of_element=size > 0 ? (uint) size : 0;
100
tree->memory_limit=memory_limit;
101
tree->free=free_element;
103
tree->elements_in_tree=0;
104
tree->custom_arg = custom_arg;
105
tree->null_element.colour=BLACK;
106
tree->null_element.left=tree->null_element.right=0;
108
if (!free_element && size >= 0 &&
109
((uint) size <= sizeof(void*) || ((uint) size & (sizeof(void*)-1))))
112
We know that the data doesn't have to be aligned (like if the key
113
contains a double), so we can store the data combined with the
116
tree->offset_to_key=sizeof(TREE_ELEMENT); /* Put key after element */
117
/* Fix allocation size so that we don't lose any memory */
118
default_alloc_size/=(sizeof(TREE_ELEMENT)+size);
119
if (!default_alloc_size)
120
default_alloc_size=1;
121
default_alloc_size*=(sizeof(TREE_ELEMENT)+size);
125
tree->offset_to_key=0; /* use key through pointer */
126
tree->size_of_element+=sizeof(void*);
128
if (!(tree->with_delete=with_delete))
130
init_alloc_root(&tree->mem_root, (uint) default_alloc_size, 0);
131
tree->mem_root.min_malloc=(sizeof(TREE_ELEMENT)+tree->size_of_element);
136
static void free_tree(TREE *tree, myf free_flags)
138
DBUG_ENTER("free_tree");
139
DBUG_PRINT("enter",("tree: 0x%lx", (long) tree));
141
if (tree->root) /* If initialized */
143
if (tree->with_delete)
144
delete_tree_element(tree,tree->root);
149
if (tree->memory_limit)
150
(*tree->free)(NULL, free_init, tree->custom_arg);
151
delete_tree_element(tree,tree->root);
152
if (tree->memory_limit)
153
(*tree->free)(NULL, free_end, tree->custom_arg);
155
free_root(&tree->mem_root, free_flags);
158
tree->root= &tree->null_element;
159
tree->elements_in_tree=0;
165
void delete_tree(TREE* tree)
167
free_tree(tree, MYF(0)); /* my_free() mem_root if applicable */
170
void reset_tree(TREE* tree)
172
/* do not free mem_root, just mark blocks as free */
173
free_tree(tree, MYF(MY_MARK_BLOCKS_FREE));
177
static void delete_tree_element(TREE *tree, TREE_ELEMENT *element)
179
if (element != &tree->null_element)
181
delete_tree_element(tree,element->left);
183
(*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
184
delete_tree_element(tree,element->right);
185
if (tree->with_delete)
186
my_free((char*) element,MYF(0));
192
insert, search and delete of elements
194
The following should be true:
195
parent[0] = & parent[-1][0]->left ||
196
parent[0] = & parent[-1][0]->right
199
TREE_ELEMENT *tree_insert(TREE *tree, void *key, uint key_size,
203
TREE_ELEMENT *element,***parent;
205
parent= tree->parents;
206
*parent = &tree->root; element= tree->root;
209
if (element == &tree->null_element ||
210
(cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
215
*++parent= &element->right; element= element->right;
219
*++parent = &element->left; element= element->left;
222
if (element == &tree->null_element)
224
uint alloc_size=sizeof(TREE_ELEMENT)+key_size+tree->size_of_element;
225
tree->allocated+=alloc_size;
227
if (tree->memory_limit && tree->elements_in_tree
228
&& tree->allocated > tree->memory_limit)
231
return tree_insert(tree, key, key_size, custom_arg);
234
key_size+=tree->size_of_element;
235
if (tree->with_delete)
236
element=(TREE_ELEMENT *) my_malloc(alloc_size, MYF(MY_WME));
238
element=(TREE_ELEMENT *) alloc_root(&tree->mem_root,alloc_size);
242
element->left=element->right= &tree->null_element;
243
if (!tree->offset_to_key)
245
if (key_size == sizeof(void*)) /* no length, save pointer */
246
*((void**) (element+1))=key;
249
*((void**) (element+1))= (void*) ((void **) (element+1)+1);
250
memcpy((uchar*) *((void **) (element+1)),key,
251
(size_t) (key_size-sizeof(void*)));
255
memcpy((uchar*) element+tree->offset_to_key,key,(size_t) key_size);
256
element->count=1; /* May give warning in purify */
257
tree->elements_in_tree++;
258
rb_insert(tree,parent,element); /* rebalance tree */
262
if (tree->flag & TREE_NO_DUPS)
265
/* Avoid a wrap over of the count. */
266
if (! element->count)
269
DBUG_EXECUTE("check_tree", test_rb_tree(tree->root););
273
int tree_delete(TREE *tree, void *key, uint key_size, void *custom_arg)
275
int cmp,remove_colour;
276
TREE_ELEMENT *element,***parent, ***org_parent, *nod;
277
if (!tree->with_delete)
278
return 1; /* not allowed */
280
parent= tree->parents;
281
*parent= &tree->root; element= tree->root;
284
if (element == &tree->null_element)
285
return 1; /* Was not in tree */
286
if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
291
*++parent= &element->right; element= element->right;
295
*++parent = &element->left; element= element->left;
298
if (element->left == &tree->null_element)
300
(**parent)=element->right;
301
remove_colour= element->colour;
303
else if (element->right == &tree->null_element)
305
(**parent)=element->left;
306
remove_colour= element->colour;
311
*++parent= &element->right; nod= element->right;
312
while (nod->left != &tree->null_element)
314
*++parent= &nod->left; nod= nod->left;
316
(**parent)=nod->right; /* unlink nod from tree */
317
remove_colour= nod->colour;
318
org_parent[0][0]=nod; /* put y in place of element */
319
org_parent[1]= &nod->right;
320
nod->left=element->left;
321
nod->right=element->right;
322
nod->colour=element->colour;
324
if (remove_colour == BLACK)
325
rb_delete_fixup(tree,parent);
327
(*tree->free)(ELEMENT_KEY(tree,element), free_free, tree->custom_arg);
328
tree->allocated-= sizeof(TREE_ELEMENT) + tree->size_of_element + key_size;
329
my_free((uchar*) element,MYF(0));
330
tree->elements_in_tree--;
335
void *tree_search(TREE *tree, void *key, void *custom_arg)
338
TREE_ELEMENT *element=tree->root;
342
if (element == &tree->null_element)
344
if ((cmp = (*tree->compare)(custom_arg, ELEMENT_KEY(tree,element),
346
return ELEMENT_KEY(tree,element);
348
element=element->right;
350
element=element->left;
354
void *tree_search_key(TREE *tree, const void *key,
355
TREE_ELEMENT **parents, TREE_ELEMENT ***last_pos,
356
enum ha_rkey_function flag, void *custom_arg)
359
TREE_ELEMENT *element= tree->root;
360
TREE_ELEMENT **last_left_step_parent= NULL, **last_right_step_parent= NULL;
361
TREE_ELEMENT **last_equal_element= NULL;
364
TODO: support for HA_READ_KEY_OR_PREV, HA_READ_PREFIX flags if needed.
367
*parents = &tree->null_element;
368
while (element != &tree->null_element)
371
if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
375
case HA_READ_KEY_EXACT:
376
case HA_READ_KEY_OR_NEXT:
377
case HA_READ_BEFORE_KEY:
378
last_equal_element= parents;
381
case HA_READ_AFTER_KEY:
384
case HA_READ_PREFIX_LAST:
385
case HA_READ_PREFIX_LAST_OR_PREV:
386
last_equal_element= parents;
393
if (cmp < 0) /* element < key */
395
last_right_step_parent= parents;
396
element= element->right;
400
last_left_step_parent= parents;
401
element= element->left;
405
case HA_READ_KEY_EXACT:
406
case HA_READ_PREFIX_LAST:
407
*last_pos= last_equal_element;
409
case HA_READ_KEY_OR_NEXT:
410
*last_pos= last_equal_element ? last_equal_element : last_left_step_parent;
412
case HA_READ_AFTER_KEY:
413
*last_pos= last_left_step_parent;
415
case HA_READ_PREFIX_LAST_OR_PREV:
416
*last_pos= last_equal_element ? last_equal_element : last_right_step_parent;
418
case HA_READ_BEFORE_KEY:
419
*last_pos= last_right_step_parent;
424
return *last_pos ? ELEMENT_KEY(tree, **last_pos) : NULL;
428
Search first (the most left) or last (the most right) tree element
430
void *tree_search_edge(TREE *tree, TREE_ELEMENT **parents,
431
TREE_ELEMENT ***last_pos, int child_offs)
433
TREE_ELEMENT *element= tree->root;
435
*parents= &tree->null_element;
436
while (element != &tree->null_element)
439
element= ELEMENT_CHILD(element, child_offs);
442
return **last_pos != &tree->null_element ?
443
ELEMENT_KEY(tree, **last_pos) : NULL;
446
void *tree_search_next(TREE *tree, TREE_ELEMENT ***last_pos, int l_offs,
449
TREE_ELEMENT *x= **last_pos;
451
if (ELEMENT_CHILD(x, r_offs) != &tree->null_element)
453
x= ELEMENT_CHILD(x, r_offs);
455
while (ELEMENT_CHILD(x, l_offs) != &tree->null_element)
457
x= ELEMENT_CHILD(x, l_offs);
460
return ELEMENT_KEY(tree, x);
464
TREE_ELEMENT *y= *--*last_pos;
465
while (y != &tree->null_element && x == ELEMENT_CHILD(y, r_offs))
470
return y == &tree->null_element ? NULL : ELEMENT_KEY(tree, y);
475
Expected that tree is fully balanced
476
(each path from root to leaf has the same length)
478
ha_rows tree_record_pos(TREE *tree, const void *key,
479
enum ha_rkey_function flag, void *custom_arg)
482
TREE_ELEMENT *element= tree->root;
484
double right= tree->elements_in_tree;
486
while (element != &tree->null_element)
488
if ((cmp= (*tree->compare)(custom_arg, ELEMENT_KEY(tree, element),
492
case HA_READ_KEY_EXACT:
493
case HA_READ_BEFORE_KEY:
496
case HA_READ_AFTER_KEY:
503
if (cmp < 0) /* element < key */
505
element= element->right;
506
left= (left + right) / 2;
510
element= element->left;
511
right= (left + right) / 2;
515
case HA_READ_KEY_EXACT:
516
case HA_READ_BEFORE_KEY:
517
return (ha_rows) right;
518
case HA_READ_AFTER_KEY:
519
return (ha_rows) left;
525
int tree_walk(TREE *tree, tree_walk_action action, void *argument, TREE_WALK visit)
528
case left_root_right:
529
return tree_walk_left_root_right(tree,tree->root,action,argument);
530
case right_root_left:
531
return tree_walk_right_root_left(tree,tree->root,action,argument);
533
return 0; /* Keep gcc happy */
536
static int tree_walk_left_root_right(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
539
if (element->left) /* Not null_element */
541
if ((error=tree_walk_left_root_right(tree,element->left,action,
543
(error=(*action)(ELEMENT_KEY(tree,element),
544
(element_count) element->count,
546
error=tree_walk_left_root_right(tree,element->right,action,argument);
552
static int tree_walk_right_root_left(TREE *tree, TREE_ELEMENT *element, tree_walk_action action, void *argument)
555
if (element->right) /* Not null_element */
557
if ((error=tree_walk_right_root_left(tree,element->right,action,
559
(error=(*action)(ELEMENT_KEY(tree,element),
560
(element_count) element->count,
562
error=tree_walk_right_root_left(tree,element->left,action,argument);
569
/* Functions to fix up the tree after insert and delete */
571
static void left_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
581
static void right_rotate(TREE_ELEMENT **parent, TREE_ELEMENT *leaf)
591
static void rb_insert(TREE *tree, TREE_ELEMENT ***parent, TREE_ELEMENT *leaf)
593
TREE_ELEMENT *y,*par,*par2;
596
while (leaf != tree->root && (par=parent[-1][0])->colour == RED)
598
if (par == (par2=parent[-2][0])->left)
601
if (y->colour == RED)
607
leaf->colour=RED; /* And the loop continues */
611
if (leaf == par->right)
613
left_rotate(parent[-1],par);
614
par=leaf; /* leaf is now parent to old leaf */
618
right_rotate(parent[-2],par2);
625
if (y->colour == RED)
631
leaf->colour=RED; /* And the loop continues */
635
if (leaf == par->left)
637
right_rotate(parent[-1],par);
642
left_rotate(parent[-2],par2);
647
tree->root->colour=BLACK;
650
static void rb_delete_fixup(TREE *tree, TREE_ELEMENT ***parent)
652
TREE_ELEMENT *x,*w,*par;
655
while (x != tree->root && x->colour == BLACK)
657
if (x == (par=parent[-1][0])->left)
660
if (w->colour == RED)
664
left_rotate(parent[-1],par);
666
*++parent= &par->left;
669
if (w->left->colour == BLACK && w->right->colour == BLACK)
677
if (w->right->colour == BLACK)
679
w->left->colour=BLACK;
681
right_rotate(&par->right,w);
684
w->colour=par->colour;
686
w->right->colour=BLACK;
687
left_rotate(parent[-1],par);
695
if (w->colour == RED)
699
right_rotate(parent[-1],par);
700
parent[0]= &w->right;
701
*++parent= &par->right;
704
if (w->right->colour == BLACK && w->left->colour == BLACK)
712
if (w->left->colour == BLACK)
714
w->right->colour=BLACK;
716
left_rotate(&par->left,w);
719
w->colour=par->colour;
721
w->left->colour=BLACK;
722
right_rotate(parent[-1],par);
733
/* Test that the proporties for a red-black tree holds */
735
static int test_rb_tree(TREE_ELEMENT *element)
740
return 0; /* Found end of tree */
741
if (element->colour == RED &&
742
(element->left->colour == RED || element->right->colour == RED))
744
printf("Wrong tree: Found two red in a row\n");
747
count_l=test_rb_tree(element->left);
748
count_r=test_rb_tree(element->right);
749
if (count_l >= 0 && count_r >= 0)
751
if (count_l == count_r)
752
return count_l+(element->colour == BLACK);
753
printf("Wrong tree: Incorrect black-count: %d - %d\n",count_l,count_r);