Decisions with Depth — Learning Trees in the Royal Ancestral Hall
Decisions with Depth — Learning Trees in the Royal Ancestral Hall
In the glowing Royal Ancestral Hall, Arjun discovers that some problems aren’t lines or loops at all — they are branching choices, spreading like a tree through generations.
Characters: Arjun, Guru Bodhi · Arc: Intermediate Gurukul · Pattern: Trees, parent/child relationships, depth, traversal (preorder, inorder, postorder)
📜 Sutra of the Hall — When One Line Isn’t Enough
Arjun stepped into the Royal Ancestral Hall. Golden statues of kings, queens, generals, sages and forgotten princes rose in tiered rows. Threads of light ran between them — parents to children, rulers to heirs.
He’d been given a simple task: “Find the third heir of the second son of the old queen.” But staring at all these branches of bloodlines, Arjun felt his brain locking up.
“Arrays I understand,” he muttered. “Even linked lists. But this? Every person has children, who have children, who have more children. The data isn't a line. It’s… stacked.”
🌳 Sutra of the Tree — Root, Branches, Leaves
The hall dimmed. A familiar warm glow appeared beside Arjun as Guru Bodhi stepped forward, staff humming with light.
He tapped his staff on the polished floor. From the stone, a glowing family tree erupted — one ancestor at the bottom, branching into children, grandchildren, and great-grandchildren.
- Root: the original ancestor or starting point.
- Children: nodes directly connected below a parent.
- Leaves: nodes with no children — the current generation.
- Level / depth: how far a node is from the root.
- Path: the chain of nodes from root down to any person.
🧠 What Is a Tree (as a Data Structure)?
In code, a tree is a collection of nodes connected by edges, with exactly one path between any two nodes and a special node called the root.
The common vocabulary is simple but non-negotiable:
- Parent / child — direct relationship between two connected nodes.
- Siblings — children of the same parent (like princes and princesses of one king).
- Subtree — a node plus all nodes beneath it.
- Height / depth — how “tall” the tree is; how many layers of decisions or generations it holds.
👑 Real-World Scroll — The Royal Line of Succession
In the center of the hall lay a long golden table. On it, a parchment showed the line of succession: the current king, his children, their children, and so on.
That parchment is a tree. Asking
"Who rules if the king and first prince fall?" is literally a
query on that tree.
🧾 Building a Tiny Royal Tree in Code
Let’s model a tiny royal family: a king, two children, and one grand-child. Nothing fancy, just the core pattern.
class Node:
def __init__(self, name):
self.name = name
self.children = [] # list[Node]
def add_child(self, child):
self.children.append(child)
# Build a tiny royal tree
king = Node("King Arvind")
prince = Node("Prince Dev")
princess = Node("Princess Meera")
heir = Node("Young Heir Kiran")
king.add_child(prince)
king.add_child(princess)
prince.add_child(heir)
Every node knows its children. The entire royal structure is just the king node plus everything reachable from
it.
🚶 Tree Traversals — Different Ways to Walk the Same Lineage
Guru Bodhi traced glowing paths along the tree: sometimes starting from the root, sometimes starting from the far-left branch.
For a binary tree (each node has up to two children: left and right), the classic traversals are:
- Preorder: Root → Left → Right
- Inorder: Left → Root → Right
- Postorder: Left → Right → Root
🧮 Traversals in Code (Binary Tree Version)
For clarity, here’s a tiny binary-tree node. Each node has at most two children: left and right.
class BNode:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def preorder(node):
# Root → Left → Right
if node is None:
return
print(node.value)
preorder(node.left)
preorder(node.right)
def inorder(node):
# Left → Root → Right
if node is None:
return
inorder(node.left)
print(node.value)
inorder(node.right)
def postorder(node):
# Left → Right → Root
if node is None:
return
postorder(node.left)
postorder(node.right)
print(node.value)
Same tree, three different traversal orders. The choice isn’t cosmetic — it changes what question you’re really asking about the structure.
🧭 Where Trees Secretly Rule Your Code
Once you see trees, you start spotting them everywhere:
- File systems — folders inside folders inside folders.
- UI hierarchies — components nested inside containers.
- Company org charts — CEO → VPs → managers → teams.
- Game worlds — scenes made of nested objects.
- Decision trees — each question branches into answers, then more questions.
Trees are the structure of “if this, then branch there”. Whenever choices or responsibilities split again and again, a tree is hiding underneath.