1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 trees of data with any number of branches.
7 <!-- ##### SECTION Long_Description ##### -->
9 The #GNode struct and its associated functions provide a N-ary tree data
10 structure, where nodes in the tree can contain arbitrary data.
13 To create a new tree use g_node_new().
16 To insert a node into a tree use g_node_insert(), g_node_insert_before(),
17 g_node_append() and g_node_prepend().
20 To create a new node and insert it into a tree use g_node_insert_data(),
21 g_node_insert_data_before(), g_node_append_data() and g_node_prepend_data().
24 To reverse the children of a node use g_node_reverse_children().
27 To find a node use g_node_get_root(), g_node_find(), g_node_find_child(),
28 g_node_child_index(), g_node_child_position(),
29 g_node_first_child(), g_node_last_child(),
30 g_node_nth_child(), g_node_first_sibling(), g_node_prev_sibling(),
31 g_node_next_sibling() or g_node_last_sibling().
34 To get information about a node or tree use G_NODE_IS_LEAF(),
35 G_NODE_IS_ROOT(), g_node_depth(), g_node_n_nodes(), g_node_n_children(),
36 g_node_is_ancestor() or g_node_max_height().
39 To traverse a tree, calling a function for each node visited in the
40 traversal, use g_node_traverse() or g_node_children_foreach().
43 To remove a node or subtree from a tree use g_node_unlink() or
47 <!-- ##### SECTION See_Also ##### -->
52 <!-- ##### STRUCT GNode ##### -->
54 The <structname>GNode</structname> struct represents one node in a
55 <link linkend="glib-N-ary-Trees">N-ary Tree</link>.
56 The <structfield>data</structfield> field contains the actual data of the node.
57 The <structfield>next</structfield> and <structfield>prev</structfield>
58 fields point to the node's siblings (a sibling is another <structname>GNode</structname> with the
60 The <structfield>parent</structfield> field points to the parent of the <structname>GNode</structname>,
61 or is %NULL if the <structname>GNode</structname> is the root of the tree.
62 The <structfield>children</structfield> field points to the first child of the
63 <structname>GNode</structname>. The other children are accessed by using the
64 <structfield>next</structfield> pointer of each child.
73 <!-- ##### FUNCTION g_node_new ##### -->
75 Creates a new #GNode containing the given data.
76 Used to create the first node in a tree.
79 @data: the data of the new node.
80 @Returns: a new #GNode.
83 <!-- ##### FUNCTION g_node_copy ##### -->
85 Recursively copies a #GNode (but does not deep-copy the data inside the nodes,
86 since there's no way for GLib to know how to do that).
90 @Returns: a new #GNode containing the same data pointers.
93 <!-- ##### FUNCTION g_node_insert ##### -->
95 Inserts a #GNode beneath the parent at the given position.
98 @parent: the #GNode to place @node under.
99 @position: the position to place @node at, with respect to its siblings.
100 If position is -1, @node is inserted as the last child of @parent.
101 @node: the #GNode to insert.
102 @Returns: the inserted #GNode.
105 <!-- ##### FUNCTION g_node_insert_before ##### -->
107 Inserts a #GNode beneath the parent before the given sibling.
110 @parent: the #GNode to place @node under.
111 @sibling: the sibling #GNode to place @node before. If sibling is %NULL,
112 the node is inserted as the last child of @parent.
113 @node: the #GNode to insert.
114 @Returns: the inserted #GNode.
117 <!-- ##### FUNCTION g_node_insert_after ##### -->
119 Inserts a #GNode beneath the parent after the given sibling.
122 @parent: the #GNode to place @node under.
123 @sibling: the sibling #GNode to place @node after. If sibling is %NULL,
124 the node is inserted as the first child of @parent.
125 @node: the #GNode to insert.
126 @Returns: the inserted #GNode.
129 <!-- ##### MACRO g_node_append ##### -->
131 Inserts a #GNode as the last child of the given parent.
134 @parent: the #GNode to place the new #GNode under.
135 @node: the #GNode to insert.
136 @Returns: the inserted #GNode.
139 <!-- ##### FUNCTION g_node_prepend ##### -->
141 Inserts a #GNode as the first child of the given parent.
144 @parent: the #GNode to place the new #GNode under.
145 @node: the #GNode to insert.
146 @Returns: the inserted #GNode.
149 <!-- ##### MACRO g_node_insert_data ##### -->
151 Inserts a new #GNode at the given position.
154 @parent: the #GNode to place the new #GNode under.
155 @position: the position to place the new #GNode at.
156 If position is -1, the new #GNode is inserted as the last child of @parent.
157 @data: the data for the new #GNode.
158 @Returns: the new #GNode.
161 <!-- ##### MACRO g_node_insert_data_before ##### -->
163 Inserts a new #GNode before the given sibling.
166 @parent: the #GNode to place the new #GNode under.
167 @sibling: the sibling #GNode to place the new #GNode before.
168 @data: the data for the new #GNode.
169 @Returns: the new #GNode.
172 <!-- ##### MACRO g_node_append_data ##### -->
174 Inserts a new #GNode as the last child of the given parent.
177 @parent: the #GNode to place the new #GNode under.
178 @data: the data for the new #GNode.
179 @Returns: the new #GNode.
182 <!-- ##### MACRO g_node_prepend_data ##### -->
184 Inserts a new #GNode as the first child of the given parent.
187 @parent: the #GNode to place the new #GNode under.
188 @data: the data for the new #GNode.
189 @Returns: the new #GNode.
192 <!-- ##### FUNCTION g_node_reverse_children ##### -->
194 Reverses the order of the children of a #GNode.
195 (It doesn't change the order of the grandchildren.)
201 <!-- ##### FUNCTION g_node_traverse ##### -->
203 Traverses a tree starting at the given root #GNode.
204 It calls the given function for each node visited.
205 The traversal can be halted at any point by returning %TRUE from @func.
208 @root: the root #GNode of the tree to traverse.
209 @order: the order in which nodes are visited - %G_IN_ORDER, %G_PRE_ORDER,
210 %G_POST_ORDER, or %G_LEVEL_ORDER.
211 @flags: which types of children are to be visited, one of %G_TRAVERSE_ALL,
212 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
213 @max_depth: the maximum depth of the traversal. Nodes below this
214 depth will not be visited. If max_depth is -1 all nodes in the tree are
215 visited. If depth is 1, only the root is visited. If depth is 2, the root
216 and its children are visited. And so on.
217 @func: the function to call for each visited #GNode.
218 @data: user data to pass to the function.
221 <!-- ##### ENUM GTraverseFlags ##### -->
223 Specifies which nodes are visited during several of the tree functions,
224 including g_node_traverse() and g_node_find().
227 @G_TRAVERSE_LEAFS: only leaf nodes should be visited.
228 @G_TRAVERSE_NON_LEAFS: only non-leaf nodes should be visited.
229 @G_TRAVERSE_ALL: all nodes should be visited.
232 <!-- ##### USER_FUNCTION GNodeTraverseFunc ##### -->
234 Specifies the type of function passed to g_node_traverse().
235 The function is called with each of the nodes visited, together with the
236 user data passed to g_node_traverse().
237 If the function returns %TRUE, then the traversal is stopped.
241 @data: user data passed to g_node_traverse().
242 @Returns: %TRUE to stop the traversal.
245 <!-- ##### FUNCTION g_node_children_foreach ##### -->
247 Calls a function for each of the children of a #GNode.
248 Note that it doesn't descend beneath the child nodes.
252 @flags: which types of children are to be visited, one of %G_TRAVERSE_ALL,
253 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
254 @func: the function to call for each visited node.
255 @data: user data to pass to the function.
258 <!-- ##### USER_FUNCTION GNodeForeachFunc ##### -->
260 Specifies the type of function passed to g_node_children_foreach().
261 The function is called with each child node, together with the user data
262 passed to g_node_children_foreach().
266 @data: user data passed to g_node_children_foreach().
269 <!-- ##### FUNCTION g_node_get_root ##### -->
271 Gets the root of a tree.
275 @Returns: the root of the tree.
278 <!-- ##### FUNCTION g_node_find ##### -->
280 Finds a #GNode in a tree.
283 @root: the root #GNode of the tree to search.
284 @order: the order in which nodes are visited - %G_IN_ORDER, %G_PRE_ORDER,
285 %G_POST_ORDER, or %G_LEVEL_ORDER.
286 @flags: which types of children are to be searched, one of %G_TRAVERSE_ALL,
287 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
288 @data: the data to find.
289 @Returns: the found #GNode, or %NULL if the data is not found.
292 <!-- ##### FUNCTION g_node_find_child ##### -->
294 Finds the first child of a #GNode with the given data.
298 @flags: which types of children are to be searched, one of %G_TRAVERSE_ALL,
299 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
300 @data: the data to find.
301 @Returns: the found child #GNode, or %NULL if the data is not found.
304 <!-- ##### FUNCTION g_node_child_index ##### -->
306 Gets the position of the first child of a #GNode which contains the given data.
310 @data: the data to find.
311 @Returns: the index of the child of @node which contains @data, or -1
312 if the data is not found.
315 <!-- ##### FUNCTION g_node_child_position ##### -->
317 Gets the position of a #GNode with respect to its siblings.
318 @child must be a child of @node.
319 The first child is numbered 0, the second 1, and so on.
323 @child: a child of @node.
324 @Returns: the position of @child with respect to its siblings.
327 <!-- ##### MACRO g_node_first_child ##### -->
329 Gets the first child of a #GNode.
333 @Returns: the last child of @node, or %NULL if @node is %NULL or has no children.
336 <!-- ##### FUNCTION g_node_last_child ##### -->
338 Gets the last child of a #GNode.
341 @node: a #GNode (must not be %NULL).
342 @Returns: the last child of @node, or %NULL if @node has no children.
345 <!-- ##### FUNCTION g_node_nth_child ##### -->
347 Gets a child of a #GNode, using the given index.
348 The first child is at index 0. If the index is too big, %NULL is returned.
352 @n: the index of the desired child.
353 @Returns: the child of @node at index @n.
356 <!-- ##### FUNCTION g_node_first_sibling ##### -->
358 Gets the first sibling of a #GNode.
359 This could possibly be the node itself.
363 @Returns: the first sibling of @node.
366 <!-- ##### MACRO g_node_next_sibling ##### -->
368 Gets the next sibling of a #GNode.
372 @Returns: the next sibling of @node, or %NULL if @node is %NULL.
375 <!-- ##### MACRO g_node_prev_sibling ##### -->
377 Gets the previous sibling of a #GNode.
381 @Returns: the previous sibling of @node, or %NULL if @node is %NULL.
384 <!-- ##### FUNCTION g_node_last_sibling ##### -->
386 Gets the last sibling of a #GNode.
387 This could possibly be the node itself.
391 @Returns: the last sibling of @node.
394 <!-- ##### MACRO G_NODE_IS_LEAF ##### -->
396 Returns %TRUE if a #GNode is a leaf node.
400 @Returns: %TRUE if the #GNode is a leaf node (i.e. it has no children).
403 <!-- ##### MACRO G_NODE_IS_ROOT ##### -->
405 Returns %TRUE if a #GNode is the root of a tree.
409 @Returns: %TRUE if the #GNode is the root of a tree (i.e. it has no parent
413 <!-- ##### FUNCTION g_node_depth ##### -->
415 Gets the depth of a #GNode.
418 If @node is %NULL the depth is 0.
419 The root node has a depth of 1.
420 For the children of the root node the depth is 2. And so on.
424 @Returns: the depth of the #GNode.
427 <!-- ##### FUNCTION g_node_n_nodes ##### -->
429 Gets the number of nodes in a tree.
433 @flags: which types of children are to be counted, one of %G_TRAVERSE_ALL,
434 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
435 @Returns: the number of nodes in the tree.
438 <!-- ##### FUNCTION g_node_n_children ##### -->
440 Gets the number of children of a #GNode.
444 @Returns: the number of children of @node.
447 <!-- ##### FUNCTION g_node_is_ancestor ##### -->
449 Returns %TRUE if @node is an ancestor of @descendant.
450 This is true if node is the parent of @descendant, or if node is the
451 grandparent of @descendant etc.
455 @descendant: a #GNode.
456 @Returns: %TRUE if @node is an ancestor of @descendant.
459 <!-- ##### FUNCTION g_node_max_height ##### -->
461 Gets the maximum height of all branches beneath a #GNode.
462 This is the maximum distance from the #GNode to all leaf nodes.
465 If @root is %NULL, 0 is returned. If @root has no children, 1 is returned.
466 If @root has children, 2 is returned. And so on.
470 @Returns: the maximum height of the tree beneath @root.
473 <!-- ##### FUNCTION g_node_unlink ##### -->
475 Unlinks a #GNode from a tree, resulting in two separate trees.
478 @node: the #GNode to unlink, which becomes the root of a new tree.
481 <!-- ##### FUNCTION g_node_destroy ##### -->
483 Removes the #GNode and its children from the tree, freeing any memory
487 @root: the root of the tree/subtree to destroy.
490 <!-- ##### FUNCTION g_node_push_allocator ##### -->
492 Sets the allocator to use to allocate #GNode elements.
493 Use g_node_pop_allocator() to restore the previous allocator.
496 @allocator: the #GAllocator to use when allocating #GNode elements.
499 <!-- ##### FUNCTION g_node_pop_allocator ##### -->
501 Restores the previous #GAllocator, used when allocating #GNode elements.