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 <!-- ##### SECTION Stability_Level ##### -->
55 <!-- ##### STRUCT GNode ##### -->
57 The <structname>GNode</structname> struct represents one node in a
58 <link linkend="glib-N-ary-Trees">N-ary Tree</link>.
59 The <structfield>data</structfield> field contains the actual data of the node.
60 The <structfield>next</structfield> and <structfield>prev</structfield>
61 fields point to the node's siblings (a sibling is another <structname>GNode</structname> with the
63 The <structfield>parent</structfield> field points to the parent of the <structname>GNode</structname>,
64 or is %NULL if the <structname>GNode</structname> is the root of the tree.
65 The <structfield>children</structfield> field points to the first child of the
66 <structname>GNode</structname>. The other children are accessed by using the
67 <structfield>next</structfield> pointer of each child.
76 <!-- ##### FUNCTION g_node_new ##### -->
78 Creates a new #GNode containing the given data.
79 Used to create the first node in a tree.
82 @data: the data of the new node.
83 @Returns: a new #GNode.
86 <!-- ##### FUNCTION g_node_copy ##### -->
88 Recursively copies a #GNode (but does not deep-copy the data inside the nodes,
89 see g_node_copy_deep() if you need that).
93 @Returns: a new #GNode containing the same data pointers.
96 <!-- ##### USER_FUNCTION GCopyFunc ##### -->
98 A function of this signature is used to copy the node data when doing a deep-copy
102 @src: A pointer to the data which should be copied.
103 @data: Additional data.
104 @Returns: A pointer to the copy.
108 <!-- ##### FUNCTION g_node_copy_deep ##### -->
119 <!-- ##### FUNCTION g_node_insert ##### -->
121 Inserts a #GNode beneath the parent at the given position.
124 @parent: the #GNode to place @node under.
125 @position: the position to place @node at, with respect to its siblings.
126 If position is -1, @node is inserted as the last child of @parent.
127 @node: the #GNode to insert.
128 @Returns: the inserted #GNode.
131 <!-- ##### FUNCTION g_node_insert_before ##### -->
133 Inserts a #GNode beneath the parent before the given sibling.
136 @parent: the #GNode to place @node under.
137 @sibling: the sibling #GNode to place @node before. If sibling is %NULL,
138 the node is inserted as the last child of @parent.
139 @node: the #GNode to insert.
140 @Returns: the inserted #GNode.
143 <!-- ##### FUNCTION g_node_insert_after ##### -->
145 Inserts a #GNode beneath the parent after the given sibling.
148 @parent: the #GNode to place @node under.
149 @sibling: the sibling #GNode to place @node after. If sibling is %NULL,
150 the node is inserted as the first child of @parent.
151 @node: the #GNode to insert.
152 @Returns: the inserted #GNode.
155 <!-- ##### MACRO g_node_append ##### -->
157 Inserts a #GNode as the last child of the given parent.
160 @parent: the #GNode to place the new #GNode under.
161 @node: the #GNode to insert.
162 @Returns: the inserted #GNode.
165 <!-- ##### FUNCTION g_node_prepend ##### -->
167 Inserts a #GNode as the first child of the given parent.
170 @parent: the #GNode to place the new #GNode under.
171 @node: the #GNode to insert.
172 @Returns: the inserted #GNode.
175 <!-- ##### MACRO g_node_insert_data ##### -->
177 Inserts a new #GNode at the given position.
180 @parent: the #GNode to place the new #GNode under.
181 @position: the position to place the new #GNode at.
182 If position is -1, the new #GNode is inserted as the last child of @parent.
183 @data: the data for the new #GNode.
184 @Returns: the new #GNode.
187 <!-- ##### MACRO g_node_insert_data_before ##### -->
189 Inserts a new #GNode before the given sibling.
192 @parent: the #GNode to place the new #GNode under.
193 @sibling: the sibling #GNode to place the new #GNode before.
194 @data: the data for the new #GNode.
195 @Returns: the new #GNode.
198 <!-- ##### MACRO g_node_append_data ##### -->
200 Inserts a new #GNode as the last child of the given parent.
203 @parent: the #GNode to place the new #GNode under.
204 @data: the data for the new #GNode.
205 @Returns: the new #GNode.
208 <!-- ##### MACRO g_node_prepend_data ##### -->
210 Inserts a new #GNode as the first child of the given parent.
213 @parent: the #GNode to place the new #GNode under.
214 @data: the data for the new #GNode.
215 @Returns: the new #GNode.
218 <!-- ##### FUNCTION g_node_reverse_children ##### -->
220 Reverses the order of the children of a #GNode.
221 (It doesn't change the order of the grandchildren.)
227 <!-- ##### FUNCTION g_node_traverse ##### -->
229 Traverses a tree starting at the given root #GNode.
230 It calls the given function for each node visited.
231 The traversal can be halted at any point by returning %TRUE from @func.
234 @root: the root #GNode of the tree to traverse.
235 @order: the order in which nodes are visited - %G_IN_ORDER, %G_PRE_ORDER,
236 %G_POST_ORDER, or %G_LEVEL_ORDER.
237 @flags: which types of children are to be visited, one of %G_TRAVERSE_ALL,
238 %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES.
239 @max_depth: the maximum depth of the traversal. Nodes below this
240 depth will not be visited. If max_depth is -1 all nodes in the tree are
241 visited. If depth is 1, only the root is visited. If depth is 2, the root
242 and its children are visited. And so on.
243 @func: the function to call for each visited #GNode.
244 @data: user data to pass to the function.
247 <!-- ##### ENUM GTraverseFlags ##### -->
249 Specifies which nodes are visited during several of the tree functions,
250 including g_node_traverse() and g_node_find().
253 @G_TRAVERSE_LEAVES: only leaf nodes should be visited. This name has been
254 introduced in 2.6, for older version use %G_TRAVERSE_LEAFS.
255 @G_TRAVERSE_NON_LEAVES: only non-leaf nodes should be visited. This name
256 has been introduced in 2.6, for older version use %G_TRAVERSE_NON_LEAFS.
257 @G_TRAVERSE_ALL: all nodes should be visited.
259 @G_TRAVERSE_LEAFS: identical to %G_TRAVERSE_LEAVES
260 @G_TRAVERSE_NON_LEAFS: identical to %G_TRAVERSE_NON_LEAVES
262 <!-- ##### USER_FUNCTION GNodeTraverseFunc ##### -->
264 Specifies the type of function passed to g_node_traverse().
265 The function is called with each of the nodes visited, together with the
266 user data passed to g_node_traverse().
267 If the function returns %TRUE, then the traversal is stopped.
271 @data: user data passed to g_node_traverse().
272 @Returns: %TRUE to stop the traversal.
275 <!-- ##### FUNCTION g_node_children_foreach ##### -->
277 Calls a function for each of the children of a #GNode.
278 Note that it doesn't descend beneath the child nodes.
282 @flags: which types of children are to be visited, one of %G_TRAVERSE_ALL,
283 %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES.
284 @func: the function to call for each visited node.
285 @data: user data to pass to the function.
288 <!-- ##### USER_FUNCTION GNodeForeachFunc ##### -->
290 Specifies the type of function passed to g_node_children_foreach().
291 The function is called with each child node, together with the user data
292 passed to g_node_children_foreach().
296 @data: user data passed to g_node_children_foreach().
299 <!-- ##### FUNCTION g_node_get_root ##### -->
301 Gets the root of a tree.
305 @Returns: the root of the tree.
308 <!-- ##### FUNCTION g_node_find ##### -->
310 Finds a #GNode in a tree.
313 @root: the root #GNode of the tree to search.
314 @order: the order in which nodes are visited - %G_IN_ORDER, %G_PRE_ORDER,
315 %G_POST_ORDER, or %G_LEVEL_ORDER.
316 @flags: which types of children are to be searched, one of %G_TRAVERSE_ALL,
317 %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES.
318 @data: the data to find.
319 @Returns: the found #GNode, or %NULL if the data is not found.
322 <!-- ##### FUNCTION g_node_find_child ##### -->
324 Finds the first child of a #GNode with the given data.
328 @flags: which types of children are to be searched, one of %G_TRAVERSE_ALL,
329 %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES.
330 @data: the data to find.
331 @Returns: the found child #GNode, or %NULL if the data is not found.
334 <!-- ##### FUNCTION g_node_child_index ##### -->
336 Gets the position of the first child of a #GNode which contains the given data.
340 @data: the data to find.
341 @Returns: the index of the child of @node which contains @data, or -1
342 if the data is not found.
345 <!-- ##### FUNCTION g_node_child_position ##### -->
347 Gets the position of a #GNode with respect to its siblings.
348 @child must be a child of @node.
349 The first child is numbered 0, the second 1, and so on.
353 @child: a child of @node.
354 @Returns: the position of @child with respect to its siblings.
357 <!-- ##### MACRO g_node_first_child ##### -->
359 Gets the first child of a #GNode.
363 @Returns: the last child of @node, or %NULL if @node is %NULL or has no children.
366 <!-- ##### FUNCTION g_node_last_child ##### -->
368 Gets the last child of a #GNode.
371 @node: a #GNode (must not be %NULL).
372 @Returns: the last child of @node, or %NULL if @node has no children.
375 <!-- ##### FUNCTION g_node_nth_child ##### -->
377 Gets a child of a #GNode, using the given index.
378 The first child is at index 0. If the index is too big, %NULL is returned.
382 @n: the index of the desired child.
383 @Returns: the child of @node at index @n.
386 <!-- ##### FUNCTION g_node_first_sibling ##### -->
388 Gets the first sibling of a #GNode.
389 This could possibly be the node itself.
393 @Returns: the first sibling of @node.
396 <!-- ##### MACRO g_node_next_sibling ##### -->
398 Gets the next sibling of a #GNode.
402 @Returns: the next sibling of @node, or %NULL if @node is %NULL.
405 <!-- ##### MACRO g_node_prev_sibling ##### -->
407 Gets the previous sibling of a #GNode.
411 @Returns: the previous sibling of @node, or %NULL if @node is %NULL.
414 <!-- ##### FUNCTION g_node_last_sibling ##### -->
416 Gets the last sibling of a #GNode.
417 This could possibly be the node itself.
421 @Returns: the last sibling of @node.
424 <!-- ##### MACRO G_NODE_IS_LEAF ##### -->
426 Returns %TRUE if a #GNode is a leaf node.
430 @Returns: %TRUE if the #GNode is a leaf node (i.e. it has no children).
433 <!-- ##### MACRO G_NODE_IS_ROOT ##### -->
435 Returns %TRUE if a #GNode is the root of a tree.
439 @Returns: %TRUE if the #GNode is the root of a tree (i.e. it has no parent
443 <!-- ##### FUNCTION g_node_depth ##### -->
445 Gets the depth of a #GNode.
448 If @node is %NULL the depth is 0.
449 The root node has a depth of 1.
450 For the children of the root node the depth is 2. And so on.
454 @Returns: the depth of the #GNode.
457 <!-- ##### FUNCTION g_node_n_nodes ##### -->
459 Gets the number of nodes in a tree.
463 @flags: which types of children are to be counted, one of %G_TRAVERSE_ALL,
464 %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES.
465 @Returns: the number of nodes in the tree.
468 <!-- ##### FUNCTION g_node_n_children ##### -->
470 Gets the number of children of a #GNode.
474 @Returns: the number of children of @node.
477 <!-- ##### FUNCTION g_node_is_ancestor ##### -->
479 Returns %TRUE if @node is an ancestor of @descendant.
480 This is true if node is the parent of @descendant, or if node is the
481 grandparent of @descendant etc.
485 @descendant: a #GNode.
486 @Returns: %TRUE if @node is an ancestor of @descendant.
489 <!-- ##### FUNCTION g_node_max_height ##### -->
491 Gets the maximum height of all branches beneath a #GNode.
492 This is the maximum distance from the #GNode to all leaf nodes.
495 If @root is %NULL, 0 is returned. If @root has no children, 1 is returned.
496 If @root has children, 2 is returned. And so on.
500 @Returns: the maximum height of the tree beneath @root.
503 <!-- ##### FUNCTION g_node_unlink ##### -->
505 Unlinks a #GNode from a tree, resulting in two separate trees.
508 @node: the #GNode to unlink, which becomes the root of a new tree.
511 <!-- ##### FUNCTION g_node_destroy ##### -->
513 Removes the #GNode and its children from the tree, freeing any memory
517 @root: the root of the tree/subtree to destroy.
520 <!-- ##### FUNCTION g_node_push_allocator ##### -->
522 Sets the allocator to use to allocate #GNode elements.
523 Use g_node_pop_allocator() to restore the previous allocator.
526 Note that this function is not available if GLib has been compiled
527 with <option>--disable-mem-pools</option>
530 @dummy: the #GAllocator to use when allocating #GNode elements.
531 @Deprecated: Since 2.10, it does nothing, since #GNode has been
532 converted to the <link linkend="glib-Memory-Slices">slice allocator</link>
535 <!-- ##### FUNCTION g_node_pop_allocator ##### -->
537 Restores the previous #GAllocator, used when allocating #GNode elements.
540 Note that this function is not available if GLib has been compiled
541 with <option>--disable-mem-pools</option>
544 @Deprecated: Since 2.10, it does nothing, since #GNode has been
545 converted to the <link linkend="glib-Memory-Slices">slice allocator</link>