TreeWorld and TreeNode#
TreeWorld visualizes a binary tree.
Nodes are placed via a level-order layout computed automatically by the world;
edges are drawn as lines between parent and child nodes.
Building the tree#
from miniworlds_data import TreeWorld
world = TreeWorld()
root = world.set_root(5)
left = world.add_left(root, 3)
right = world.add_right(root, 8)
world.add_left(left, 1)
world.add_right(left, 4)
world.run()
TreeWorld operations#
Method |
Description |
|---|---|
|
Create and return the root node. Can only be called once. |
|
Add a left child to |
|
Add a right child to |
|
Highlight a node with a named state. States: |
|
Color the edge between |
|
Reset all nodes and edges to their default colors. |
|
Return nodes in in-order sequence. |
|
Return nodes in pre-order sequence. |
|
Return nodes in post-order sequence. |
|
The root |
|
List of all |
TreeNode attributes#
Attribute |
Description |
|---|---|
|
The stored value. |
|
Left child |
|
Right child |
|
Current highlight state string. |
|
Change the highlight state and update the color. |
API Reference#
- class miniworlds_data.tree_world.TreeWorld(*, width=600, height=400, node_radius=22, level_height=70, background=(250, 250, 250, 255))[source]#
Pixel world that visualizes a binary tree.
Nodes are added as a root plus left/right children. The world computes a level-order layout automatically and exposes traversal-friendly highlight methods.
Example
from miniworlds_data import TreeWorld world = TreeWorld() root = world.set_root(5) world.add_left(root, 3) world.add_right(root, 8) world.highlight(root, "current") world.run()
- dialog#
- highlight(node, state='current')[source]#
Highlight a node with the given state.
- Return type:
- Parameters:
node – Node to highlight.
state – One of
default,visited,current,root.
- highlight_edge(parent, child, visited=True)[source]#
Color the edge between
parentandchildas visited/unvisited.- Return type: