Skip to main content

Tree

Trees are hierarchical data structures with parent-child relationships. They are perfect for visualizing tree algorithms, organizational structures, and hierarchical data.

Source Code

1tree family = {
2 nodes: [grandpa, dad, mom, child1, child2]
3 children: [grandpa-dad, grandpa-mom, dad-child1, dad-child2]
4 value: ["John", "Mike", "Sarah", "Emma", "Alex"]
5 color: ["gold", "blue", "pink", "lightblue", "lightgreen"]
6}
7
8page
9show family

Rendered Diagram

Properties

Trees support the following properties:

PropertyTypeDescription
nodesid[]List of node identifiers
childrenchild[]Parent-child relationships (e.g., parent-child)
value(number | string | null)[]Values for each node
color(color | null)[]Colors for each node
arrow(number | string | null)[]Arrows or labels for each node
abovestring | text_object (see special features)Label positioned above the tree
belowstring | text_objectLabel positioned below the tree
leftstring | text_objectLabel positioned to the left
rightstring | text_objectLabel positioned to the right

Methods

Trees support these methods for manipulation:

Text Positioning Methods

  • setText(text, position) - Set or remove text at specific positions ("above", "below", "left", "right")

Node Methods

  • addNode(name, value?) - Add new node with optional value
  • removeNode(name) - Remove node and its subtree
  • setValue(index | id, value) - Set value for node at index or node id
  • setColor(index | id, color) - Set color for node at index or node id
  • setArrow(index | id, arrow) - Set arrow for node at index or node id

Tree Structure Methods

  • addChild(parent-child, value?) - Add child to parent with optional value
  • setChild(parent-child) - Change parent-child relationship
  • removeSubtree(node) - Remove node and entire subtree

Multiple Element Methods

  • setValues([...]) - Set multiple node values
  • setColors([...]) - Set multiple node colors
  • setArrows([...]) - Set multiple node arrows

Examples

Basic Tree Example

Binary Search Tree

Source Code

1tree bst = {
2 nodes: [root, l, r, ll, lr]
3 children: [root-l, root-r, l-ll, l-lr]
4 value: [50, 30, 70, 20, 40]
5 color: [null, null, null, null, null]
6}
7
8page
9show bst

Rendered Diagram

Dynamic Tree Operations

Source Code

1tree org = {
2 nodes: [CEO, CTO]
3 children: [CEO-CTO]
4 value: ["Alice", "Bob"]
5}
6
7page
8show org
9
10page
11org.addChild(CEO-CFO, "Carol")
12org.setColor(CFO, "lightgreen")
13
14page
15org.addChild(CTO-DevLead, "Dave")
16org.setColor(DevLead, "lightblue")
17
18page
19org.addChild(DevLead-Engineer, "Eve")
20org.setColor(Engineer, "yellow")

Rendered Diagram

Tree Traversal Visualization

Source Code

1tree traverse = {
2 nodes: [A, B, C, D, E]
3 children: [A-B, A-C, B-D, B-E]
4 value: [1, 2, 3, 4, 5]
5}
6
7page
8show traverse
9
10page
11traverse.setColor(A, "blue")
12traverse.setArrow(A, "visiting")
13
14page
15traverse.setColor(B, "blue")
16traverse.setArrow(B, "visiting")
17traverse.setColor(A, "gray")
18traverse.setArrow(A, "visited")
19
20page
21traverse.setColor(D, "blue")
22traverse.setArrow(D, "visiting")
23traverse.setColor(B, "gray")
24traverse.setArrow(B, "visited")

Rendered Diagram

Organizational Chart

Source Code

1tree company = {
2 nodes: [president, marketing, engineering, sales]
3 children: [president-marketing, president-engineering, president-sales]
4 value: ["Jane Smith", "Marketing Dept", "Engineering Dept", "Sales Dept"]
5 color: ["gold", "lightcoral", "lightblue", "lightgreen"]
6 above: "Company Structure"
7}
8
9page
10show company

Rendered Diagram

Use Cases

Trees are ideal for:

  • Tree algorithms - Tree traversal (pre/in/post-order), tree search
  • Binary search trees - Insertion, deletion, search operations
  • Decision trees - Decision making processes, game trees
  • Parse trees - Syntax trees, expression evaluation
  • File systems - Directory hierarchies, folder structures
  • Organizational charts - Company hierarchy, family trees
  • Heap operations - Priority queues, heap sort visualization
  • Trie structures - String matching, autocomplete