1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 typedef struct _GRealTree GRealTree;
23 typedef struct _GTreeNode GTreeNode;
28 GCompareFunc key_compare;
33 gint balance; /* height (left) - height (right) */
34 GTreeNode *left; /* left subtree */
35 GTreeNode *right; /* right subtree */
36 gpointer key; /* key for this node */
37 gpointer value; /* value stored at this node */
41 static GTreeNode* g_tree_node_new (gpointer key,
43 static void g_tree_node_destroy (GTreeNode *node);
44 static GTreeNode* g_tree_node_insert (GTreeNode *node,
49 static GTreeNode* g_tree_node_remove (GTreeNode *node,
52 static GTreeNode* g_tree_node_balance (GTreeNode *node);
53 static GTreeNode* g_tree_node_remove_leftmost (GTreeNode *node,
54 GTreeNode **leftmost);
55 static GTreeNode* g_tree_node_restore_left_balance (GTreeNode *node,
57 static GTreeNode* g_tree_node_restore_right_balance (GTreeNode *node,
59 static gpointer g_tree_node_lookup (GTreeNode *node,
62 static gint g_tree_node_count (GTreeNode *node);
63 static gint g_tree_node_pre_order (GTreeNode *node,
64 GTraverseFunc traverse_func,
66 static gint g_tree_node_in_order (GTreeNode *node,
67 GTraverseFunc traverse_func,
69 static gint g_tree_node_post_order (GTreeNode *node,
70 GTraverseFunc traverse_func,
72 static gpointer g_tree_node_search (GTreeNode *node,
73 GSearchFunc search_func,
75 static gint g_tree_node_height (GTreeNode *node);
76 static GTreeNode* g_tree_node_rotate_left (GTreeNode *node);
77 static GTreeNode* g_tree_node_rotate_right (GTreeNode *node);
78 static void g_tree_node_check (GTreeNode *node);
81 static GMemChunk *node_mem_chunk = NULL;
82 static GSList *node_free_list = NULL;
86 g_tree_new (GCompareFunc key_compare_func)
90 rtree = g_new (GRealTree, 1);
92 rtree->key_compare = key_compare_func;
94 return (GTree*) rtree;
98 g_tree_destroy (GTree *tree)
102 g_return_if_fail (tree != NULL);
104 rtree = (GRealTree*) tree;
106 g_tree_node_destroy (rtree->root);
111 g_tree_insert (GTree *tree,
118 g_return_if_fail (tree != NULL);
120 rtree = (GRealTree*) tree;
123 rtree->root = g_tree_node_insert (rtree->root, rtree->key_compare,
124 key, value, &inserted);
128 g_tree_remove (GTree *tree,
133 g_return_if_fail (tree != NULL);
135 rtree = (GRealTree*) tree;
137 rtree->root = g_tree_node_remove (rtree->root, rtree->key_compare, key);
141 g_tree_lookup (GTree *tree,
146 g_return_val_if_fail (tree != NULL, NULL);
148 rtree = (GRealTree*) tree;
150 return g_tree_node_lookup (rtree->root, rtree->key_compare, key);
154 g_tree_traverse (GTree *tree,
155 GTraverseFunc traverse_func,
156 GTraverseType traverse_type,
161 g_return_if_fail (tree != NULL);
163 rtree = (GRealTree*) tree;
165 g_return_if_fail (rtree->root != NULL);
167 switch (traverse_type)
170 g_tree_node_pre_order (rtree->root, traverse_func, data);
174 g_tree_node_in_order (rtree->root, traverse_func, data);
178 g_tree_node_post_order (rtree->root, traverse_func, data);
182 g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
188 g_tree_search (GTree *tree,
189 GSearchFunc search_func,
194 g_return_val_if_fail (tree != NULL, NULL);
196 rtree = (GRealTree*) tree;
199 return g_tree_node_search (rtree->root, search_func, data);
204 g_tree_height (GTree *tree)
208 g_return_val_if_fail (tree != NULL, 0);
210 rtree = (GRealTree*) tree;
213 return g_tree_node_height (rtree->root);
218 g_tree_nnodes (GTree *tree)
222 g_return_val_if_fail (tree != NULL, 0);
224 rtree = (GRealTree*) tree;
227 return g_tree_node_count (rtree->root);
233 g_tree_node_new (gpointer key,
241 tmp_list = node_free_list;
242 node_free_list = node_free_list->next;
244 node = tmp_list->data;
247 GListAllocator *tmp_allocator = g_list_set_allocator (NULL);
248 g_slist_free_1 (tmp_list);
249 g_list_set_allocator (tmp_allocator);
255 node_mem_chunk = g_mem_chunk_new ("tree node mem chunk", sizeof (GTreeNode), 1024, G_ALLOC_ONLY);
257 node = g_chunk_new (GTreeNode, node_mem_chunk);
270 g_tree_node_destroy (GTreeNode *node)
274 node_free_list = g_slist_prepend (node_free_list, node);
275 g_tree_node_destroy (node->right);
276 g_tree_node_destroy (node->left);
281 g_tree_node_insert (GTreeNode *node,
282 GCompareFunc compare,
293 return g_tree_node_new (key, value);
296 cmp = (* compare) (key, node->key);
308 old_balance = node->left->balance;
309 node->left = g_tree_node_insert (node->left, compare, key, value, inserted);
311 if ((old_balance != node->left->balance) && node->left->balance)
317 node->left = g_tree_node_new (key, value);
325 old_balance = node->right->balance;
326 node->right = g_tree_node_insert (node->right, compare, key, value, inserted);
328 if ((old_balance != node->right->balance) && node->right->balance)
334 node->right = g_tree_node_new (key, value);
341 if ((node->balance < -1) || (node->balance > 1))
342 node = g_tree_node_balance (node);
349 g_tree_node_remove (GTreeNode *node,
350 GCompareFunc compare,
361 cmp = (* compare) (key, node->key);
372 old_balance = node->right->balance;
373 node->right = g_tree_node_remove_leftmost (node->right, &new_root);
374 new_root->left = node->left;
375 new_root->right = node->right;
376 new_root->balance = node->balance;
377 node = g_tree_node_restore_right_balance (new_root, old_balance);
380 node_free_list = g_slist_prepend (node_free_list, garbage);
386 old_balance = node->left->balance;
387 node->left = g_tree_node_remove (node->left, compare, key);
388 node = g_tree_node_restore_left_balance (node, old_balance);
395 old_balance = node->right->balance;
396 node->right = g_tree_node_remove (node->right, compare, key);
397 node = g_tree_node_restore_right_balance (node, old_balance);
405 g_tree_node_balance (GTreeNode *node)
407 if (node->balance < -1)
409 if (node->left->balance > 0)
410 node->left = g_tree_node_rotate_left (node->left);
411 node = g_tree_node_rotate_right (node);
413 else if (node->balance > 1)
415 if (node->right->balance < 0)
416 node->right = g_tree_node_rotate_right (node->right);
417 node = g_tree_node_rotate_left (node);
424 g_tree_node_remove_leftmost (GTreeNode *node,
425 GTreeNode **leftmost)
435 old_balance = node->left->balance;
436 node->left = g_tree_node_remove_leftmost (node->left, leftmost);
437 return g_tree_node_restore_left_balance (node, old_balance);
441 g_tree_node_restore_left_balance (GTreeNode *node,
446 else if ((node->left->balance != old_balance) &&
447 (node->left->balance == 0))
450 if (node->balance > 1)
451 return g_tree_node_balance (node);
456 g_tree_node_restore_right_balance (GTreeNode *node,
461 else if ((node->right->balance != old_balance) &&
462 (node->right->balance == 0))
465 if (node->balance < -1)
466 return g_tree_node_balance (node);
471 g_tree_node_lookup (GTreeNode *node,
472 GCompareFunc compare,
480 cmp = (* compare) (key, node->key);
487 return g_tree_node_lookup (node->left, compare, key);
492 return g_tree_node_lookup (node->right, compare, key);
499 g_tree_node_count (GTreeNode *node)
505 count += g_tree_node_count (node->left);
507 count += g_tree_node_count (node->right);
513 g_tree_node_pre_order (GTreeNode *node,
514 GTraverseFunc traverse_func,
517 if ((*traverse_func) (node->key, node->value, data))
521 if (g_tree_node_pre_order (node->left, traverse_func, data))
526 if (g_tree_node_pre_order (node->right, traverse_func, data))
534 g_tree_node_in_order (GTreeNode *node,
535 GTraverseFunc traverse_func,
540 if (g_tree_node_in_order (node->left, traverse_func, data))
543 if ((*traverse_func) (node->key, node->value, data))
547 if (g_tree_node_in_order (node->right, traverse_func, data))
555 g_tree_node_post_order (GTreeNode *node,
556 GTraverseFunc traverse_func,
561 if (g_tree_node_post_order (node->left, traverse_func, data))
566 if (g_tree_node_post_order (node->right, traverse_func, data))
569 if ((*traverse_func) (node->key, node->value, data))
576 g_tree_node_search (GTreeNode *node,
577 GSearchFunc search_func,
586 dir = (* search_func) (node->key, data);
594 } while (node && (dir != 0));
600 g_tree_node_height (GTreeNode *node)
611 left_height = g_tree_node_height (node->left);
614 right_height = g_tree_node_height (node->right);
616 return MAX (left_height, right_height) + 1;
623 g_tree_node_rotate_left (GTreeNode *node)
633 node->right = right->left;
636 a_bal = node->balance;
637 b_bal = right->balance;
642 right->balance = b_bal - 1;
644 right->balance = a_bal + b_bal - 2;
645 node->balance = a_bal - 1;
650 right->balance = a_bal - 2;
652 right->balance = b_bal - 1;
653 node->balance = a_bal - b_bal - 1;
660 g_tree_node_rotate_right (GTreeNode *node)
670 node->left = left->right;
673 a_bal = node->balance;
674 b_bal = left->balance;
679 left->balance = b_bal + 1;
681 left->balance = a_bal + 2;
682 node->balance = a_bal - b_bal + 1;
687 left->balance = b_bal + 1;
689 left->balance = a_bal + b_bal + 2;
690 node->balance = a_bal + 1;
697 g_tree_node_check (GTreeNode *node)
709 left_height = g_tree_node_height (node->left);
711 right_height = g_tree_node_height (node->right);
713 balance = right_height - left_height;
714 if (balance != node->balance)
715 g_print ("g_tree_node_check: failed: %d ( %d )\n",
716 balance, node->balance);
719 g_tree_node_check (node->left);
721 g_tree_node_check (node->right);