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: Any = None) int#
Gets the position of the first child of a
Nodewhich contains the given data.- Parameters:
data – the data to find
- child_position(child: Node) int#
Gets the position of a
Nodewith respect to its siblings.childmust 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[[Node, Any], None], data: Any = None) None#
Calls a function for each of the children of a
Node. Note that it doesn’t descend beneath the child nodes.funcmust not do anything that would modify the structure of the tree.- Parameters:
flags – which types of children are to be visited, one of
ALL,LEAVESandNON_LEAVESfunc – 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
nodeisNonethe 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
Trueifnodeis an ancestor ofdescendant. This is true if node is the parent ofdescendant, or if node is the grandparent ofdescendantetc.- Parameters:
descendant – a
Node
- max_height() int#
Gets the maximum height of all branches beneath a
Node. This is the maximum distance from theNodeto all leaf nodes.If
rootisNone, 0 is returned. Ifroothas no children, 1 is returned. Ifroothas 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,LEAVESandNON_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[[Node, Any], bool], data: Any = None) 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 returningTruefromfunc.funcmust 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,LEAVESandNON_LEAVESmax_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
Nodedata – user data to pass to the function