Tree
A tree data structure with a nested Node class.
Usage
Tree()Parameters
root_value=None-
Value for the root node.
Classes
| Name | Description |
|---|---|
| Node | A tree node. |
Node
A tree node.
Usage
Node()Parameters
value-
The node value.
Methods
| Name | Description |
|---|---|
| add_child() | Add a child node. |
| is_leaf() | Check if this node is a leaf. |
add_child()
Add a child node.
Usage
add_child(value)Parameters
value-
The child’s value.
Returns
Tree.Node-
The new child node.
is_leaf()
Check if this node is a leaf.
Usage
is_leaf()Returns
bool-
True if no children.
Methods
| Name | Description |
|---|---|
| depth() | Calculate the depth of the tree. |
| size() | Count the total number of nodes. |
depth()
Calculate the depth of the tree.
Usage
depth()Returns
int-
Maximum depth from root to leaf.
size()
Count the total number of nodes.
Usage
size()Returns
int-
Total number of nodes.