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 of node. 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 and NON_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 is None 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.

destroy() None

Removes root and its children from the tree, freeing any memory allocated.

is_ancestor(descendant: Node) bool

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.

Parameters:

descendant – a Node

max_height() int

Gets the maximum height of all branches beneath a Node. This is the maximum distance from the Node to all leaf nodes.

If root is None, 0 is returned. If root has no children, 1 is returned. If root has children, 2 is returned. And so on.

n_children() int

Gets the number of children of a Node.

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 and NON_LEAVES

pop_allocator() None
push_allocator(allocator: Allocator) None
Parameters:

allocator

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 returning True from func. 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, or LEVEL_ORDER.

  • flags – which types of children are to be visited, one of ALL, LEAVES and NON_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

Unlinks a Node from a tree, resulting in two separate trees.

Returns:

0 if the name was successfully deleted, -1 if an error occurred

Fields

class Node
children

Points to the first child of the Node. The other children are accessed by using the next pointer of each child.

data

Contains the actual data of the node.

next

Points to the node’s next sibling (a sibling is another Node with the same parent).

parent

Points to the parent of the Node, or is None if the Node is the root of the tree.

prev

Points to the node’s previous sibling.