Tree
import mathy_core.tree
BinaryTreeNode¶
BinaryTreeNode(
self: ~NodeType,
left: Optional[~NodeType] = None,
right: Optional[~NodeType] = None,
parent: Optional[~NodeType] = None,
id: Optional[str] = None,
)
The binary tree node is the base node for all of our trees, and provides a rich set of methods for constructing, inspecting, and modifying them. The node itself defines the structure of the binary tree, having left and right children, and a parent.
clone¶
BinaryTreeNode.clone(self: ~NodeType) -> ~NodeType
get_children¶
BinaryTreeNode.get_children(self: ~NodeType) -> List[~NodeType]
get_root¶
BinaryTreeNode.get_root(self: ~NodeType) -> ~NodeType
get_root_side¶
BinaryTreeNode.get_root_side(self: ~NodeType) -> Literal['left', 'right']
get_sibling¶
BinaryTreeNode.get_sibling(self: ~NodeType) -> Optional[~NodeType]
get_side¶
BinaryTreeNode.get_side(
self,
child: Optional[~NodeType],
) -> Literal['left', 'right']
child
is the left or right child of this node is_leaf¶
BinaryTreeNode.is_leaf(self) -> bool
rotate¶
BinaryTreeNode.rotate(self: ~NodeType) -> ~NodeType
Rotate a node, changing the structure of the tree, without modifying the order of the nodes in the tree.
set_left¶
BinaryTreeNode.set_left(
self: ~NodeType,
child: Optional[~NodeType] = None,
clear_old_child_parent: bool = False,
) -> ~NodeType
child
set_right¶
BinaryTreeNode.set_right(
self: ~NodeType,
child: Optional[~NodeType] = None,
clear_old_child_parent: bool = False,
) -> ~NodeType
child
set_side¶
BinaryTreeNode.set_side(
self,
child: ~NodeType,
side: Literal['left', 'right'],
) -> ~NodeType
child
on the given side
visit_inorder¶
BinaryTreeNode.visit_inorder(
self,
visit_fn: Callable[[Any, int, Optional[Any]], Optional[Literal['stop']]],
depth: int = 0,
data: Optional[Any] = None,
) -> Optional[Literal['stop']]
Left -> Visit -> Right
This method accepts a function that will be invoked for each node in the tree. The callback function is passed three arguments: the node being visited, the current depth in the tree, and a user specified data parameter.
Info
Traversals may be canceled by returning STOP
from any visit function.
visit_postorder¶
BinaryTreeNode.visit_postorder(
self,
visit_fn: Callable[[Any, int, Optional[Any]], Optional[Literal['stop']]],
depth: int = 0,
data: Optional[Any] = None,
) -> Optional[Literal['stop']]
Left -> Right -> Visit
This method accepts a function that will be invoked for each node in the tree. The callback function is passed three arguments: the node being visited, the current depth in the tree, and a user specified data parameter.
Info
Traversals may be canceled by returning STOP
from any visit function.
visit_preorder¶
BinaryTreeNode.visit_preorder(
self,
visit_fn: Callable[[Any, int, Optional[Any]], Optional[Literal['stop']]],
depth: int = 0,
data: Optional[Any] = None,
) -> Optional[Literal['stop']]
Visit -> Left -> Right
This method accepts a function that will be invoked for each node in the tree. The callback function is passed three arguments: the node being visited, the current depth in the tree, and a user specified data parameter.
Info
Traversals may be canceled by returning STOP
from any visit function.
VisitDataType¶
Template type of user data passed to visit functions.