Node#
- class Node(*args, **kwargs)#
The Node
struct represents one node in a [n-ary tree][glib-N-ary-Trees].
Methods#
- class Node
- child_index(data: None) int #
Gets the position of the first child of a
Node
which contains the given data.- Parameters:
data – the data to find
- child_position(child: Node) int #
Gets the position of a
Node
with respect to its siblings.child
must be a child ofnode
. The first child is numbered 0, the second 1, and so on.- Parameters:
child – a child of
node
- children_foreach(flags: TraverseFlags, func: Callable[[...], None], *data: Any) None #
Calls a function for each of the children of a
Node
. Note that it doesn’t descend beneath the child nodes.func
must not do anything that would modify the structure of the tree.- Parameters:
flags – which types of children are to be visited, one of
ALL
,LEAVES
andNON_LEAVES
func – the function to call for each visited node
data – user data to pass to the function
- depth() int #
Gets the depth of a
Node
.If
node
isNone
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.
- is_ancestor(descendant: Node) bool #
Returns
True
ifnode
is an ancestor ofdescendant
. This is true if node is the parent ofdescendant
, or if node is the grandparent ofdescendant
etc.- Parameters:
descendant – a
Node
- max_height() int #
Gets the maximum height of all branches beneath a
Node
. This is the maximum distance from theNode
to all leaf nodes.If
root
isNone
, 0 is returned. Ifroot
has no children, 1 is returned. Ifroot
has children, 2 is returned. And so on.
- n_nodes(flags: TraverseFlags) int #
Gets the number of nodes in a tree.
- Parameters:
flags – which types of children are to be counted, one of
ALL
,LEAVES
andNON_LEAVES
- reverse_children() None #
Reverses the order of the children of a
Node
. (It doesn’t change the order of the grandchildren.)
- traverse(order: TraverseType, flags: TraverseFlags, max_depth: int, func: Callable[[...], bool], *data: Any) None #
Traverses a tree starting at the given root
Node
. It calls the given function for each node visited. The traversal can be halted at any point by returningTrue
fromfunc
.func
must not do anything that would modify the structure of the tree.- Parameters:
order – the order in which nodes are visited -
IN_ORDER
,PRE_ORDER
,POST_ORDER
, orLEVEL_ORDER
.flags – which types of children are to be visited, one of
ALL
,LEAVES
andNON_LEAVES
max_depth – the maximum depth of the traversal. Nodes below this depth will not be visited. If max_depth is -1 all nodes in the tree are visited. If depth is 1, only the root is visited. If depth is 2, the root and its children are visited. And so on.
func – the function to call for each visited
Node
data – user data to pass to the function