mode.utils.trees

Data structure: Trees.

class mode.utils.trees.Node(data: _T, *, root: mode.utils.types.trees.NodeT = None, parent: mode.utils.types.trees.NodeT = None, children: List[mode.utils.types.trees.NodeT[_T]] = None)[source]

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.

new(data: _T) → mode.utils.types.trees.NodeT[source]

Create new node from this node.

reattach(parent: mode.utils.types.trees.NodeT[_T]) → mode.utils.types.trees.NodeT[_T][source]

Attach this node to parent node.

detach(parent: mode.utils.types.trees.NodeT[_T]) → mode.utils.types.trees.NodeT[_T][source]

Detach this node from parent node.

add(data: _T) → None[source]

Add node as a child node.

discard(data: _T) → None[source]

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

traverse() → Iterator[mode.utils.types.trees.NodeT[_T]][source]

Iterate over the tree in BFS order.

walk() → Iterator[mode.utils.types.trees.NodeT[_T]][source]

Iterate over hierarchy backwards.

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

as_graph() → mode.utils.types.graphs.DependencyGraphT[source]

Convert to DependencyGraph.

property depth
property path
property parent
property root