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 #GNode 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 #GNode with the
60 The <structfield>parent</structfield> field points to the parent of the #GNode,
61 or is NULL if the #GNode is the root of the tree.
62 The <structfield>children</structfield> field points to the first child of the
63 #GNode. 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_insert ##### -->
85 Inserts a #GNode beneath the parent at the given position.
88 @parent: the #GNode to place @node under.
89 @position: the position to place @node at, with respect to its siblings.
90 If position is -1, @node is inserted as the last child of @parent.
91 @node: the #GNode to insert.
92 @Returns: the inserted #GNode.
95 <!-- ##### FUNCTION g_node_insert_before ##### -->
97 Inserts a #GNode beneath the parent before the given sibling.
100 @parent: the #GNode to place @node under.
101 @sibling: the sibling #GNode to place @node before. If sibling is NULL,
102 the node is inserted as the last child of @parent.
103 @node: the #GNode to insert.
104 @Returns: the inserted #GNode.
107 <!-- ##### MACRO g_node_append ##### -->
109 Inserts a #GNode as the last child of the given parent.
112 @parent: the #GNode to place the new #GNode under.
113 @node: the #GNode to insert.
114 @Returns: the inserted #GNode.
117 <!-- ##### FUNCTION g_node_prepend ##### -->
119 Inserts a #GNode as the first child of the given parent.
122 @parent: the #GNode to place the new #GNode under.
123 @node: the #GNode to insert.
124 @Returns: the inserted #GNode.
127 <!-- ##### MACRO g_node_insert_data ##### -->
129 Inserts a new #GNode at the given position.
132 @parent: the #GNode to place the new #GNode under.
133 @position: the position to place the new #GNode at.
134 If position is -1, the new #GNode is inserted as the last child of @parent.
135 @data: the data for the new #GNode.
136 @Returns: the new #GNode.
139 <!-- ##### MACRO g_node_insert_data_before ##### -->
141 Inserts a new #GNode before the given sibling.
144 @parent: the #GNode to place the new #GNode under.
145 @sibling: the sibling #GNode to place the new #GNode before.
146 @data: the data for the new #GNode.
147 @Returns: the new #GNode.
150 <!-- ##### MACRO g_node_append_data ##### -->
152 Inserts a new #GNode as the last child of the given parent.
155 @parent: the #GNode to place the new #GNode under.
156 @data: the data for the new #GNode.
157 @Returns: the new #GNode.
160 <!-- ##### MACRO g_node_prepend_data ##### -->
162 Inserts a new #GNode as the first child of the given parent.
165 @parent: the #GNode to place the new #GNode under.
166 @data: the data for the new #GNode.
167 @Returns: the new #GNode.
170 <!-- ##### FUNCTION g_node_reverse_children ##### -->
172 Reverses the order of the children of a #GNode.
173 (It doesn't change the order of the grandchildren.)
179 <!-- ##### FUNCTION g_node_traverse ##### -->
181 Traverses a tree starting at the given root #GNode.
182 It calls the given function for each node visited.
183 The traversal can be halted at any point by returning TRUE from @func.
186 @root: the root #GNode of the tree to traverse.
187 @order: the order in which nodes are visited - %G_IN_ORDER, %G_PRE_ORDER,
188 %G_POST_ORDER, or %G_LEVEL_ORDER.
189 @flags: which types of children are to be visited, one of %G_TRAVERSE_ALL,
190 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
191 @max_depth: the maximum depth of the traversal. Nodes below this
192 depth will not be visited. If max_depth is -1 all nodes in the tree are
193 visited. If depth is 1, only the root is visited. If depth is 2, the root
194 and its children are visited. And so on.
195 @func: the function to call for each visited #GNode.
196 @data: user data to pass to the function.
199 <!-- ##### ENUM GTraverseFlags ##### -->
201 Specifies which nodes are visited during several of the tree functions,
202 including g_node_traverse() and g_node_find().
205 %G_TRAVERSE_LEAFS specifies that only leaf nodes should be visited.
208 %G_TRAVERSE_NON_LEAFS specifies that only non-leaf nodes should be visited.
211 %G_TRAVERSE_ALL specifies that all nodes should be visited.
217 @G_TRAVERSE_NON_LEAFS:
221 <!-- ##### USER_FUNCTION GNodeTraverseFunc ##### -->
223 Specifies the type of function passed to g_node_traverse().
224 The function is called with each of the nodes visited, together with the
225 user data passed to g_node_traverse().
226 If the function returns TRUE, then the traversal is stopped.
230 @data: user data passed to g_node_traverse().
231 @Returns: TRUE to stop the traversal.
234 <!-- ##### FUNCTION g_node_children_foreach ##### -->
236 Calls a function for each of the children of a #GNode.
237 Note that it doesn't descend beneath the child nodes.
241 @flags: which types of children are to be visited, one of %G_TRAVERSE_ALL,
242 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
243 @func: the function to call for each visited node.
244 @data: user data to pass to the function.
247 <!-- ##### USER_FUNCTION GNodeForeachFunc ##### -->
249 Specifies the type of function passed to g_node_children_foreach().
250 The function is called with each child node, together with the user data
251 passed to g_node_children_foreach().
255 @data: user data passed to g_node_children_foreach().
258 <!-- ##### FUNCTION g_node_get_root ##### -->
260 Gets the root of a tree.
264 @Returns: the root of the tree.
267 <!-- ##### FUNCTION g_node_find ##### -->
269 Finds a #GNode in a tree.
272 @root: the root #GNode of the tree to search.
273 @order: the order in which nodes are visited - %G_IN_ORDER, %G_PRE_ORDER,
274 %G_POST_ORDER, or %G_LEVEL_ORDER.
275 @flags: which types of children are to be searched, one of %G_TRAVERSE_ALL,
276 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
277 @data: the data to find.
278 @Returns: the found #GNode, or NULL if the data is not found.
281 <!-- ##### FUNCTION g_node_find_child ##### -->
283 Finds the first child of a #GNode with the given data.
287 @flags: which types of children are to be searched, one of %G_TRAVERSE_ALL,
288 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
289 @data: the data to find.
290 @Returns: the found child #GNode, or NULL if the data is not found.
293 <!-- ##### FUNCTION g_node_child_index ##### -->
295 Gets the position of the first child of a #GNode which contains the given data.
299 @data: the data to find.
300 @Returns: the index of the child of @node which contains @data, or -1
301 if the data is not found.
304 <!-- ##### FUNCTION g_node_child_position ##### -->
306 Gets the position of a #GNode with respect to its siblings.
307 @child must be a child of @node.
308 The first child is numbered 0, the second 1, and so on.
312 @child: a child of @node.
313 @Returns: the position of @child with respect to its siblings.
316 <!-- ##### MACRO g_node_first_child ##### -->
318 Gets the first child of a #GNode.
322 @Returns: the last child of @node, or NULL if @node is NULL or has no children.
325 <!-- ##### FUNCTION g_node_last_child ##### -->
327 Gets the last child of a #GNode.
330 @node: a #GNode (must not be NULL).
331 @Returns: the last child of @node, or NULL if @node has no children.
334 <!-- ##### FUNCTION g_node_nth_child ##### -->
336 Gets a child of a #GNode, using the given index.
337 The first child is at index 0. If the index is too big, NULL is returned.
341 @n: the index of the desired child.
342 @Returns: the child of @node at index @n.
345 <!-- ##### FUNCTION g_node_first_sibling ##### -->
347 Gets the first sibling of a #GNode.
348 This could possibly be the node itself.
352 @Returns: the first sibling of @node.
355 <!-- ##### MACRO g_node_next_sibling ##### -->
357 Gets the next sibling of a #GNode.
361 @Returns: the next sibling of @node, or NULL if @node is NULL.
364 <!-- ##### MACRO g_node_prev_sibling ##### -->
366 Gets the previous sibling of a #GNode.
370 @Returns: the previous sibling of @node, or NULL if @node is NULL.
373 <!-- ##### FUNCTION g_node_last_sibling ##### -->
375 Gets the last sibling of a #GNode.
376 This could possibly be the node itself.
380 @Returns: the last sibling of @node.
383 <!-- ##### MACRO G_NODE_IS_LEAF ##### -->
385 Returns TRUE if a #GNode is a leaf node.
389 @Returns: TRUE if the #GNode is a leaf node (i.e. it has no children).
392 <!-- ##### MACRO G_NODE_IS_ROOT ##### -->
394 Returns TRUE if a #GNode is the root of a tree.
398 @Returns: TRUE if the #GNode is the root of a tree (i.e. it has no parent
402 <!-- ##### FUNCTION g_node_depth ##### -->
404 Gets the depth of a #GNode.
407 If @node is NULL the depth is 0.
408 The root node has a depth of 1.
409 For the children of the root node the depth is 2. And so on.
413 @Returns: the depth of the #GNode.
416 <!-- ##### FUNCTION g_node_n_nodes ##### -->
418 Gets the number of nodes in a tree.
422 @flags: which types of children are to be counted, one of %G_TRAVERSE_ALL,
423 %G_TRAVERSE_LEAFS and %G_TRAVERSE_NON_LEAFS.
424 @Returns: the number of nodes in the tree.
427 <!-- ##### FUNCTION g_node_n_children ##### -->
429 Gets the number of children of a #GNode.
433 @Returns: the number of children of @node.
436 <!-- ##### FUNCTION g_node_is_ancestor ##### -->
438 Returns TRUE if @node is an ancestor of @descendant.
439 This is true if node is the parent of descendant, or if node is the
440 grandparent of descendant etc.
444 @descendant: a #GNode.
445 @Returns: TRUE if @node is an ancestor of @descendant.
448 <!-- ##### FUNCTION g_node_max_height ##### -->
450 Gets the maximum height of all branches beneath a #GNode.
451 This is the maximum distance from the #GNode to all leaf nodes.
454 If @root is NULL, 0 is returned. If @root has no children, 1 is returned.
455 If @root has children, 2 is returned. And so on.
459 @Returns: the maximum height of the tree beneath @root.
462 <!-- ##### FUNCTION g_node_unlink ##### -->
464 Unlinks a #GNode from a tree, resulting in two separate trees.
467 @node: the #GNode to unlink, which becomes the root of a new tree.
470 <!-- ##### FUNCTION g_node_destroy ##### -->
472 Removes the #GNode and its children from the tree, freeing any memory
476 @root: the root of the tree/subtree to destroy.
479 <!-- ##### FUNCTION g_node_pop_allocator ##### -->
481 Restores the previous #GAllocator, used when allocating #GNode elements.
486 <!-- ##### FUNCTION g_node_push_allocator ##### -->
488 Sets the allocator to use to allocate #GNode elements.
489 Use g_node_pop_allocator() to restore the previous allocator.
492 @allocator: the #GAllocator to use when allocating #GNode elements.