justree.tree module

class justree.tree.Tree(value: Any, children: Iterable[Tree] = ())[source]

Tree implementation that represent tree node with value and children nodes

Iterating through children:

>>> tree_node = Tree(...)
>>> for child_node in tree_node:
        ...
__copy__() → justree.tree.Tree[source]

Shallow copy method implementation

Returns

copy of tree with preserved values references

__deepcopy__(memo=None) → justree.tree.Tree[source]

Deep copy method implementation

Returns

copy of tree with deep copied values

__eq__(o: object) → bool[source]

Overloaded equation method

Parameters

o – another Tree object to check equation with

Returns

boolean result of equation

__hash__() → int[source]

Overloaded hash method, working only on frozen tree (see freeze())

Returns

hash value

__init__(value: Any, children: Iterable[Tree] = ()) → None[source]

Create an instance of a tree

Parameters
  • value – value to store in tree root node

  • children – optional list of child tree nodes

Example
>>> t = Tree('Hello World!')
>>> print(t)
(Hello World!)
__len__() → int[source]
Returns

number of children of the tree root node

__ne__(o: object) → bool[source]

Overloaded not-equation method

Parameters

o – another Tree object to check not-equation with

Returns

boolean result of not-equation

__repr__() → str[source]

Overloaded repr method

Returns

string representation of Tree object that is the valid Python expression

__str__() → str[source]

Overloaded string method

Returns

string representation of Tree object

append(tree: justree.tree.Tree) → None[source]

Add another tree to the current tree’s children list

Parameters

tree – Tree object to append in current tree’s children list

bfs(reverse: bool = False, mirror: bool = False) → Iterable[justree.tree.Tree][source]

Breadth First Search

Parameters
  • reverse – reverse resulting order of nodes (require O(n) memory)

  • mirror – used reversed children order on whole tree

Returns

nodes in requested order

bfs_ex(depth: Optional[int] = None, reverse: bool = False, mirror: bool = False) → Iterable[Tuple[justree.tree.Tree, int, Tuple[int, ...]]][source]

Breadth First Search appended with nodes positions

Parameters
  • depth – limit search with max allowed depth

  • reverse – reverse resulting order of nodes (require O(n) memory)

  • mirror – used reversed children order on whole tree

Returns

nodes in requested order

clone(deep=False) → justree.tree.Tree[source]

Clone the tree

Parameters

deep – cloning not only tree’s nodes but also nodes value

Returns

clone of a tree

dfs(reverse: bool = False, mirror: bool = False, post_order: bool = False) → Iterable[justree.tree.Tree][source]

Depth First Search

Parameters
  • reverse – reverse resulting order of nodes (require twice more time)

  • mirror – used reversed children order on whole tree

  • post_order – taking node on leaving (usually on entering) (require twice more time, incompatible with param reverse)

Returns

nodes in requested order

dfs_ex(depth: Optional[int] = None, reverse: bool = False, mirror: bool = False, post_order: bool = False) → Iterable[Tuple[justree.tree.Tree, int, Tuple[int, ...]]][source]

Depth First Search appended with nodes positions

Parameters
  • depth – limit search with max allowed depth

  • reverse – reverse resulting order of nodes (require twice more time)

  • mirror – used reversed children order on whole tree

  • post_order – taking node on leaving (usually on entering) (require twice more time, incompatible with param reverse)

Returns

nodes in requested order

freeze() → None[source]

Make tree readonly in place (to make it writable again use unfreeze())

classmethod from_tuple(itr: Tuple[Any, Iterable[Tuple]]) → justree.tree.Tree[source]

Convert flat structure into tree.

Parameters

itr – flat structure of tree

Returns

tree with the same order of elements

height() → int[source]
Returns

height of tree

insert(index: Union[int, Tuple[int, ...]], tree: justree.tree.Tree) → None[source]

Insert another tree as child into root node or sub-node children list

Parameters
  • index – int index of root node child position or tuple of int indexes of sub-node child position

  • treeTree object

size() → int[source]
Returns

number of tree nodes

to_tuple() → Tuple[Any, Iterable[Tuple]][source]

Convert tree into flat structure.

Returns

flat structure with the same order of elements

unfreeze(unsafe: bool = False, deep: bool = False) → justree.tree.Tree[source]

Make writable copy of tree (opposite for freeze())

Parameters
  • unsafe – convert Tree to writable in place

  • deep – cloning not only Tree’s nodes but also nodes value

Returns

writable tree

value: Any = None

Value of a tree node