3.4.1 · D2Trees

Visual walkthrough — Tree terminology — root, leaf, height, depth, degree, subtree

2,008 words9 min readBack to topic

Step 1 — Draw a single node and ask "how tall?"

WHAT. Put one dot on the paper. Call it (just a name — think "the node we are staring at"). It has nobody below it. We want a number that answers: "How many steps down does the longest path from go before it hits the bottom?" We will call that number the height of .

WHY this question. Later, trees decide how fast algorithms run, and speed depends on the longest chain of steps. So "how many steps to the bottom" is the number worth measuring. We start with the tiniest possible tree — one dot — because you cannot understand a rule until you see its simplest case.

PICTURE. A lone dot. To go "down to the bottom" from it, you travel along edges (the lines connecting nodes). This dot has zero edges leaving downward, so the longest downward trip has length .

Figure — Tree terminology — root, leaf, height, depth, degree, subtree

Step 2 — Add ONE child and watch the height grow

WHAT. Below draw one child, call it (for child). Connect them with a single edge. Now is a leaf (no children of its own), so from Step 1, .

WHY. We change the picture by the smallest possible amount — one child — and simply watch what happens to the height number. This is how you discover a rule: perturb the simplest case and observe.

PICTURE. The longest trip down from now is: hop the one edge from to , then stop (because is a leaf). That trip is 1 edge long. So .

Figure — Tree terminology — root, leaf, height, depth, degree, subtree

Look at where that came from — term by term:

  • The is the single edge you just walked to reach the child.
  • The is how much further you fall once you land on .
  • Add them: . ✅

Step 3 — Make the child taller and re-measure

WHAT. Give its own child (a grandchild of ). Now is no longer a leaf. First measure : it has one leaf child , so by Step 2's logic .

WHY. We need to see that the same rule survives when the child is itself tall. If the rule only worked for leaf children it would be useless — real trees are deep.

PICTURE. Longest trip down from : edge , then edge , stop. That is 2 edges. And indeed:

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
  • The is still just the one edge from to its child.
  • now equals because itself has a fall of one edge below it.
  • The whole fall from is one edge to land on plus 's own fall — the drops stack.

Step 4 — Give SEVERAL children: which one decides the height?

WHAT. Now let have three children , , with different heights below them — say , , . We must pick which child determines 's height.

WHY the word "longest". Height is the longest path down. If I want the longest fall from , I must dive into the child that has the deepest well beneath it. Diving into a shallow child gives a short path — that is not the height. So we need a tool that says "pick the biggest of these numbers."

PICTURE. Three wells hang below of depths , , . The deepest is 's well (). Walk one edge into , then fall more:

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
  • — this selects the deepest child, here .
  • The — the single edge from down to that chosen deepest child.
  • Result — the true longest fall bottom.

Step 5 — Assemble the general rule

WHAT. Everything above collapses into a single statement that works for any node.

WHY it's now proven, not guessed. We have a base case (Step 1: a leaf is height ) and a combining step (Steps 2–4: add one edge to the tallest child). Any node is either a leaf (base) or has children (combine). Those two cover every possible node, so the rule is complete.

This is exactly the formula the recursive machinery evaluates: it dives to the leaves, tags each with , then bubbles back up adding at every parent. See it link to real trees in Binary Trees (where each node has at most 2 children, so the is over just two numbers) and to why we cap it in Balanced Trees (AVL, Red-Black).


Step 6 — The degenerate cases (never leave a gap)

WHAT & WHY. A rule you can trust must survive weird inputs. We check the three edge cases.

Case A — empty tree (no nodes at all). There is no node to stand on, so "longest path down" has no starting point. By convention we set Why ? So that a leaf (a node with two empty children) still computes to : . The makes the empty child add nothing through the . It is the value that keeps the formula honest.

Case B — a single node. It is a leaf → height (Step 1). No surprises.

Case C — a straight chain (each node has exactly one child). Every has only one thing to choose, so it just returns that child. Then once per edge — the height equals the number of edges in the chain. A chain of nodes has height : this is the tallest a tree of nodes can be, the worst case that balancing exists to prevent.

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
  • Left: the empty child slots contribute , so a leaf stays .
  • Right: a chain stacks per edge — no branching to shorten it.

The one-picture summary

Figure — Tree terminology — root, leaf, height, depth, degree, subtree

Read the picture bottom-up: leaves are stamped ; every parent grabs the biggest child number and adds for the edge it walked; the root's number is the tree's height.

Recall Feynman retelling — say it like a story

Imagine you are standing on a node and you drop a marble down the deepest tube below you. The height is how many pipe-joints (edges) the marble clicks past before it stops.

  • If you are a leaf, there is no pipe below you — the marble clicks zero times. Height 0.
  • If you have children, the marble must first fall through one joint to reach a child (that is the ), and then it keeps falling through that child's deepest tube. So you ask each child "how deep is your tube?", take the deepest one (), and add your one joint.
  • More children sideways does not help — a marble falls down one tube, so we pick the deepest, never add them up.
  • An imaginary "no-child" slot is scored so that a plain leaf still comes out to exactly .

That single " for my edge, plus my deepest child's answer" is the whole formula, and it is why computing height is just recursion bubbling up from the leaves.


Quick self-test

Recall Why

and not sum in the height formula? Because a path travels down one branch at a time — the longest single path is the biggest child height, not the total. Summing would count branches you never walk together.

Recall Why does a leaf get height 0, not 1?

We count edges below the node, and a leaf has zero edges going down. Counting nodes would give a different convention; this note counts edges.

Recall Why assign height

to the empty tree? So a leaf (whose two children are empty) evaluates to , keeping the recursion consistent.

recall::: (see cards above)


Connections

  • Parent: Tree terminology — this page derives its height formula.
  • Recursion — the base-case-plus-combine pattern is this derivation.
  • Binary Trees — the is over at most two children.
  • Balanced Trees (AVL, Red-Black) — why the tall-chain worst case matters.
  • Tree Traversals — the same leaves-first bubbling powers post-order traversal.
  • Graphs — a tree is an acyclic connected graph; "longest downward path" is a shortest-vs-longest contrast worth noting.