tree_sorting
Class and functions for building binary trees.
- class Node(i)[source]
Bases:
object
Class encapsulating a node in a binary tree.
- Parameters:
i (any) – Value of this node.
- value
Value of this node.
- Type:
any
- value_tuple
Tuple of the value of this node and the values of its children.
- Type:
tuple
- children
List of the children of this node.
- Type:
List
- leaves
List of the leaves of this node.
- Type:
List
- is_sorted
Whether the children of this node are sorted.
- Type:
bool
- tree_id
Identifier of the tree this node belongs to.
- Type:
any
- check_sorted(sort_depth=1)[source]
Check if the children of this node are sorted.
- Parameters:
sort_depth (int) – Depth to check sorting at.
- Returns:
is_sorted – Whether the children of this node are sorted.
- Return type:
bool
- get_current_value_tuple()[source]
Return current value tuple of this node.
- Returns:
this_tup – Tuple of the value of this node and the values of its children.
- Return type:
tuple
- return_children_vals(depth=1)[source]
Return the values of the children of this node.
- Parameters:
depth (int) – Depth up to which to return values.
- Returns:
vals – Values of the children of this node.
- Return type:
tuple
- set_children(children, set_sorted=False, sort_depth=1)[source]
Set the children of this node.
- Parameters:
children (List or tuple) – Children to be set.
set_sorted (bool) – If True, set the children in a sorted fashion.
sort_depth (int) – Depth to sort children at, if set_sorted is True.
- update_children(set_sorted=False, sort_depth=1)[source]
Update the children of this node.
Essentially calls set_children with children already attached to node.
- Parameters:
set_sorted (bool) – If True, set the children in a sorted fashion.
sort_depth (int) – Depth to sort children at, if set_sorted is True.
- build_full_tree(l, L, L_R)[source]
Build a full binary tree from the given lists.
Works for rank 4, 5 and 6.
- Parameters:
l (List) – list (multiset) of angular momentum indices l1,l2,…lN
L (List) – list (multiset) of intermediate angular momentum indices l1,l2,…lN
L_R (int) – Resultant angular momentum quantum number. This determines the equivariant character of the rank N descriptor after reduction. L_R=0 corresponds to a rotationally invariant feature, L_R=1 corresponds to a feature that transforms like a vector, L_R=2 a tensor, etc.
- Returns:
root – Root node of the constructed tree.
- Return type:
- build_quick_tree(l)[source]
Build a binary tree from the given list.
Rank is assumed to be the length of the list. If the rank is odd, a remainder is returned.
- Parameters:
l (List) – List to build the tree from.
- Returns:
tree – Tuple containing the tree and the remainder which could not fit in the tree.
- Return type:
tuple