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

set_root(value)

Create and return the root node. Can only be called once.

add_left(parent, value)

Add a left child to parent; return the new node.

add_right(parent, value)

Add a right child to parent; return the new node.

highlight(node, state)

Highlight a node with a named state. States: default, visited, current, root.

highlight_edge(parent, child, visited)

Color the edge between parent and child.

reset_colors()

Reset all nodes and edges to their default colors.

inorder()

Return nodes in in-order sequence.

preorder()

Return nodes in pre-order sequence.

postorder()

Return nodes in post-order sequence.

root

The root TreeNode (property, None if not yet set).

nodes

List of all TreeNode instances (property).

TreeNode attributes#

Attribute

Description

value

The stored value.

left

Left child TreeNode or None.

right

Right child TreeNode or None.

state

Current highlight state string.

set_state(state)

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()
add_left(parent, value)[source]#

Add a left child to parent and return it.

Return type:

TreeNode

add_right(parent, value)[source]#

Add a right child to parent and return it.

Return type:

TreeNode

dialog#
highlight(node, state='current')[source]#

Highlight a node with the given state.

Return type:

None

Parameters:
  • node – Node to highlight.

  • state – One of default, visited, current, root.

highlight_edge(parent, child, visited=True)[source]#

Color the edge between parent and child as visited/unvisited.

Return type:

None

inorder()[source]#

Return nodes in in-order sequence.

Return type:

List[TreeNode]

property nodes: List[TreeNode]#
postorder()[source]#

Return nodes in post-order sequence.

Return type:

List[TreeNode]

preorder()[source]#

Return nodes in pre-order sequence.

Return type:

List[TreeNode]

reset_colors()[source]#

Reset every node to its default/root state and edges to default color.

Return type:

None

property root: TreeNode | None#
set_root(value)[source]#

Create the root node and return it.

Return type:

TreeNode

class miniworlds_data.tree_world.TreeNode(value, *, world, radius, color)[source]#

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)[source]#
Return type:

None

remove_label()[source]#
Return type:

None

set_state(state)[source]#
Return type:

None

property state: str#