colour.utilities.TreeNode#

class colour.utilities.TreeNode(*args: Any, **kwargs: Any)[source]#

Bases: object

Define a basic node supporting the creation of hierarchical node trees.

Parameters:
  • name – Node name.

  • parent – Parent of the node.

  • children – Children of the node.

  • data – Data belonging to this node.

  • args (Any)

  • kwargs (Any)

Return type:

Self

Attributes

Methods

Examples

>>> node_a = TreeNode("Node A")
>>> node_b = TreeNode("Node B", node_a)
>>> node_c = TreeNode("Node C", node_a)
>>> node_d = TreeNode("Node D", node_b)
>>> node_e = TreeNode("Node E", node_b)
>>> node_f = TreeNode("Node F", node_d)
>>> node_g = TreeNode("Node G", node_f)
>>> node_h = TreeNode("Node H", node_g)
>>> [node.name for node in node_a.leaves]
['Node H', 'Node E', 'Node C']
>>> print(node_h.root.name)
Node A
>>> len(node_a)
7

Return a new instance of the colour.utilities.TreeNode class.

Parameters:
  • args (Any) – Arguments.

  • kwargs (Any) – Keywords arguments.

Return type:

Self

static __new__(cls, *args: Any, **kwargs: Any) Self[source]#

Return a new instance of the colour.utilities.TreeNode class.

Parameters:
  • args (Any) – Arguments.

  • kwargs (Any) – Keywords arguments.

Return type:

Self

__init__(name: str | None = None, parent: Self | None = None, children: List[Self] | None = None, data: Any | None = None) None[source]#
Parameters:
Return type:

None

property id: int#

Getter for the node identifier.

Returns:

Node identifier.

Return type:

int

property name: str#

Getter and setter for the node name.

Parameters:

value – Value to set the node name with.

Returns:

Node name.

Return type:

str

property parent: Self | None#

Getter and setter for the node parent.

Parameters:

value – Parent to set the node with.

Returns:

Node parent.

Return type:

TreeNode or None

property children: List[Self]#

Getter and setter for the node children.

Parameters:

value – Children to set the node with.

Returns:

Node children.

Return type:

list

property root: Self#

Getter for the root node of the tree hierarchy.

Returns:

Root node of the tree.

Return type:

TreeNode

property leaves: Generator#

Getter for all leaf nodes in the hierarchy.

Returns:

Generator yielding all leaf nodes (nodes without children) in the hierarchy.

Return type:

Generator

property siblings: Generator#

Getter for the sibling nodes at the same hierarchical level.

Returns:

Generator yielding sibling nodes that share the same parent node in the hierarchy.

Return type:

Generator

property data: Any#

Getter and setter for the node data.

Parameters:

value – Data to assign to the node.

Returns:

Data stored in the node.

Return type:

object

__str__() str[source]#

Return a formatted string representation of the node.

Returns:

Formatted string representation.

Return type:

str

__len__() int[source]#

Return the number of children of the node.

Returns:

Number of children of the node.

Return type:

int

is_root() bool[source]#

Determine whether the node is a root node.

Returns:

Whether the node is a root node.

Return type:

bool

Examples

>>> node_a = TreeNode("Node A")
>>> node_b = TreeNode("Node B", node_a)
>>> node_c = TreeNode("Node C", node_b)
>>> node_a.is_root()
True
>>> node_b.is_root()
False
is_inner() bool[source]#

Determine whether the node is an inner node.

Returns:

Whether the node is an inner node.

Return type:

bool

Examples

>>> node_a = TreeNode("Node A")
>>> node_b = TreeNode("Node B", node_a)
>>> node_c = TreeNode("Node C", node_b)
>>> node_a.is_inner()
False
>>> node_b.is_inner()
True
is_leaf() bool[source]#

Determine whether the node is a leaf node.

Returns:

Whether the node is a leaf node.

Return type:

bool

Examples

>>> node_a = TreeNode("Node A")
>>> node_b = TreeNode("Node B", node_a)
>>> node_c = TreeNode("Node C", node_b)
>>> node_a.is_leaf()
False
>>> node_c.is_leaf()
True
walk_hierarchy(ascendants: bool = False) Generator[source]#

Generate a generator to walk the colour.utilities.TreeNode tree hierarchy.

Parameters:

ascendants (bool) – Whether to walk up the node tree.

Yields:

Generator – Node tree walker.

Return type:

Generator

Examples

>>> node_a = TreeNode("Node A")
>>> node_b = TreeNode("Node B", node_a)
>>> node_c = TreeNode("Node C", node_a)
>>> node_d = TreeNode("Node D", node_b)
>>> node_e = TreeNode("Node E", node_b)
>>> node_f = TreeNode("Node F", node_d)
>>> node_g = TreeNode("Node G", node_f)
>>> node_h = TreeNode("Node H", node_g)
>>> for node in node_a.walk_hierarchy():
...     print(node.name)
Node B
Node D
Node F
Node G
Node H
Node E
Node C
render(tab_level: int = 0) str[source]#

Render the node and its children as a formatted tree string.

Parameters:

tab_level (int) – Initial indentation level for the tree structure.

Returns:

Formatted tree representation of the node hierarchy.

Return type:

str

Examples

>>> node_a = TreeNode("Node A")
>>> node_b = TreeNode("Node B", node_a)
>>> node_c = TreeNode("Node C", node_a)
>>> print(node_a.render())
|----"Node A"
    |----"Node B"
    |----"Node C"
__weakref__#

list of weak references to the object