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))[Quellcode]#
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()
- add_left(parent, value)[Quellcode]#
Add a left child to
parentand return it.- Rückgabetyp:
- add_right(parent, value)[Quellcode]#
Add a right child to
parentand return it.- Rückgabetyp:
- dialog#
- highlight(node, state='current')[Quellcode]#
Highlight a node with the given state.
- Rückgabetyp:
- Parameter:
node – Node to highlight.
state – One of
default,visited,current,root.
- highlight_edge(parent, child, visited=True)[Quellcode]#
Color the edge between
parentandchildas visited/unvisited.- Rückgabetyp:
- inorder()[Quellcode]#
Return nodes in in-order sequence.
- postorder()[Quellcode]#
Return nodes in post-order sequence.
- preorder()[Quellcode]#
Return nodes in pre-order sequence.
- reset_colors()[Quellcode]#
Reset every node to its default/root state and edges to default color.
- Rückgabetyp:
- set_root(value)[Quellcode]#
Create the root node and return it.
- Rückgabetyp:
- class miniworlds_data.tree_world.TreeNode(value, *, world, radius, color)[Quellcode]#
A node actor in a
TreeWorld.Each node stores a value, optional left/right children, and a highlight state used during traversals. Edges to its children are drawn as lines.
- center_on(x, y)[Quellcode]#
- Rückgabetyp:
- remove_label()[Quellcode]#
- Rückgabetyp:
- set_state(state)[Quellcode]#
- Rückgabetyp: