pygsti.layouts.prefixtable.ChildNode#
- class ChildNode(value, parent=None, children=None, orig_indices=None)#
Bases:
TreeNodeClass for representing a child node for a tree, along with the corresponding metadata specific to child nodes.
- Parameters:
value (any) – The value to be stored in the node.
parent (TreeNode, optional (default is None)) – The parent node.
children (list, optional (default is None)) – A list of child nodes. If None, initializes an empty list.
orig_indices (list, optional (default is None)) – A list of original indices. If None, initializes an empty list.
Methods
__init__(value[, parent, children, orig_indices])add_child(child_node)Add a child node to the current node.
add_orig_index(value)Add an original index to the node.
Calculate the cost of promoting this child node to a root node.
Get all ancestor nodes of the current node up to the root node.
get_child_node(value)Get the child node associated with the input value.
Get all descendant node values of the current node in pre-order traversal.
get_root()Get the root node of the current node.
print_tree([level, prefix])Print the tree structure starting from the current node.
Promote this child node to a root node, updating the tree structure accordingly.
remove_child(child_node)Remove a child node from the current node.
Calculate the total number of orig_indices values for this node and all of its descendants.
traverse()Traverse the tree in pre-order and return a list of node values.
- add_child(child_node)#
Add a child node to the current node.
- Parameters:
child_node (TreeNode) – The child node to be added.
- add_orig_index(value)#
Add an original index to the node.
- Parameters:
value (int) – The original index to be added.
- calculate_promotion_cost()#
Calculate the cost of promoting this child node to a root node. This corresponds to the sum of the cost of this node’s current root, plus the total number of ancestors (less the root).
- get_ancestors()#
Get all ancestor nodes of the current node up to the root node.
- Returns:
A list of ancestor nodes.
- Return type:
list
- get_child_node(value)#
Get the child node associated with the input value. If that node is not present, return None.
- Parameters:
value (any) – The value to search for in the child nodes.
- Returns:
The child node with the specified value, or None if not found.
- Return type:
TreeNode or None
- get_descendants()#
Get all descendant node values of the current node in pre-order traversal.
- Returns:
A list of descendant node values.
- Return type:
list
- get_root()#
Get the root node of the current node.
- Returns:
The root node of the current node.
- Return type:
- print_tree(level=0, prefix='')#
Print the tree structure starting from the current node.
- Parameters:
level (int, optional (default 0)) – The current level in the tree.
prefix (str, optional (default "")) – The prefix for the current level.
- promote_to_root()#
Promote this child node to a root node, updating the tree structure accordingly.
- remove_child(child_node)#
Remove a child node from the current node.
- Parameters:
child_node (TreeNode) – The child node to be removed.
- total_orig_indices()#
Calculate the total number of orig_indices values for this node and all of its descendants.
- traverse()#
Traverse the tree in pre-order and return a list of node values.
- Returns:
A list of node values in pre-order traversal.
- Return type:
list