1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gtestutils.h"
40 * SECTION:trees-binary
41 * @title: Balanced Binary Trees
42 * @short_description: a sorted collection of key/value pairs optimized
43 * for searching and traversing in order
45 * The #GTree structure and its associated functions provide a sorted
46 * collection of key/value pairs optimized for searching and traversing
47 * in order. This means that most of the operations (access, search,
48 * insertion, deletion, ...) on #GTree are O(log(n)) in average and O(n)
49 * in worst case for time complexity. But, note that maintaining a
50 * balanced sorted #GTree of n elements is done in time O(n log(n)).
52 * To create a new #GTree use g_tree_new().
54 * To insert a key/value pair into a #GTree use g_tree_insert()
57 * To remove a key/value pair use g_tree_remove() (O(n log(n))).
59 * To look up the value corresponding to a given key, use
60 * g_tree_lookup() and g_tree_lookup_extended().
62 * To find out the number of nodes in a #GTree, use g_tree_nnodes(). To
63 * get the height of a #GTree, use g_tree_height().
65 * To traverse a #GTree, calling a function for each node visited in
66 * the traversal, use g_tree_foreach().
68 * To destroy a #GTree, use g_tree_destroy().
71 #define MAX_GTREE_HEIGHT 40
76 * The GTree struct is an opaque data structure representing a
77 * [balanced binary tree][glib-Balanced-Binary-Trees]. It should be
78 * accessed only by using the following functions.
83 GCompareDataFunc key_compare;
84 GDestroyNotify key_destroy_func;
85 GDestroyNotify value_destroy_func;
86 gpointer key_compare_data;
93 gpointer key; /* key for this node */
94 gpointer value; /* value stored at this node */
95 GTreeNode *left; /* left subtree */
96 GTreeNode *right; /* right subtree */
97 gint8 balance; /* height (right) - height (left) */
103 static GTreeNode* g_tree_node_new (gpointer key,
105 static GTreeNode *g_tree_insert_internal (GTree *tree,
109 static gboolean g_tree_remove_internal (GTree *tree,
112 static GTreeNode* g_tree_node_balance (GTreeNode *node);
113 static GTreeNode *g_tree_find_node (GTree *tree,
115 static gint g_tree_node_pre_order (GTreeNode *node,
116 GTraverseFunc traverse_func,
118 static gint g_tree_node_in_order (GTreeNode *node,
119 GTraverseFunc traverse_func,
121 static gint g_tree_node_post_order (GTreeNode *node,
122 GTraverseFunc traverse_func,
124 static GTreeNode *g_tree_node_search (GTreeNode *node,
125 GCompareFunc search_func,
127 static GTreeNode* g_tree_node_rotate_left (GTreeNode *node);
128 static GTreeNode* g_tree_node_rotate_right (GTreeNode *node);
130 static void g_tree_node_check (GTreeNode *node);
135 g_tree_node_new (gpointer key,
138 GTreeNode *node = g_slice_new (GTreeNode);
143 node->left_child = FALSE;
144 node->right_child = FALSE;
153 * @key_compare_func: the function used to order the nodes in the #GTree.
154 * It should return values similar to the standard strcmp() function -
155 * 0 if the two arguments are equal, a negative value if the first argument
156 * comes before the second, or a positive value if the first argument comes
159 * Creates a new #GTree.
161 * Returns: a newly allocated #GTree
164 g_tree_new (GCompareFunc key_compare_func)
166 g_return_val_if_fail (key_compare_func != NULL, NULL);
168 return g_tree_new_full ((GCompareDataFunc) key_compare_func, NULL,
173 * g_tree_new_with_data:
174 * @key_compare_func: qsort()-style comparison function
175 * @key_compare_data: data to pass to comparison function
177 * Creates a new #GTree with a comparison function that accepts user data.
178 * See g_tree_new() for more details.
180 * Returns: a newly allocated #GTree
183 g_tree_new_with_data (GCompareDataFunc key_compare_func,
184 gpointer key_compare_data)
186 g_return_val_if_fail (key_compare_func != NULL, NULL);
188 return g_tree_new_full (key_compare_func, key_compare_data,
194 * @key_compare_func: qsort()-style comparison function
195 * @key_compare_data: data to pass to comparison function
196 * @key_destroy_func: a function to free the memory allocated for the key
197 * used when removing the entry from the #GTree or %NULL if you don't
198 * want to supply such a function
199 * @value_destroy_func: a function to free the memory allocated for the
200 * value used when removing the entry from the #GTree or %NULL if you
201 * don't want to supply such a function
203 * Creates a new #GTree like g_tree_new() and allows to specify functions
204 * to free the memory allocated for the key and value that get called when
205 * removing the entry from the #GTree.
207 * Returns: a newly allocated #GTree
210 g_tree_new_full (GCompareDataFunc key_compare_func,
211 gpointer key_compare_data,
212 GDestroyNotify key_destroy_func,
213 GDestroyNotify value_destroy_func)
217 g_return_val_if_fail (key_compare_func != NULL, NULL);
219 tree = g_slice_new (GTree);
221 tree->key_compare = key_compare_func;
222 tree->key_destroy_func = key_destroy_func;
223 tree->value_destroy_func = value_destroy_func;
224 tree->key_compare_data = key_compare_data;
235 * Returns the first in-order node of the tree, or %NULL
238 * Returns: (nullable) (transfer none): the first node in the tree
243 g_tree_node_first (GTree *tree)
247 g_return_val_if_fail (tree != NULL, NULL);
254 while (tmp->left_child)
264 * Returns the last in-order node of the tree, or %NULL
267 * Returns: (nullable) (transfer none): the last node in the tree
272 g_tree_node_last (GTree *tree)
276 g_return_val_if_fail (tree != NULL, NULL);
283 while (tmp->right_child)
290 * g_tree_node_previous
291 * @node: a #GTree node
293 * Returns the previous in-order node of the tree, or %NULL
294 * if the passed node was already the first one.
296 * Returns: (nullable) (transfer none): the previous node in the tree
301 g_tree_node_previous (GTreeNode *node)
305 g_return_val_if_fail (node != NULL, NULL);
309 if (node->left_child)
310 while (tmp->right_child)
318 * @node: a #GTree node
320 * Returns the next in-order node of the tree, or %NULL
321 * if the passed node was already the last one.
323 * Returns: (nullable) (transfer none): the next node in the tree
328 g_tree_node_next (GTreeNode *node)
332 g_return_val_if_fail (node != NULL, NULL);
336 if (node->right_child)
337 while (tmp->left_child)
347 * Removes all nodes from a #GTree and destroys their keys and values,
348 * then resets the #GTree’s root to %NULL.
353 g_tree_remove_all (GTree *tree)
358 g_return_if_fail (tree != NULL);
360 node = g_tree_node_first (tree);
364 next = g_tree_node_next (node);
366 if (tree->key_destroy_func)
367 tree->key_destroy_func (node->key);
368 if (tree->value_destroy_func)
369 tree->value_destroy_func (node->value);
370 g_slice_free (GTreeNode, node);
373 g_assert (tree->nnodes > 0);
381 g_assert (tree->nnodes == 0);
394 * Increments the reference count of @tree by one.
396 * It is safe to call this function from any thread.
398 * Returns: the passed in #GTree
403 g_tree_ref (GTree *tree)
405 g_return_val_if_fail (tree != NULL, NULL);
407 g_atomic_int_inc (&tree->ref_count);
416 * Decrements the reference count of @tree by one.
417 * If the reference count drops to 0, all keys and values will
418 * be destroyed (if destroy functions were specified) and all
419 * memory allocated by @tree will be released.
421 * It is safe to call this function from any thread.
426 g_tree_unref (GTree *tree)
428 g_return_if_fail (tree != NULL);
430 if (g_atomic_int_dec_and_test (&tree->ref_count))
432 g_tree_remove_all (tree);
433 g_slice_free (GTree, tree);
441 * Removes all keys and values from the #GTree and decreases its
442 * reference count by one. If keys and/or values are dynamically
443 * allocated, you should either free them first or create the #GTree
444 * using g_tree_new_full(). In the latter case the destroy functions
445 * you supplied will be called on all keys and values before destroying
449 g_tree_destroy (GTree *tree)
451 g_return_if_fail (tree != NULL);
453 g_tree_remove_all (tree);
458 * g_tree_insert_node:
460 * @key: the key to insert
461 * @value: the value corresponding to the key
463 * Inserts a key/value pair into a #GTree.
465 * If the given key already exists in the #GTree its corresponding value
466 * is set to the new value. If you supplied a @value_destroy_func when
467 * creating the #GTree, the old value is freed using that function. If
468 * you supplied a @key_destroy_func when creating the #GTree, the passed
469 * key is freed using that function.
471 * The tree is automatically 'balanced' as new key/value pairs are added,
472 * so that the distance from the root to every leaf is as small as possible.
473 * The cost of maintaining a balanced tree while inserting new key/value
474 * result in a O(n log(n)) operation where most of the other operations
477 * Returns: (transfer none): the inserted (or set) node.
482 g_tree_insert_node (GTree *tree,
488 g_return_val_if_fail (tree != NULL, NULL);
490 node = g_tree_insert_internal (tree, key, value, FALSE);
493 g_tree_node_check (tree->root);
502 * @key: the key to insert
503 * @value: the value corresponding to the key
505 * Inserts a key/value pair into a #GTree.
507 * Inserts a new key and value into a #GTree as g_tree_insert_node() does,
508 * only this function does not return the inserted or set node.
511 g_tree_insert (GTree *tree,
515 g_tree_insert_node (tree, key, value);
519 * g_tree_replace_node:
521 * @key: the key to insert
522 * @value: the value corresponding to the key
524 * Inserts a new key and value into a #GTree similar to g_tree_insert_node().
525 * The difference is that if the key already exists in the #GTree, it gets
526 * replaced by the new key. If you supplied a @value_destroy_func when
527 * creating the #GTree, the old value is freed using that function. If you
528 * supplied a @key_destroy_func when creating the #GTree, the old key is
529 * freed using that function.
531 * The tree is automatically 'balanced' as new key/value pairs are added,
532 * so that the distance from the root to every leaf is as small as possible.
534 * Returns: (transfer none): the inserted (or set) node.
539 g_tree_replace_node (GTree *tree,
545 g_return_val_if_fail (tree != NULL, NULL);
547 node = g_tree_insert_internal (tree, key, value, TRUE);
550 g_tree_node_check (tree->root);
559 * @key: the key to insert
560 * @value: the value corresponding to the key
562 * Inserts a new key and value into a #GTree as g_tree_replace_node() does,
563 * only this function does not return the inserted or set node.
566 g_tree_replace (GTree *tree,
570 g_tree_replace_node (tree, key, value);
573 /* internal insert routine */
575 g_tree_insert_internal (GTree *tree,
580 GTreeNode *node, *retnode;
581 GTreeNode *path[MAX_GTREE_HEIGHT];
584 g_return_val_if_fail (tree != NULL, NULL);
588 tree->root = g_tree_node_new (key, value);
599 int cmp = tree->key_compare (key, node->key, tree->key_compare_data);
603 if (tree->value_destroy_func)
604 tree->value_destroy_func (node->value);
610 if (tree->key_destroy_func)
611 tree->key_destroy_func (node->key);
617 /* free the passed key */
618 if (tree->key_destroy_func)
619 tree->key_destroy_func (key);
626 if (node->left_child)
633 GTreeNode *child = g_tree_node_new (key, value);
635 child->left = node->left;
638 node->left_child = TRUE;
649 if (node->right_child)
656 GTreeNode *child = g_tree_node_new (key, value);
658 child->right = node->right;
661 node->right_child = TRUE;
672 /* Restore balance. This is the goodness of a non-recursive
673 * implementation, when we are done with balancing we 'break'
674 * the loop and we are done.
678 GTreeNode *bparent = path[--idx];
679 gboolean left_node = (bparent && node == bparent->left);
680 g_assert (!bparent || bparent->left == node || bparent->right == node);
682 if (node->balance < -1 || node->balance > 1)
684 node = g_tree_node_balance (node);
688 bparent->left = node;
690 bparent->right = node;
693 if (node->balance == 0 || bparent == NULL)
697 bparent->balance -= 1;
699 bparent->balance += 1;
710 * @key: the key to remove
712 * Removes a key/value pair from a #GTree.
714 * If the #GTree was created using g_tree_new_full(), the key and value
715 * are freed using the supplied destroy functions, otherwise you have to
716 * make sure that any dynamically allocated values are freed yourself.
717 * If the key does not exist in the #GTree, the function does nothing.
719 * The cost of maintaining a balanced tree while removing a key/value
720 * result in a O(n log(n)) operation where most of the other operations
723 * Returns: %TRUE if the key was found (prior to 2.8, this function
727 g_tree_remove (GTree *tree,
732 g_return_val_if_fail (tree != NULL, FALSE);
734 removed = g_tree_remove_internal (tree, key, FALSE);
737 g_tree_node_check (tree->root);
746 * @key: the key to remove
748 * Removes a key and its associated value from a #GTree without calling
749 * the key and value destroy functions.
751 * If the key does not exist in the #GTree, the function does nothing.
753 * Returns: %TRUE if the key was found (prior to 2.8, this function
757 g_tree_steal (GTree *tree,
762 g_return_val_if_fail (tree != NULL, FALSE);
764 removed = g_tree_remove_internal (tree, key, TRUE);
767 g_tree_node_check (tree->root);
773 /* internal remove routine */
775 g_tree_remove_internal (GTree *tree,
779 GTreeNode *node, *parent, *balance;
780 GTreeNode *path[MAX_GTREE_HEIGHT];
784 g_return_val_if_fail (tree != NULL, FALSE);
795 int cmp = tree->key_compare (key, node->key, tree->key_compare_data);
801 if (!node->left_child)
809 if (!node->right_child)
817 /* The following code is almost equal to g_tree_remove_node,
818 * except that we do not have to call g_tree_node_parent.
820 balance = parent = path[--idx];
821 g_assert (!parent || parent->left == node || parent->right == node);
822 left_node = (parent && node == parent->left);
824 if (!node->left_child)
826 if (!node->right_child)
832 parent->left_child = FALSE;
833 parent->left = node->left;
834 parent->balance += 1;
838 parent->right_child = FALSE;
839 parent->right = node->right;
840 parent->balance -= 1;
843 else /* node has a right child */
845 GTreeNode *tmp = g_tree_node_next (node);
846 tmp->left = node->left;
849 tree->root = node->right;
852 parent->left = node->right;
853 parent->balance += 1;
857 parent->right = node->right;
858 parent->balance -= 1;
862 else /* node has a left child */
864 if (!node->right_child)
866 GTreeNode *tmp = g_tree_node_previous (node);
867 tmp->right = node->right;
870 tree->root = node->left;
873 parent->left = node->left;
874 parent->balance += 1;
878 parent->right = node->left;
879 parent->balance -= 1;
882 else /* node has a both children (pant, pant!) */
884 GTreeNode *prev = node->left;
885 GTreeNode *next = node->right;
886 GTreeNode *nextp = node;
887 int old_idx = idx + 1;
890 /* path[idx] == parent */
891 /* find the immediately next node (and its parent) */
892 while (next->left_child)
894 path[++idx] = nextp = next;
898 path[old_idx] = next;
901 /* remove 'next' from the tree */
904 if (next->right_child)
905 nextp->left = next->right;
907 nextp->left_child = FALSE;
910 next->right_child = TRUE;
911 next->right = node->right;
916 /* set the prev to point to the right place */
917 while (prev->right_child)
921 /* prepare 'next' to replace 'node' */
922 next->left_child = TRUE;
923 next->left = node->left;
924 next->balance = node->balance;
931 parent->right = next;
935 /* restore balance */
939 GTreeNode *bparent = path[--idx];
940 g_assert (!bparent || bparent->left == balance || bparent->right == balance);
941 left_node = (bparent && balance == bparent->left);
943 if(balance->balance < -1 || balance->balance > 1)
945 balance = g_tree_node_balance (balance);
947 tree->root = balance;
949 bparent->left = balance;
951 bparent->right = balance;
954 if (balance->balance != 0 || !bparent)
958 bparent->balance += 1;
960 bparent->balance -= 1;
967 if (tree->key_destroy_func)
968 tree->key_destroy_func (node->key);
969 if (tree->value_destroy_func)
970 tree->value_destroy_func (node->value);
973 g_slice_free (GTreeNode, node);
982 * @node: a #GTree node
984 * Gets the key stored at a particular tree node.
986 * Returns: (nullable) (transfer none): the key at the node.
991 g_tree_node_key (GTreeNode *node)
993 g_return_val_if_fail (node != NULL, NULL);
1000 * @node: a #GTree node
1002 * Gets the value stored at a particular tree node.
1004 * Returns: (nullable) (transfer none): the value at the node.
1009 g_tree_node_value (GTreeNode *node)
1011 g_return_val_if_fail (node != NULL, NULL);
1017 * g_tree_lookup_node:
1019 * @key: the key to look up
1021 * Gets the tree node corresponding to the given key. Since a #GTree is
1022 * automatically balanced as key/value pairs are added, key lookup
1023 * is O(log n) (where n is the number of key/value pairs in the tree).
1025 * Returns: (nullable) (transfer none): the tree node corresponding to
1026 * the key, or %NULL if the key was not found
1031 g_tree_lookup_node (GTree *tree,
1034 g_return_val_if_fail (tree != NULL, NULL);
1036 return g_tree_find_node (tree, key);
1042 * @key: the key to look up
1044 * Gets the value corresponding to the given key. Since a #GTree is
1045 * automatically balanced as key/value pairs are added, key lookup
1046 * is O(log n) (where n is the number of key/value pairs in the tree).
1048 * Returns: the value corresponding to the key, or %NULL
1049 * if the key was not found
1052 g_tree_lookup (GTree *tree,
1057 node = g_tree_lookup_node (tree, key);
1059 return node ? node->value : NULL;
1063 * g_tree_lookup_extended:
1065 * @lookup_key: the key to look up
1066 * @orig_key: (out) (optional) (nullable): returns the original key
1067 * @value: (out) (optional) (nullable): returns the value associated with the key
1069 * Looks up a key in the #GTree, returning the original key and the
1070 * associated value. This is useful if you need to free the memory
1071 * allocated for the original key, for example before calling
1074 * Returns: %TRUE if the key was found in the #GTree
1077 g_tree_lookup_extended (GTree *tree,
1078 gconstpointer lookup_key,
1084 g_return_val_if_fail (tree != NULL, FALSE);
1086 node = g_tree_find_node (tree, lookup_key);
1091 *orig_key = node->key;
1093 *value = node->value;
1103 * @func: the function to call for each node visited.
1104 * If this function returns %TRUE, the traversal is stopped.
1105 * @user_data: user data to pass to the function
1107 * Calls the given function for each of the key/value pairs in the #GTree.
1108 * The function is passed the key and value of each pair, and the given
1109 * @data parameter. The tree is traversed in sorted order.
1111 * The tree may not be modified while iterating over it (you can't
1112 * add/remove items). To remove all items matching a predicate, you need
1113 * to add each item to a list in your #GTraverseFunc as you walk over
1114 * the tree, then walk the list and remove each item.
1117 g_tree_foreach (GTree *tree,
1123 g_return_if_fail (tree != NULL);
1128 node = g_tree_node_first (tree);
1132 if ((*func) (node->key, node->value, user_data))
1135 node = g_tree_node_next (node);
1140 * g_tree_foreach_node:
1142 * @func: the function to call for each node visited.
1143 * If this function returns %TRUE, the traversal is stopped.
1144 * @user_data: user data to pass to the function
1146 * Calls the given function for each of the nodes in the #GTree.
1147 * The function is passed the pointer to the particular node, and the given
1148 * @data parameter. The tree traversal happens in-order.
1150 * The tree may not be modified while iterating over it (you can't
1151 * add/remove items). To remove all items matching a predicate, you need
1152 * to add each item to a list in your #GTraverseFunc as you walk over
1153 * the tree, then walk the list and remove each item.
1158 g_tree_foreach_node (GTree *tree,
1159 GTraverseNodeFunc func,
1164 g_return_if_fail (tree != NULL);
1169 node = g_tree_node_first (tree);
1173 if ((*func) (node, user_data))
1176 node = g_tree_node_next (node);
1183 * @traverse_func: the function to call for each node visited. If this
1184 * function returns %TRUE, the traversal is stopped.
1185 * @traverse_type: the order in which nodes are visited, one of %G_IN_ORDER,
1186 * %G_PRE_ORDER and %G_POST_ORDER
1187 * @user_data: user data to pass to the function
1189 * Calls the given function for each node in the #GTree.
1191 * Deprecated:2.2: The order of a balanced tree is somewhat arbitrary.
1192 * If you just want to visit all nodes in sorted order, use
1193 * g_tree_foreach() instead. If you really need to visit nodes in
1194 * a different order, consider using an [n-ary tree][glib-N-ary-Trees].
1198 * @key: a key of a #GTree node
1199 * @value: the value corresponding to the key
1200 * @user_data: user data passed to g_tree_traverse()
1202 * Specifies the type of function passed to g_tree_traverse(). It is
1203 * passed the key and value of each node, together with the @user_data
1204 * parameter passed to g_tree_traverse(). If the function returns
1205 * %TRUE, the traversal is stopped.
1207 * Returns: %TRUE to stop the traversal
1210 g_tree_traverse (GTree *tree,
1211 GTraverseFunc traverse_func,
1212 GTraverseType traverse_type,
1215 g_return_if_fail (tree != NULL);
1220 switch (traverse_type)
1223 g_tree_node_pre_order (tree->root, traverse_func, user_data);
1227 g_tree_node_in_order (tree->root, traverse_func, user_data);
1231 g_tree_node_post_order (tree->root, traverse_func, user_data);
1235 g_warning ("g_tree_traverse(): traverse type G_LEVEL_ORDER isn't implemented.");
1241 * g_tree_search_node:
1243 * @search_func: a function used to search the #GTree
1244 * @user_data: the data passed as the second argument to @search_func
1246 * Searches a #GTree using @search_func.
1248 * The @search_func is called with a pointer to the key of a key/value
1249 * pair in the tree, and the passed in @user_data. If @search_func returns
1250 * 0 for a key/value pair, then the corresponding node is returned as
1251 * the result of g_tree_search(). If @search_func returns -1, searching
1252 * will proceed among the key/value pairs that have a smaller key; if
1253 * @search_func returns 1, searching will proceed among the key/value
1254 * pairs that have a larger key.
1256 * Returns: (nullable) (transfer none): the node corresponding to the
1257 * found key, or %NULL if the key was not found
1262 g_tree_search_node (GTree *tree,
1263 GCompareFunc search_func,
1264 gconstpointer user_data)
1266 g_return_val_if_fail (tree != NULL, NULL);
1271 return g_tree_node_search (tree->root, search_func, user_data);
1277 * @search_func: a function used to search the #GTree
1278 * @user_data: the data passed as the second argument to @search_func
1280 * Searches a #GTree using @search_func.
1282 * The @search_func is called with a pointer to the key of a key/value
1283 * pair in the tree, and the passed in @user_data. If @search_func returns
1284 * 0 for a key/value pair, then the corresponding value is returned as
1285 * the result of g_tree_search(). If @search_func returns -1, searching
1286 * will proceed among the key/value pairs that have a smaller key; if
1287 * @search_func returns 1, searching will proceed among the key/value
1288 * pairs that have a larger key.
1290 * Returns: the value corresponding to the found key, or %NULL
1291 * if the key was not found
1294 g_tree_search (GTree *tree,
1295 GCompareFunc search_func,
1296 gconstpointer user_data)
1300 node = g_tree_search_node (tree, search_func, user_data);
1302 return node ? node->value : NULL;
1306 * g_tree_lower_bound:
1308 * @key: the key to calculate the lower bound for
1310 * Gets the lower bound node corresponding to the given key,
1311 * or %NULL if the tree is empty or all the nodes in the tree
1312 * have keys that are strictly lower than the searched key.
1314 * The lower bound is the first node that has its key greater
1315 * than or equal to the searched key.
1317 * Returns: (nullable) (transfer none): the tree node corresponding to
1318 * the lower bound, or %NULL if the tree is empty or has only
1319 * keys strictly lower than the searched key.
1324 g_tree_lower_bound (GTree *tree,
1327 GTreeNode *node, *result;
1330 g_return_val_if_fail (tree != NULL, NULL);
1339 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
1344 if (!node->left_child)
1351 if (!node->right_child)
1360 * g_tree_upper_bound:
1362 * @key: the key to calculate the upper bound for
1364 * Gets the upper bound node corresponding to the given key,
1365 * or %NULL if the tree is empty or all the nodes in the tree
1366 * have keys that are lower than or equal to the searched key.
1368 * The upper bound is the first node that has its key strictly greater
1369 * than the searched key.
1371 * Returns: (nullable) (transfer none): the tree node corresponding to the
1372 * upper bound, or %NULL if the tree is empty or has only keys
1373 * lower than or equal to the searched key.
1378 g_tree_upper_bound (GTree *tree,
1381 GTreeNode *node, *result;
1384 g_return_val_if_fail (tree != NULL, NULL);
1393 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
1398 if (!node->left_child)
1405 if (!node->right_child)
1417 * Gets the height of a #GTree.
1419 * If the #GTree contains no nodes, the height is 0.
1420 * If the #GTree contains only one root node the height is 1.
1421 * If the root node has children the height is 2, etc.
1423 * Returns: the height of @tree
1426 g_tree_height (GTree *tree)
1431 g_return_val_if_fail (tree != NULL, 0);
1441 height += 1 + MAX(node->balance, 0);
1443 if (!node->left_child)
1454 * Gets the number of nodes in a #GTree.
1456 * Returns: the number of nodes in @tree
1459 g_tree_nnodes (GTree *tree)
1461 g_return_val_if_fail (tree != NULL, 0);
1463 return tree->nnodes;
1467 g_tree_node_balance (GTreeNode *node)
1469 if (node->balance < -1)
1471 if (node->left->balance > 0)
1472 node->left = g_tree_node_rotate_left (node->left);
1473 node = g_tree_node_rotate_right (node);
1475 else if (node->balance > 1)
1477 if (node->right->balance < 0)
1478 node->right = g_tree_node_rotate_right (node->right);
1479 node = g_tree_node_rotate_left (node);
1486 g_tree_find_node (GTree *tree,
1498 cmp = tree->key_compare (key, node->key, tree->key_compare_data);
1503 if (!node->left_child)
1510 if (!node->right_child)
1519 g_tree_node_pre_order (GTreeNode *node,
1520 GTraverseFunc traverse_func,
1523 if ((*traverse_func) (node->key, node->value, data))
1526 if (node->left_child)
1528 if (g_tree_node_pre_order (node->left, traverse_func, data))
1532 if (node->right_child)
1534 if (g_tree_node_pre_order (node->right, traverse_func, data))
1542 g_tree_node_in_order (GTreeNode *node,
1543 GTraverseFunc traverse_func,
1546 if (node->left_child)
1548 if (g_tree_node_in_order (node->left, traverse_func, data))
1552 if ((*traverse_func) (node->key, node->value, data))
1555 if (node->right_child)
1557 if (g_tree_node_in_order (node->right, traverse_func, data))
1565 g_tree_node_post_order (GTreeNode *node,
1566 GTraverseFunc traverse_func,
1569 if (node->left_child)
1571 if (g_tree_node_post_order (node->left, traverse_func, data))
1575 if (node->right_child)
1577 if (g_tree_node_post_order (node->right, traverse_func, data))
1581 if ((*traverse_func) (node->key, node->value, data))
1588 g_tree_node_search (GTreeNode *node,
1589 GCompareFunc search_func,
1599 dir = (* search_func) (node->key, data);
1604 if (!node->left_child)
1611 if (!node->right_child)
1620 g_tree_node_rotate_left (GTreeNode *node)
1626 right = node->right;
1628 if (right->left_child)
1629 node->right = right->left;
1632 node->right_child = FALSE;
1633 right->left_child = TRUE;
1637 a_bal = node->balance;
1638 b_bal = right->balance;
1643 right->balance = b_bal - 1;
1645 right->balance = a_bal + b_bal - 2;
1646 node->balance = a_bal - 1;
1651 right->balance = a_bal - 2;
1653 right->balance = b_bal - 1;
1654 node->balance = a_bal - b_bal - 1;
1661 g_tree_node_rotate_right (GTreeNode *node)
1669 if (left->right_child)
1670 node->left = left->right;
1673 node->left_child = FALSE;
1674 left->right_child = TRUE;
1678 a_bal = node->balance;
1679 b_bal = left->balance;
1684 left->balance = b_bal + 1;
1686 left->balance = a_bal + 2;
1687 node->balance = a_bal - b_bal + 1;
1692 left->balance = b_bal + 1;
1694 left->balance = a_bal + b_bal + 2;
1695 node->balance = a_bal + 1;
1703 g_tree_node_height (GTreeNode *node)
1713 if (node->left_child)
1714 left_height = g_tree_node_height (node->left);
1716 if (node->right_child)
1717 right_height = g_tree_node_height (node->right);
1719 return MAX (left_height, right_height) + 1;
1726 g_tree_node_check (GTreeNode *node)
1735 if (node->left_child)
1737 tmp = g_tree_node_previous (node);
1738 g_assert (tmp->right == node);
1741 if (node->right_child)
1743 tmp = g_tree_node_next (node);
1744 g_assert (tmp->left == node);
1750 if (node->left_child)
1751 left_height = g_tree_node_height (node->left);
1752 if (node->right_child)
1753 right_height = g_tree_node_height (node->right);
1755 balance = right_height - left_height;
1756 g_assert (balance == node->balance);
1758 if (node->left_child)
1759 g_tree_node_check (node->left);
1760 if (node->right_child)
1761 g_tree_node_check (node->right);
1766 g_tree_node_dump (GTreeNode *node,
1769 g_print ("%*s%c\n", indent, "", *(char *)node->key);
1771 if (node->left_child)
1773 g_print ("%*sLEFT\n", indent, "");
1774 g_tree_node_dump (node->left, indent + 2);
1776 else if (node->left)
1777 g_print ("%*s<%c\n", indent + 2, "", *(char *)node->left->key);
1779 if (node->right_child)
1781 g_print ("%*sRIGHT\n", indent, "");
1782 g_tree_node_dump (node->right, indent + 2);
1784 else if (node->right)
1785 g_print ("%*s>%c\n", indent + 2, "", *(char *)node->right->key);
1789 g_tree_dump (GTree *tree)
1792 g_tree_node_dump (tree->root, 0);