mode.utils.trees

Data structure: Trees.

class mode.utils.trees.Node(data: T, *, root: Optional[NodeT] = None, parent: Optional[NodeT] = None, children: Optional[list[mode.utils.types.trees.NodeT[T]]] = None)

Tree node.

Notes

Nodes have a link to

  • the .root node (or None if this is the top-most node)

  • the .parent node (if this is a child node).

  • a list of children

A Node may have arbitrary .data associated with it, and arbitrary data may also be stored in .children.

Parameters:

data (Any) – Data to associate with node.

Keyword Arguments:
  • root (NodeT) – Root node.

  • parent (NodeT) – Parent node.

  • children (list[NodeT]) – List of child nodes.

add(data: Union[T, NodeT[T]]) None

Add node as a child node.

add_deduplicate(data: Union[T, NodeT[T]]) None
as_graph() DependencyGraphT

Convert to DependencyGraph.

property depth: int
detach(parent: NodeT[T]) NodeT[T]

Detach this node from parent node.

discard(data: T) None

Remove node so it’s no longer a child of this node.

new(data: T) NodeT

Create new node from this node.

property parent: Optional[NodeT]
property path: str
reattach(parent: NodeT[T]) NodeT[T]

Attach this node to parent node.

property root: Optional[NodeT]
traverse() Iterator[NodeT[T]]

Iterate over the tree in BFS order.

walk() Iterator[NodeT[T]]

Iterate over hierarchy backwards.

This will yield parent nodes all the way up to the root.