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
8page9show familyRendered Diagram
Properties
Trees support the following properties:
| Property | Type | Description |
|---|---|---|
nodes | id[] | List of node identifiers |
children | child[] | 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 |
above | string | text_object (see special features) | Label positioned above the tree |
below | string | text_object | Label positioned below the tree |
left | string | text_object | Label positioned to the left |
right | string | text_object | Label 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 valueremoveNode(name)- Remove node and its subtreesetValue(index | id, value)- Set value for node at index or node idsetColor(index | id, color)- Set color for node at index or node idsetArrow(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 valuesetChild(parent-child)- Change parent-child relationshipremoveSubtree(node)- Remove node and entire subtree
Multiple Element Methods
setValues([...])- Set multiple node valuessetColors([...])- Set multiple node colorssetArrows([...])- 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
8page9show bstRendered Diagram
Dynamic Tree Operations
Source Code
1tree org = {2 nodes: [CEO, CTO]3 children: [CEO-CTO]4 value: ["Alice", "Bob"]5}6
7page8show org9
10page11org.addChild(CEO-CFO, "Carol")12org.setColor(CFO, "lightgreen")13
14page15org.addChild(CTO-DevLead, "Dave")16org.setColor(DevLead, "lightblue")17
18page19org.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
7page8show traverse9
10page11traverse.setColor(A, "blue")12traverse.setArrow(A, "visiting")13
14page15traverse.setColor(B, "blue")16traverse.setArrow(B, "visiting")17traverse.setColor(A, "gray")18traverse.setArrow(A, "visited")19
20page21traverse.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
9page10show companyRendered 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
Related
- Methods Reference - Complete list of tree methods
- Graph - General graph structures
- LinkedList - Linear linked structures