3.4.1 · D3Trees

Worked examples — Tree terminology — root, leaf, height, depth, degree, subtree

4,195 words19 min readBack to topic

Two symbols we will lean on — defined before use

Before the examples, we pin down the two tools that keep reappearing, so no symbol is ever used on faith.


The scenario matrix

Every tree question is really one of these shapes or edge cases. The examples below are tagged with the cell(s) they cover.

Cell The scenario Why it is tricky
C1 Normal bushy tree (multiple children per node) The baseline — measure depth/height/degree cleanly
C2 Leaf sitting above the bottom level Breaks the "leaf = deepest" instinct
C3 Single-node tree (root is a leaf) Degenerate: is the root a leaf? height? depth?
C4 Empty tree () The ultimate degenerate — height convention flips
C5 A "stick" / path tree (every node has 1 child) Height is maximal for its node count
C6 Perfectly balanced tree Height is minimal — the geometric-series bound is tight
C7 Recursive height computation on an asymmetric tree Must pick the tallest child, not the first
C8 Subtree extraction + re-measuring inside it Depth resets to 0 inside the subtree
C9 Real-world word problem (file system) Translate English → tree vocabulary
C10 Exam twist: given constraints, deduce a hidden number Use edges / geometric bound backwards

We now cover all ten across the worked examples. Each example names its cell(s).


Example 1 — Baseline bushy tree (C1, C2)

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
Figure s01: the 8-node tree drawn top-down. Purple nodes are internal (A, B, D, G); mint nodes are leaves (C, E, F, H). Depth labels 0–3 run down the left; a coral arrow marks C as a leaf at depth 1 and traces the longest path A→D→G→H.

  1. Root = A. Why this step? Root is the only node with no parent (parent arrow pointing into it). Look at the figure — every node has an incoming edge from above except A.
  2. Leaves = {E, F, C, H}. Why? A leaf is defined only by "no children" — no edges leave it going downward. In the figure C has nothing below it, so it is a leaf even though it sits at depth 1 (this is cell C2). Do not tie "leaf" to "bottom row".
  3. Degree of the tree = 3. Why? Degree of a node = its number of children. A has 3 children (B, C, D), and no node has more. The tree's degree is the maximum node degree .
  4. Depth of F = 2. Why? Depth = edges on the path root → node. Path A→B→F has 2 edges. Count the edges (arrows), not the nodes.
  5. Height of B = 1. Why? Height = edges on the longest downward path from the node to a leaf. From B we can go B→E or B→F, both 1 edge. So height .
  6. Height of the tree = 3. Why? Height of the tree = height of the root = longest root-to-leaf path. A→D→G→H is 3 edges — the longest such path.

Verify: Node count (A,B,C,D,E,F,G,H) ⇒ a tree must have edges. Count the arrows in the figure: A-B, A-C, A-D, B-E, B-F, D-G, G-H . ✅ Consistent, so we drew a valid tree.


Example 2 — Single-node tree (C3, degenerate)

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
Figure s02: a single mint circle labelled R floating alone, with a coral note reading "root AND leaf, depth 0, height 0." No edges are drawn — there is nothing to connect.

  1. Root = R. Why? It has no parent — it satisfies the root definition trivially.
  2. R is also a leaf. Why? A leaf is "no children," and R has zero children. Yes — the same node is simultaneously root and leaf. There is no rule forbidding it; the two definitions look in opposite directions and both happen to be satisfied.
  3. Degree of the tree = 0. Why? R has 0 children, and it is the only node, so the max degree is 0.
  4. Depth of R = 0. Why? Zero edges from the root to itself.
  5. Height of R = 0. Why? Zero edges from R down to a leaf — and R is the leaf, so the longest downward path is length 0.
  6. Height of the tree = 0. Why? It equals the height of the root, which is 0.

Verify: ⇒ edges . The picture has zero edges. ✅ And depth height of tree, matching "height of tree = max depth."


Example 3 — Empty tree (C4, the ultimate edge case)

  1. There is no root, no leaf, nothing to measure a path from. Why care? Recursive code hits this case: height(null) must return something so 1 + max(children) still works for a node with one missing child.
  2. Convention: height of the empty tree . Why ? Take the recursive rule (the operator we defined at the top). For a single-node tree we want . R has no children, so we treat "the tallest child" as an empty tree of height , giving . So empty is exactly the value that makes the leaf formula fall out automatically.

Verify: Plug into the recursion: ✅ We recover leaf height 0, so is the self-consistent choice.


Example 4 — The "stick" tree: maximum height (C5)

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
Figure s03: four nodes P, Q, S, T in a diagonal chain, each with exactly one child. P (top, purple) is the root; T (bottom, mint) is the sole leaf. A coral note flags "all 3 edges on ONE path, height = n-1 = 3."

  1. Height of the tree = 3. Why? Longest root-to-leaf path P→Q→S→T has 3 edges.
  2. Depth of T = 3. Why? Same path measured downward from root; here height and this deepest depth coincide only because it is the deepest node.
  3. Degree of the tree = 1. Why? Every node has at most one child.
  4. Tallest possible for ? Why? Height is the number of edges on a root-to-leaf path. With nodes you have edges total; a stick puts all of them on one single path, so height , the absolute maximum. You cannot make a 4-node tree taller than 3.

Verify: edges ; the stick uses all 3 on one path, so . ✅ Maximum confirmed.


Example 5 — Perfectly balanced tree: minimum height + geometric bound (C6)

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
Figure s04: a fully filled binary tree — A on top, B and C below, then leaves D, E, F, G. Left-edge labels show each level's capacity (lvl 0: 1, lvl 1: 2, lvl 2: 4); a coral caption states n = 1+2+4 = 7 = max for h=2, d=2.

  1. Count nodes: . Why? Level 0 has 1, level 1 has 2, level 2 has 4; .
  2. Apply the bound (the one we derived in "Two symbols we will lean on") with . Why this tool and not just counting? Counting works when you can see the tree; the formula answers "what is the most a tree of this shape could hold?" — needed when you only know and . Here is the child-cap .
  3. The full tree hits the maximum (). Why? "Perfectly balanced / full" means every level is completely packed, so it is the densest possible tree — equality holds.
  4. Minimal height for ? Why? Height is minimized by making each level as wide as possible. With degree 2 the widest a 7-node tree can be still needs 3 levels (heights ), because . So height 2 is the smallest achievable — you cannot fit 7 nodes into height 1 (that holds at most ).

Verify: height-1 bound , so height 1 is impossible; height-2 bound , so height 2 is achievable and minimal. ✅ This is the shape Balanced Trees (AVL, Red-Black) chases to keep height .


Example 6 — Recursive height on an asymmetric tree (C7)

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
Figure s05: an asymmetric tree. Root R (purple) has children X (mint leaf), Y (purple), Z (butter). Y→W (mint leaf); Z→M (butter)→N (mint leaf). Each node is tagged with its computed height h=…; a coral arrow highlights that Z, the tallest child at h=2, sets h(R)=3.

  1. Leaves first: . Why? No children ⇒ base case, height 0.
  2. . Why? Y's only child W has height 0.
  3. . Why? Same base-plus-one step.
  4. . Why? Z's child M has height 1.
  5. . Why? The operator (defined at the top) keeps the biggest child height, so the longest path leaves through the tallest child Z. If you naively took the first child X you would get 1 — wrong. The max is the whole point.

Verify: Longest root-to-leaf path is R→Z→M→N edges. ✅ Matches .


Example 7 — Subtree: depth resets inside it (C8)

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
Figure s06: two side-by-side pictures. Left — the full Example-6 tree with the {Z, M, N} branch coloured coral, noting "N at depth 3." Right — that same branch lifted out on its own with Z as the new local root at depth 0, so M is depth 1 and N is depth 2.

  1. Subtree . Why? A subtree is a node plus all its descendants. Z's descendants are M (child) and N (grandchild).
  2. Depth of N inside the subtree = 2. Why? Inside the subtree, Z becomes the local root with depth 0. Path Z→M→N has 2 edges, so N is at depth 2 relative to Z.
  3. Depth of N in the original tree = 3. Why? From the global root R: path R→Z→M→N has 3 edges.
  4. They differ by exactly 1. Why? The subtree view drops the single edge R→Z. In general, depth-in-subtree depth-in-original depth-of-subtree-root. Here . ✅

Verify: depth of subtree root Z in original (path R→Z). Then global depth of N that local depth. ✅ Depth is relative to whichever root you declare.


Example 8 — Word problem: a file system (C9)

Figure — Tree terminology — root, leaf, height, depth, degree, subtree
Figure s07: rounded boxes for a file tree. Purple boxes are folders (/home, docs, pics); mint boxes are files (cv.pdf, a.jpg, b.jpg). /home branches to docs and pics; pics branches to the two jpgs. A coral arrow marks "b.jpg depth 2."

  1. Root = /home. Why? It is the topmost container with no parent folder in this problem.
  2. Leaves = {cv.pdf, a.jpg, b.jpg}. Why? Files contain nothing below them ⇒ no children ⇒ leaves.
  3. Degree of the tree = 2. Why? /home has 2 children (docs, pics) and pics has 2 children (a.jpg, b.jpg). Max node degree .
  4. Depth of b.jpg = 2. Why? Path /home → pics → b.jpg has 2 edges.
  5. Height of the tree = 2. Why? Longest root-to-leaf path, e.g. /home → pics → b.jpg, is 2 edges.
  6. Subtree rooted at pics = {pics, a.jpg, b.jpg}. Why? pics plus its descendants — a little folder-tree by itself.

Verify: nodes (/home, docs, pics, cv.pdf, a.jpg, b.jpg) ⇒ edges . Count: home-docs, home-pics, docs-cv, pics-a, pics-b . ✅


Example 9 — Exam twist: deduce a hidden number (C10)

  1. (a) Use . Why this tool? Every tree obeys "edges " exactly (the parent note derived it: each non-root node contributes one parent-edge). Since it is an identity, knowing one side pins the other. Invert: nodes.
  2. (b) Use the geometric max-nodes bound — the very formula we derived in "Two symbols we will lean on." Why this tool? We want the most nodes a bounded-degree, bounded-height tree can hold, and that is precisely the packed-every-level count that bound computes. Here the child-cap is and the height is : What each piece means: is "if every one of the levels were as wide as the child-cap allows"; subtracting and dividing by is just the geometric-series shortcut for .
  3. (b) Cross-check by adding the levels directly: level 0 holds , level 1 holds , level 2 holds ; total . ✅ Same answer, so the closed form and the hand-sum agree.

Verify: (a) edges ✅. (b) and ✅.


Example 10 — Limiting behaviour: stick vs balanced as grows (C5 vs C6 combined)

  1. Stick height . Why? All 6 edges lie on one path (Example 4's logic scaled to 7 nodes).
  2. Balanced height . Why? From Example 5, a full degree-2 tree with 7 nodes packs 3 levels ⇒ height 2. Formally, height .
  3. The gap is edges, and it widens as grows: stick height grows linearly () while balanced grows logarithmically (). Why care? Search cost in Binary Search Trees is proportional to height — this gap is the entire reason Balanced Trees (AVL, Red-Black) exist.

Verify: : stick ; balanced ; gap . ✅


Recall Self-test: name the cell, then answer

A node with no children sitting at depth 1 — is it a leaf? ::: Yes (cell C2). Leaf = no children, at any depth. Height of a tree containing one node? ::: 0 (cell C3). Zero edges below the root. Height of the empty tree, and why? ::: (cell C4), so that gives leaf height 0. For fixed , which shape maximizes height? ::: The stick (cell C5); height . Max nodes in a degree-2 tree of height 3? ::: (cell C6/C10). Depth of a node inside a subtree vs whole tree? ::: Subtree depth global depth depth of subtree root (cell C8).


Connections

  • Back to the parent terminology note
  • Binary Trees — the degree- special case behind Examples 5, 10.
  • Binary Search Trees — search cost height (Example 10's punchline).
  • Balanced Trees (AVL, Red-Black) — engineer height down to .
  • Tree Traversals — visiting the nodes we just measured.
  • Recursion — the height formula in Examples 6–7 is pure recursion.
  • Graphs — trees are acyclic connected graphs; degree conventions differ.

Scenario Map

one node

no nodes

one child each

levels full

many children

inside a node

words

deduce number

Tree question

Which scenario

Single node root is leaf

Empty height is minus one

Stick max height

Balanced min height

Bushy baseline

Leaf above bottom

Subtree depth resets

File system

Invert the formula