1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GNode: N-way tree implementation.
5 * Copyright (C) 1998 Tim Janik
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
38 #include "gtestutils.h"
43 * @short_description: trees of data with any number of branches
45 * The #GNode struct and its associated functions provide a N-ary tree
46 * data structure, where nodes in the tree can contain arbitrary data.
48 * To create a new tree use g_node_new().
50 * To insert a node into a tree use g_node_insert(),
51 * g_node_insert_before(), g_node_append() and g_node_prepend().
53 * To create a new node and insert it into a tree use
54 * g_node_insert_data(), g_node_insert_data_after(),
55 * g_node_insert_data_before(), g_node_append_data()
56 * and g_node_prepend_data().
58 * To reverse the children of a node use g_node_reverse_children().
60 * To find a node use g_node_get_root(), g_node_find(),
61 * g_node_find_child(), g_node_child_index(), g_node_child_position(),
62 * g_node_first_child(), g_node_last_child(), g_node_nth_child(),
63 * g_node_first_sibling(), g_node_prev_sibling(), g_node_next_sibling()
64 * or g_node_last_sibling().
66 * To get information about a node or tree use G_NODE_IS_LEAF(),
67 * G_NODE_IS_ROOT(), g_node_depth(), g_node_n_nodes(),
68 * g_node_n_children(), g_node_is_ancestor() or g_node_max_height().
70 * To traverse a tree, calling a function for each node visited in the
71 * traversal, use g_node_traverse() or g_node_children_foreach().
73 * To remove a node or subtree from a tree use g_node_unlink() or
79 * @data: contains the actual data of the node.
80 * @next: points to the node's next sibling (a sibling is another
81 * #GNode with the same parent).
82 * @prev: points to the node's previous sibling.
83 * @parent: points to the parent of the #GNode, or is %NULL if the
84 * #GNode is the root of the tree.
85 * @children: points to the first child of the #GNode. The other
86 * children are accessed by using the @next pointer of each
89 * The #GNode struct represents one node in a
90 * <link linkend="glib-N-ary-Trees">N-ary Tree</link>. fields
93 #define g_node_alloc0() g_slice_new0 (GNode)
94 #define g_node_free(node) g_slice_free (GNode, node)
96 /* --- functions --- */
99 * @data: the data of the new node
101 * Creates a new #GNode containing the given data.
102 * Used to create the first node in a tree.
104 * Returns: a new #GNode
107 g_node_new (gpointer data)
109 GNode *node = g_node_alloc0 ();
115 g_nodes_free (GNode *node)
119 GNode *next = node->next;
121 g_nodes_free (node->children);
129 * @root: the root of the tree/subtree to destroy
131 * Removes @root and its children from the tree, freeing any memory
135 g_node_destroy (GNode *root)
137 g_return_if_fail (root != NULL);
139 if (!G_NODE_IS_ROOT (root))
140 g_node_unlink (root);
147 * @node: the #GNode to unlink, which becomes the root of a new tree
149 * Unlinks a #GNode from a tree, resulting in two separate trees.
152 g_node_unlink (GNode *node)
154 g_return_if_fail (node != NULL);
157 node->prev->next = node->next;
158 else if (node->parent)
159 node->parent->children = node->next;
163 node->next->prev = node->prev;
172 * @copy_func: the function which is called to copy the data inside each node,
173 * or %NULL to use the original data.
174 * @data: data to pass to @copy_func
176 * Recursively copies a #GNode and its data.
178 * Return value: a new #GNode containing copies of the data in @node.
183 g_node_copy_deep (GNode *node,
187 GNode *new_node = NULL;
189 if (copy_func == NULL)
190 return g_node_copy (node);
194 GNode *child, *new_child;
196 new_node = g_node_new (copy_func (node->data, data));
198 for (child = g_node_last_child (node); child; child = child->prev)
200 new_child = g_node_copy_deep (child, copy_func, data);
201 g_node_prepend (new_node, new_child);
212 * Recursively copies a #GNode (but does not deep-copy the data inside the
213 * nodes, see g_node_copy_deep() if you need that).
215 * Returns: a new #GNode containing the same data pointers
218 g_node_copy (GNode *node)
220 GNode *new_node = NULL;
226 new_node = g_node_new (node->data);
228 for (child = g_node_last_child (node); child; child = child->prev)
229 g_node_prepend (new_node, g_node_copy (child));
237 * @parent: the #GNode to place @node under
238 * @position: the position to place @node at, with respect to its siblings
239 * If position is -1, @node is inserted as the last child of @parent
240 * @node: the #GNode to insert
242 * Inserts a #GNode beneath the parent at the given position.
244 * Returns: the inserted #GNode
247 g_node_insert (GNode *parent,
251 g_return_val_if_fail (parent != NULL, node);
252 g_return_val_if_fail (node != NULL, node);
253 g_return_val_if_fail (G_NODE_IS_ROOT (node), node);
256 return g_node_insert_before (parent,
257 g_node_nth_child (parent, position),
259 else if (position == 0)
260 return g_node_prepend (parent, node);
261 else /* if (position < 0) */
262 return g_node_append (parent, node);
266 * g_node_insert_before:
267 * @parent: the #GNode to place @node under
268 * @sibling: the sibling #GNode to place @node before.
269 * If sibling is %NULL, the node is inserted as the last child of @parent.
270 * @node: the #GNode to insert
272 * Inserts a #GNode beneath the parent before the given sibling.
274 * Returns: the inserted #GNode
277 g_node_insert_before (GNode *parent,
281 g_return_val_if_fail (parent != NULL, node);
282 g_return_val_if_fail (node != NULL, node);
283 g_return_val_if_fail (G_NODE_IS_ROOT (node), node);
285 g_return_val_if_fail (sibling->parent == parent, node);
287 node->parent = parent;
293 node->prev = sibling->prev;
294 node->prev->next = node;
295 node->next = sibling;
296 sibling->prev = node;
300 node->parent->children = node;
301 node->next = sibling;
302 sibling->prev = node;
307 if (parent->children)
309 sibling = parent->children;
310 while (sibling->next)
311 sibling = sibling->next;
312 node->prev = sibling;
313 sibling->next = node;
316 node->parent->children = node;
323 * g_node_insert_after:
324 * @parent: the #GNode to place @node under
325 * @sibling: the sibling #GNode to place @node after.
326 * If sibling is %NULL, the node is inserted as the first child of @parent.
327 * @node: the #GNode to insert
329 * Inserts a #GNode beneath the parent after the given sibling.
331 * Returns: the inserted #GNode
334 g_node_insert_after (GNode *parent,
338 g_return_val_if_fail (parent != NULL, node);
339 g_return_val_if_fail (node != NULL, node);
340 g_return_val_if_fail (G_NODE_IS_ROOT (node), node);
342 g_return_val_if_fail (sibling->parent == parent, node);
344 node->parent = parent;
350 sibling->next->prev = node;
352 node->next = sibling->next;
353 node->prev = sibling;
354 sibling->next = node;
358 if (parent->children)
360 node->next = parent->children;
361 parent->children->prev = node;
363 parent->children = node;
371 * @parent: the #GNode to place the new #GNode under
372 * @node: the #GNode to insert
374 * Inserts a #GNode as the first child of the given parent.
376 * Returns: the inserted #GNode
379 g_node_prepend (GNode *parent,
382 g_return_val_if_fail (parent != NULL, node);
384 return g_node_insert_before (parent, parent->children, node);
391 * Gets the root of a tree.
393 * Returns: the root of the tree
396 g_node_get_root (GNode *node)
398 g_return_val_if_fail (node != NULL, NULL);
407 * g_node_is_ancestor:
409 * @descendant: a #GNode
411 * Returns %TRUE if @node is an ancestor of @descendant.
412 * This is true if node is the parent of @descendant,
413 * or if node is the grandparent of @descendant etc.
415 * Returns: %TRUE if @node is an ancestor of @descendant
418 g_node_is_ancestor (GNode *node,
421 g_return_val_if_fail (node != NULL, FALSE);
422 g_return_val_if_fail (descendant != NULL, FALSE);
426 if (descendant->parent == node)
429 descendant = descendant->parent;
439 * Gets the depth of a #GNode.
441 * If @node is %NULL the depth is 0. The root node has a depth of 1.
442 * For the children of the root node the depth is 2. And so on.
444 * Returns: the depth of the #GNode
447 g_node_depth (GNode *node)
461 * g_node_reverse_children:
464 * Reverses the order of the children of a #GNode.
465 * (It doesn't change the order of the grandchildren.)
468 g_node_reverse_children (GNode *node)
473 g_return_if_fail (node != NULL);
475 child = node->children;
481 last->next = last->prev;
484 node->children = last;
491 * Gets the maximum height of all branches beneath a #GNode.
492 * This is the maximum distance from the #GNode to all leaf nodes.
494 * If @root is %NULL, 0 is returned. If @root has no children,
495 * 1 is returned. If @root has children, 2 is returned. And so on.
497 * Returns: the maximum height of the tree beneath @root
500 g_node_max_height (GNode *root)
503 guint max_height = 0;
508 child = root->children;
513 tmp_height = g_node_max_height (child);
514 if (tmp_height > max_height)
515 max_height = tmp_height;
519 return max_height + 1;
523 g_node_traverse_pre_order (GNode *node,
524 GTraverseFlags flags,
525 GNodeTraverseFunc func,
532 if ((flags & G_TRAVERSE_NON_LEAFS) &&
536 child = node->children;
542 child = current->next;
543 if (g_node_traverse_pre_order (current, flags, func, data))
547 else if ((flags & G_TRAVERSE_LEAFS) &&
555 g_node_depth_traverse_pre_order (GNode *node,
556 GTraverseFlags flags,
558 GNodeTraverseFunc func,
565 if ((flags & G_TRAVERSE_NON_LEAFS) &&
573 child = node->children;
579 child = current->next;
580 if (g_node_depth_traverse_pre_order (current, flags, depth, func, data))
584 else if ((flags & G_TRAVERSE_LEAFS) &&
592 g_node_traverse_post_order (GNode *node,
593 GTraverseFlags flags,
594 GNodeTraverseFunc func,
601 child = node->children;
607 child = current->next;
608 if (g_node_traverse_post_order (current, flags, func, data))
612 if ((flags & G_TRAVERSE_NON_LEAFS) &&
617 else if ((flags & G_TRAVERSE_LEAFS) &&
625 g_node_depth_traverse_post_order (GNode *node,
626 GTraverseFlags flags,
628 GNodeTraverseFunc func,
638 child = node->children;
644 child = current->next;
645 if (g_node_depth_traverse_post_order (current, flags, depth, func, data))
650 if ((flags & G_TRAVERSE_NON_LEAFS) &&
655 else if ((flags & G_TRAVERSE_LEAFS) &&
663 g_node_traverse_in_order (GNode *node,
664 GTraverseFlags flags,
665 GNodeTraverseFunc func,
673 child = node->children;
675 child = current->next;
677 if (g_node_traverse_in_order (current, flags, func, data))
680 if ((flags & G_TRAVERSE_NON_LEAFS) &&
687 child = current->next;
688 if (g_node_traverse_in_order (current, flags, func, data))
692 else if ((flags & G_TRAVERSE_LEAFS) &&
700 g_node_depth_traverse_in_order (GNode *node,
701 GTraverseFlags flags,
703 GNodeTraverseFunc func,
714 child = node->children;
716 child = current->next;
718 if (g_node_depth_traverse_in_order (current, flags, depth, func, data))
721 if ((flags & G_TRAVERSE_NON_LEAFS) &&
728 child = current->next;
729 if (g_node_depth_traverse_in_order (current, flags, depth, func, data))
733 else if ((flags & G_TRAVERSE_NON_LEAFS) &&
737 else if ((flags & G_TRAVERSE_LEAFS) &&
745 g_node_traverse_level (GNode *node,
746 GTraverseFlags flags,
748 GNodeTraverseFunc func,
750 gboolean *more_levels)
757 return (flags & G_TRAVERSE_NON_LEAFS) && func (node, data);
761 return (flags & G_TRAVERSE_LEAFS) && func (node, data);
766 node = node->children;
770 if (g_node_traverse_level (node, flags, level - 1, func, data, more_levels))
781 g_node_depth_traverse_level (GNode *node,
782 GTraverseFlags flags,
784 GNodeTraverseFunc func,
788 gboolean more_levels;
791 while (level != depth)
794 if (g_node_traverse_level (node, flags, level, func, data, &more_levels))
805 * @root: the root #GNode of the tree to traverse
806 * @order: the order in which nodes are visited - %G_IN_ORDER,
807 * %G_PRE_ORDER, %G_POST_ORDER, or %G_LEVEL_ORDER.
808 * @flags: which types of children are to be visited, one of
809 * %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
810 * @max_depth: the maximum depth of the traversal. Nodes below this
811 * depth will not be visited. If max_depth is -1 all nodes in
812 * the tree are visited. If depth is 1, only the root is visited.
813 * If depth is 2, the root and its children are visited. And so on.
814 * @func: the function to call for each visited #GNode
815 * @data: user data to pass to the function
817 * Traverses a tree starting at the given root #GNode.
818 * It calls the given function for each node visited.
819 * The traversal can be halted at any point by returning %TRUE from @func.
824 * @G_IN_ORDER: vists a node's left child first, then the node itself,
825 * then its right child. This is the one to use if you
826 * want the output sorted according to the compare
831 * <imagedata align="right" fileref="Sorted_binary_tree_inorder.svg" format="SVG"/>
833 * <caption>In order: A, B, C, D, E, F, G, H, I</caption>
836 * @G_PRE_ORDER: visits a node, then its children.
840 * <imagedata align="right" fileref="Sorted_binary_tree_preorder.svg" format="SVG"/>
842 * <caption>Pre order: F, B, A, D, C, E, G, I, H</caption>
845 * @G_POST_ORDER: visits the node's children, then the node itself.
849 * <imagedata align="right" fileref="Sorted_binary_tree_postorder.svg" format="SVG"/>
851 * <caption>Post order: A, C, E, D, B, H, I, G, F</caption>
854 * @G_LEVEL_ORDER: is not implemented for <link
855 * linkend="glib-Balanced-Binary-Trees">Balanced Binary
856 * Trees</link>. For <link
857 * linkend="glib-N-ary-Trees">N-ary Trees</link>, it
858 * vists the root node first, then its children, then
859 * its grandchildren, and so on. Note that this is less
860 * efficient than the other orders.
864 * <imagedata align="right" fileref="Sorted_binary_tree_breadth-first_traversal.svg" format="SVG"/>
866 * <caption>Level order: F, B, G, A, D, I, C, E, H</caption>
870 * Specifies the type of traveral performed by g_tree_traverse(),
871 * g_node_traverse() and g_node_find().
876 * @G_TRAVERSE_LEAVES: only leaf nodes should be visited. This name has
877 * been introduced in 2.6, for older version use
879 * @G_TRAVERSE_NON_LEAVES: only non-leaf nodes should be visited. This
880 * name has been introduced in 2.6, for older
881 * version use %G_TRAVERSE_NON_LEAFS.
882 * @G_TRAVERSE_ALL: all nodes should be visited.
883 * @G_TRAVERSE_MASK: a mask of all traverse flags.
884 * @G_TRAVERSE_LEAFS: identical to %G_TRAVERSE_LEAVES.
885 * @G_TRAVERSE_NON_LEAFS: identical to %G_TRAVERSE_NON_LEAVES.
887 * Specifies which nodes are visited during several of the tree
888 * functions, including g_node_traverse() and g_node_find().
893 * @data: user data passed to g_node_traverse().
895 * Specifies the type of function passed to g_node_traverse(). The
896 * function is called with each of the nodes visited, together with the
897 * user data passed to g_node_traverse(). If the function returns
898 * %TRUE, then the traversal is stopped.
900 * Returns: %TRUE to stop the traversal.
903 g_node_traverse (GNode *root,
905 GTraverseFlags flags,
907 GNodeTraverseFunc func,
910 g_return_if_fail (root != NULL);
911 g_return_if_fail (func != NULL);
912 g_return_if_fail (order <= G_LEVEL_ORDER);
913 g_return_if_fail (flags <= G_TRAVERSE_MASK);
914 g_return_if_fail (depth == -1 || depth > 0);
920 g_node_traverse_pre_order (root, flags, func, data);
922 g_node_depth_traverse_pre_order (root, flags, depth, func, data);
926 g_node_traverse_post_order (root, flags, func, data);
928 g_node_depth_traverse_post_order (root, flags, depth, func, data);
932 g_node_traverse_in_order (root, flags, func, data);
934 g_node_depth_traverse_in_order (root, flags, depth, func, data);
937 g_node_depth_traverse_level (root, flags, depth, func, data);
943 g_node_find_func (GNode *node,
948 if (*d != node->data)
958 * @root: the root #GNode of the tree to search
959 * @order: the order in which nodes are visited - %G_IN_ORDER,
960 * %G_PRE_ORDER, %G_POST_ORDER, or %G_LEVEL_ORDER
961 * @flags: which types of children are to be searched, one of
962 * %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
963 * @data: the data to find
965 * Finds a #GNode in a tree.
967 * Returns: the found #GNode, or %NULL if the data is not found
970 g_node_find (GNode *root,
972 GTraverseFlags flags,
977 g_return_val_if_fail (root != NULL, NULL);
978 g_return_val_if_fail (order <= G_LEVEL_ORDER, NULL);
979 g_return_val_if_fail (flags <= G_TRAVERSE_MASK, NULL);
984 g_node_traverse (root, order, flags, -1, g_node_find_func, d);
990 g_node_count_func (GNode *node,
991 GTraverseFlags flags,
998 if (flags & G_TRAVERSE_NON_LEAFS)
1001 child = node->children;
1004 g_node_count_func (child, flags, n);
1005 child = child->next;
1008 else if (flags & G_TRAVERSE_LEAFS)
1015 * @flags: which types of children are to be counted, one of
1016 * %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
1018 * Gets the number of nodes in a tree.
1020 * Returns: the number of nodes in the tree
1023 g_node_n_nodes (GNode *root,
1024 GTraverseFlags flags)
1028 g_return_val_if_fail (root != NULL, 0);
1029 g_return_val_if_fail (flags <= G_TRAVERSE_MASK, 0);
1031 g_node_count_func (root, flags, &n);
1037 * g_node_last_child:
1038 * @node: a #GNode (must not be %NULL)
1040 * Gets the last child of a #GNode.
1042 * Returns: the last child of @node, or %NULL if @node has no children
1045 g_node_last_child (GNode *node)
1047 g_return_val_if_fail (node != NULL, NULL);
1049 node = node->children;
1060 * @n: the index of the desired child
1062 * Gets a child of a #GNode, using the given index.
1063 * The first child is at index 0. If the index is
1064 * too big, %NULL is returned.
1066 * Returns: the child of @node at index @n
1069 g_node_nth_child (GNode *node,
1072 g_return_val_if_fail (node != NULL, NULL);
1074 node = node->children;
1076 while ((n-- > 0) && node)
1083 * g_node_n_children:
1086 * Gets the number of children of a #GNode.
1088 * Returns: the number of children of @node
1091 g_node_n_children (GNode *node)
1095 g_return_val_if_fail (node != NULL, 0);
1097 node = node->children;
1108 * g_node_find_child:
1110 * @flags: which types of children are to be searched, one of
1111 * %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
1112 * @data: the data to find
1114 * Finds the first child of a #GNode with the given data.
1116 * Returns: the found child #GNode, or %NULL if the data is not found
1119 g_node_find_child (GNode *node,
1120 GTraverseFlags flags,
1123 g_return_val_if_fail (node != NULL, NULL);
1124 g_return_val_if_fail (flags <= G_TRAVERSE_MASK, NULL);
1126 node = node->children;
1129 if (node->data == data)
1131 if (G_NODE_IS_LEAF (node))
1133 if (flags & G_TRAVERSE_LEAFS)
1138 if (flags & G_TRAVERSE_NON_LEAFS)
1149 * g_node_child_position:
1151 * @child: a child of @node
1153 * Gets the position of a #GNode with respect to its siblings.
1154 * @child must be a child of @node. The first child is numbered 0,
1155 * the second 1, and so on.
1157 * Returns: the position of @child with respect to its siblings
1160 g_node_child_position (GNode *node,
1165 g_return_val_if_fail (node != NULL, -1);
1166 g_return_val_if_fail (child != NULL, -1);
1167 g_return_val_if_fail (child->parent == node, -1);
1169 node = node->children;
1182 * g_node_child_index:
1184 * @data: the data to find
1186 * Gets the position of the first child of a #GNode
1187 * which contains the given data.
1189 * Returns: the index of the child of @node which contains
1190 * @data, or -1 if the data is not found
1193 g_node_child_index (GNode *node,
1198 g_return_val_if_fail (node != NULL, -1);
1200 node = node->children;
1203 if (node->data == data)
1213 * g_node_first_sibling:
1216 * Gets the first sibling of a #GNode.
1217 * This could possibly be the node itself.
1219 * Returns: the first sibling of @node
1222 g_node_first_sibling (GNode *node)
1224 g_return_val_if_fail (node != NULL, NULL);
1227 return node->parent->children;
1236 * g_node_last_sibling:
1239 * Gets the last sibling of a #GNode.
1240 * This could possibly be the node itself.
1242 * Returns: the last sibling of @node
1245 g_node_last_sibling (GNode *node)
1247 g_return_val_if_fail (node != NULL, NULL);
1256 * g_node_children_foreach:
1258 * @flags: which types of children are to be visited, one of
1259 * %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
1260 * @func: the function to call for each visited node
1261 * @data: user data to pass to the function
1263 * Calls a function for each of the children of a #GNode.
1264 * Note that it doesn't descend beneath the child nodes.
1269 * @data: user data passed to g_node_children_foreach().
1271 * Specifies the type of function passed to g_node_children_foreach().
1272 * The function is called with each child node, together with the user
1273 * data passed to g_node_children_foreach().
1276 g_node_children_foreach (GNode *node,
1277 GTraverseFlags flags,
1278 GNodeForeachFunc func,
1281 g_return_if_fail (node != NULL);
1282 g_return_if_fail (flags <= G_TRAVERSE_MASK);
1283 g_return_if_fail (func != NULL);
1285 node = node->children;
1291 node = current->next;
1292 if (G_NODE_IS_LEAF (current))
1294 if (flags & G_TRAVERSE_LEAFS)
1295 func (current, data);
1299 if (flags & G_TRAVERSE_NON_LEAFS)
1300 func (current, data);