Represents a binary tree node. This class provides methods and properties for managing the current node, and the binary tree in which the node is the root. When a docstring in this class mentions "binary tree", it is referring to the current node and its descendants. :param val
| 93 | |
| 94 | |
| 95 | class Node: |
| 96 | """Represents a binary tree node. |
| 97 | |
| 98 | This class provides methods and properties for managing the current node, |
| 99 | and the binary tree in which the node is the root. When a docstring in |
| 100 | this class mentions "binary tree", it is referring to the current node and |
| 101 | its descendants. |
| 102 | |
| 103 | :param value: Node value (must be a float/int/str). |
| 104 | :type value: float | int | str |
| 105 | :param left: Left child node (default: None). |
| 106 | :type left: binarytree.Node | None |
| 107 | :param right: Right child node (default: None). |
| 108 | :type right: binarytree.Node | None |
| 109 | :raise binarytree.exceptions.NodeTypeError: If left or right child node is |
| 110 | not an instance of :class:`binarytree.Node`. |
| 111 | :raise binarytree.exceptions.NodeValueError: If node value is invalid. |
| 112 | """ |
| 113 | |
| 114 | def __init__( |
| 115 | self, |
| 116 | value: NodeValue, |
| 117 | left: Optional["Node"] = None, |
| 118 | right: Optional["Node"] = None, |
| 119 | ) -> None: |
| 120 | self.value = self.val = value |
| 121 | self.left = left |
| 122 | self.right = right |
| 123 | |
| 124 | def __repr__(self) -> str: |
| 125 | """Return the string representation of the current node. |
| 126 | |
| 127 | :return: String representation. |
| 128 | :rtype: str |
| 129 | |
| 130 | **Example**: |
| 131 | |
| 132 | .. doctest:: |
| 133 | |
| 134 | >>> from binarytree import Node |
| 135 | >>> |
| 136 | >>> Node(1) |
| 137 | Node(1) |
| 138 | """ |
| 139 | return "Node({})".format(self.val) |
| 140 | |
| 141 | def __str__(self) -> str: |
| 142 | """Return the pretty-print string for the binary tree. |
| 143 | |
| 144 | :return: Pretty-print string. |
| 145 | :rtype: str |
| 146 | |
| 147 | **Example**: |
| 148 | |
| 149 | .. doctest:: |
| 150 | |
| 151 | >>> from binarytree import Node |
| 152 | >>> |
no outgoing calls