3.4.1Trees

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

2,017 words9 min readdifficulty · medium

WHAT is a tree (the foundation)

WHY n1n-1 edges? Every node except the root is connected to its parent by exactly one edge. That's n1n-1 nodes, each contributing one edge → n1n-1 edges. The root contributes none (it has no parent). This is derived, not memorized.


The core vocabulary

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

Depth vs Height — the part everyone confuses


Worked examples


Common mistakes (Steel-manned)


Recall Explain it to a 12-year-old (hidden)

Imagine a family tree drawn upside-down. The great-grandparent at the very top is the root. People with no kids are leaves. Your depth is how many steps up it takes to reach the top boss. Your height is how many steps down to the farthest kid in your branch. Degree is just how many direct children you have. A subtree is "you and your whole family line below you" — it's a little tree all by itself. No one can be their own ancestor, so there are no loops!


Active-recall flashcards

A tree with nn nodes has how many edges, and why?
n1n-1; every node except the root contributes exactly one edge to its parent.
Definition of a leaf?
A node with no children.
Definition of root?
The single node with no parent; the topmost node.
What is the depth of the root?
0 (zero edges from root to itself).
What is the height of a leaf?
0 (zero edges from it down to a leaf — itself).
Depth vs height, in one line each?
Depth = edges up to root; Height = edges down to deepest leaf.
Recursive formula for height of node v?
height(v)=1+maxcheight(c)\text{height}(v) = 1 + \max_{c}\text{height}(c), with leaf height =0=0.
Degree of a node vs degree of a tree?
Node degree = number of its children; tree degree = max degree over all nodes.
What is a subtree?
A node together with all its descendants, viewed as a tree on its own.
Can a leaf appear above the bottom level?
Yes — leaf means "no children", which can happen at any depth.
Max nodes in a tree of height h with degree ≤ d (d≥2)?
dh+11d1\frac{d^{h+1}-1}{d-1}, summing d0++dhd^0+\dots+d^h.
Height of the whole tree equals?
Height of the root = max depth among all nodes.

Connections

  • Binary Trees — special case where degree of tree 2\le 2.
  • Binary Search Trees — ordering invariant built on this terminology.
  • Tree Traversals — DFS/BFS visit nodes by depth/level.
  • Balanced Trees (AVL, Red-Black) — control height to keep operations O(logn)O(\log n).
  • Recursion — height/subtree definitions are inherently recursive.
  • Graphs — a tree is a connected acyclic graph; compare degree definitions.

Concept Map

has one

n nodes gives

no cycles

top of

node with no children

node with children

count of children

path down to node

path up defines

recursive

plus descendants

of root equals

Tree

Root no parent

n-1 edges

Acyclic structure

Parent child links

Leaf

Internal node

Degree of node

Depth root is 0

Height leaf is 0

1 plus max child height

Subtree

Tree height

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho ek ulta family tree — sabse upar dada ji (jiske upar koi nahi), woh hai root. Jin logon ke koi bacche nahi hai, woh leaf kehlate hain. Tree mein koi loop nahi hota, aur har node ka sirf ek hi parent hota hai. Bas isi structure ke alag-alag hisson ko naam diye gaye hain — yeh terminology hi aage saare tree algorithms ki bhasha ban jaati hai.

Sabse zyada confusion depth aur height mein hota hai. Depth matlab "main root se kitna neeche hoon" — upar ki taraf gin ke, root ki depth 0. Height matlab "mere neeche sabse door wala leaf kitni door hai" — neeche ki taraf gin ke, leaf ki height 0. Yeh dono ulti directions hain, isiliye log galti karte hain. Yaad rakho: Depth = Down from root, Height = how far to the bottom.

Degree ka matlab tree mein hota hai kisi node ke kitne children hain (graph theory wala total edges nahi). Pure tree ka degree = sabse zyada children wale node ka count. Aur subtree matlab koi node plus uske neeche ke saare descendants — woh khud ek chhota tree ban jaata hai, isiliye recursion yahan natural fit hai.

Yeh basics kyun important? Kyunki balanced trees (AVL, Red-Black) ka pura khel height ko control karne ka hai taaki search O(logn)O(\log n) rahe. Agar terminology saaf hai, toh traversal, BST, heap — sab cheezein aasaani se samajh aati hain. Pehle vocabulary pakki karo, phir algorithms khud-ba-khud khulte jaayenge.

Go deeper — visual, from zero

Test yourself — Trees

Connections