:right-sidebar: True Node =================================================================== .. currentmodule:: gi.repository.GLib .. class:: Node(*args, **kwargs) :no-contents-entry: The :obj:`~gi.repository.GLib.Node` struct represents one node in a [n-ary tree][glib-N-ary-Trees]. Methods ------- .. rst-class:: interim-class .. class:: Node :no-index: .. method:: child_index(data: ~typing.Any = None) -> int Gets the position of the first child of a :obj:`~gi.repository.GLib.Node` which contains the given data. :param data: the data to find .. method:: child_position(child: ~gi.repository.GLib.Node) -> int Gets the position of a :obj:`~gi.repository.GLib.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. :param child: a child of ``node`` .. method:: children_foreach(flags: ~gi.repository.GLib.TraverseFlags, func: ~typing.Callable[[~gi.repository.GLib.Node, ~typing.Any], None], data: ~typing.Any = None) -> None Calls a function for each of the children of a :obj:`~gi.repository.GLib.Node`\. Note that it doesn't descend beneath the child nodes. ``func`` must not do anything that would modify the structure of the tree. :param flags: which types of children are to be visited, one of :const:`~gi.repository.GLib.TraverseFlags.ALL`, :const:`~gi.repository.GLib.TraverseFlags.LEAVES` and :const:`~gi.repository.GLib.TraverseFlags.NON_LEAVES` :param func: the function to call for each visited node :param data: user data to pass to the function .. method:: depth() -> int Gets the depth of a :obj:`~gi.repository.GLib.Node`\. If ``node`` is :const:`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. .. method:: destroy() -> None Removes ``root`` and its children from the tree, freeing any memory allocated. .. method:: is_ancestor(descendant: ~gi.repository.GLib.Node) -> bool Returns :const:`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. :param descendant: a :obj:`~gi.repository.GLib.Node` .. method:: max_height() -> int Gets the maximum height of all branches beneath a :obj:`~gi.repository.GLib.Node`\. This is the maximum distance from the :obj:`~gi.repository.GLib.Node` to all leaf nodes. If ``root`` is :const:`None`, 0 is returned. If ``root`` has no children, 1 is returned. If ``root`` has children, 2 is returned. And so on. .. method:: n_children() -> int Gets the number of children of a :obj:`~gi.repository.GLib.Node`\. .. method:: n_nodes(flags: ~gi.repository.GLib.TraverseFlags) -> int Gets the number of nodes in a tree. :param flags: which types of children are to be counted, one of :const:`~gi.repository.GLib.TraverseFlags.ALL`, :const:`~gi.repository.GLib.TraverseFlags.LEAVES` and :const:`~gi.repository.GLib.TraverseFlags.NON_LEAVES` .. classmethod:: pop_allocator() -> None .. classmethod:: push_allocator() -> None .. method:: reverse_children() -> None Reverses the order of the children of a :obj:`~gi.repository.GLib.Node`\. (It doesn't change the order of the grandchildren.) .. method:: traverse(order: ~gi.repository.GLib.TraverseType, flags: ~gi.repository.GLib.TraverseFlags, max_depth: int, func: ~typing.Callable[[~gi.repository.GLib.Node, ~typing.Any], bool], data: ~typing.Any = None) -> None Traverses a tree starting at the given root :obj:`~gi.repository.GLib.Node`\. It calls the given function for each node visited. The traversal can be halted at any point by returning :const:`True` from ``func``\. ``func`` must not do anything that would modify the structure of the tree. :param order: the order in which nodes are visited - :const:`~gi.repository.GLib.TraverseType.IN_ORDER`, :const:`~gi.repository.GLib.TraverseType.PRE_ORDER`, :const:`~gi.repository.GLib.TraverseType.POST_ORDER`, or :const:`~gi.repository.GLib.TraverseType.LEVEL_ORDER`. :param flags: which types of children are to be visited, one of :const:`~gi.repository.GLib.TraverseFlags.ALL`, :const:`~gi.repository.GLib.TraverseFlags.LEAVES` and :const:`~gi.repository.GLib.TraverseFlags.NON_LEAVES` :param 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. :param func: the function to call for each visited :obj:`~gi.repository.GLib.Node` :param data: user data to pass to the function .. method:: unlink() -> None Unlinks a :obj:`~gi.repository.GLib.Node` from a tree, resulting in two separate trees. :return: 0 if the name was successfully deleted, -1 if an error occurred Fields ------ .. rst-class:: interim-class .. class:: Node :no-index: .. attribute:: children Points to the first child of the :obj:`~gi.repository.GLib.Node`\. The other children are accessed by using the ``next`` pointer of each child. .. attribute:: data Contains the actual data of the node. .. attribute:: next Points to the node's next sibling (a sibling is another :obj:`~gi.repository.GLib.Node` with the same parent). .. attribute:: parent Points to the parent of the :obj:`~gi.repository.GLib.Node`\, or is :const:`None` if the :obj:`~gi.repository.GLib.Node` is the root of the tree. .. attribute:: prev Points to the node's previous sibling.