Description
The GNode struct and its associated functions provide a N-ary tree data
structure, where nodes in the tree can contain arbitrary data.
To create a new tree use g_node_new().
To insert a node into a tree use g_node_insert(), g_node_insert_before(),
g_node_append() and g_node_prepend().
To create a new node and insert it into a tree use g_node_insert_data(),
g_node_insert_data_before(), g_node_append_data() and g_node_prepend_data().
To reverse the children of a node use g_node_reverse_children().
To find a node use g_node_get_root(), g_node_find(), g_node_find_child(),
g_node_child_index(), g_node_child_position(),
g_node_first_child(), g_node_last_child(),
g_node_nth_child(), g_node_first_sibling(), g_node_prev_sibling(),
g_node_next_sibling() or g_node_last_sibling().
To get information about a node or tree use G_NODE_IS_LEAF(),
G_NODE_IS_ROOT(), g_node_depth(), g_node_n_nodes(), g_node_n_children(),
g_node_is_ancestor() or g_node_max_height().
To traverse a tree, calling a function for each node visited in the
traversal, use g_node_traverse() or g_node_children_foreach().
To remove a node or subtree from a tree use g_node_unlink() or
g_node_destroy().
Details
struct GNode
struct GNode
{
gpointer data;
GNode *next;
GNode *prev;
GNode *parent;
GNode *children;
}; |
The GNode struct represents one node in a
N-ary Tree.
The data field contains the actual data of the node.
The next and prev
fields point to the node's siblings (a sibling is another GNode with the
same parent).
The parent field points to the parent of the GNode,
or is NULL if the GNode is the root of the tree.
The children field points to the first child of the
GNode. The other children are accessed by using the
next pointer of each child.
g_node_new ()
Creates a new GNode containing the given data.
Used to create the first node in a tree.
g_node_insert ()
Inserts a GNode beneath the parent at the given position.
g_node_insert_before ()
Inserts a GNode beneath the parent before the given sibling.
g_node_append()
#define g_node_append(parent, node) |
Inserts a GNode as the last child of the given parent.
g_node_prepend ()
Inserts a GNode as the first child of the given parent.
g_node_insert_data()
#define g_node_insert_data(parent, position, data) |
Inserts a new GNode at the given position.
g_node_insert_data_before()
#define g_node_insert_data_before(parent, sibling, data) |
Inserts a new GNode before the given sibling.
g_node_append_data()
#define g_node_append_data(parent, data) |
Inserts a new GNode as the last child of the given parent.
g_node_prepend_data()
#define g_node_prepend_data(parent, data) |
Inserts a new GNode as the first child of the given parent.
g_node_reverse_children ()
void g_node_reverse_children (GNode *node); |
Reverses the order of the children of a GNode.
(It doesn't change the order of the grandchildren.)
g_node_traverse ()
Traverses a tree starting at the given root GNode.
It calls the given function for each node visited.
The traversal can be halted at any point by returning TRUE from func.
enum GTraverseFlags
typedef enum
{
G_TRAVERSE_LEAFS = 1 << 0,
G_TRAVERSE_NON_LEAFS = 1 << 1,
G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
G_TRAVERSE_MASK = 0x03
} GTraverseFlags; |
Specifies which nodes are visited during several of the tree functions,
including g_node_traverse() and g_node_find().
G_TRAVERSE_LEAFS specifies that only leaf nodes should be visited.
G_TRAVERSE_NON_LEAFS specifies that only non-leaf nodes should be visited.
G_TRAVERSE_ALL specifies that all nodes should be visited.
GNodeTraverseFunc ()
Specifies the type of function passed to g_node_traverse().
The function is called with each of the nodes visited, together with the
user data passed to g_node_traverse().
If the function returns TRUE, then the traversal is stopped.
g_node_children_foreach ()
Calls a function for each of the children of a GNode.
Note that it doesn't descend beneath the child nodes.
g_node_get_root ()
Gets the root of a tree.
g_node_find ()
Finds a GNode in a tree.
g_node_find_child ()
Finds the first child of a GNode with the given data.
g_node_child_index ()
Gets the position of the first child of a GNode which contains the given data.
g_node_child_position ()
Gets the position of a GNode with respect to its siblings.
child must be a child of node.
The first child is numbered 0, the second 1, and so on.
g_node_first_child()
#define g_node_first_child(node) |
Gets the first child of a GNode.
g_node_last_child ()
Gets the last child of a GNode.
g_node_nth_child ()
Gets a child of a GNode, using the given index.
The first child is at index 0. If the index is too big, NULL is returned.
g_node_first_sibling ()
Gets the first sibling of a GNode.
This could possibly be the node itself.
g_node_next_sibling()
#define g_node_next_sibling(node) |
Gets the next sibling of a GNode.
g_node_prev_sibling()
#define g_node_prev_sibling(node) |
Gets the previous sibling of a GNode.
g_node_last_sibling ()
Gets the last sibling of a GNode.
This could possibly be the node itself.
G_NODE_IS_LEAF()
#define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL) |
Returns TRUE if a GNode is a leaf node.
G_NODE_IS_ROOT()
#define G_NODE_IS_ROOT(node) |
Returns TRUE if a GNode is the root of a tree.
g_node_depth ()
Gets the depth of a GNode.
If node is NULL the depth is 0.
The root node has a depth of 1.
For the children of the root node the depth is 2. And so on.
g_node_n_nodes ()
Gets the number of nodes in a tree.
g_node_n_children ()
Gets the number of children of a GNode.
g_node_is_ancestor ()
Returns TRUE if node is an ancestor of descendant.
This is true if node is the parent of descendant, or if node is the
grandparent of descendant etc.
g_node_max_height ()
Gets the maximum height of all branches beneath a GNode.
This is the maximum distance from the GNode to all leaf nodes.
If root is NULL, 0 is returned. If root has no children, 1 is returned.
If root has children, 2 is returned. And so on.
g_node_unlink ()
void g_node_unlink (GNode *node); |
Unlinks a GNode from a tree, resulting in two separate trees.
g_node_destroy ()
void g_node_destroy (GNode *root); |
Removes the GNode and its children from the tree, freeing any memory
allocated.
g_node_push_allocator ()
void g_node_push_allocator (GAllocator *allocator); |
Sets the allocator to use to allocate GNode elements.
Use g_node_pop_allocator() to restore the previous allocator.
g_node_pop_allocator ()
void g_node_pop_allocator (void); |
Restores the previous GAllocator, used when allocating GNode elements.