Skip to content

Chapter 28. Rooted Trees

A rooted tree is a tree with one distinguished vertex called the root.

The root gives direction to an otherwise undirected structure. In an unrooted tree, no vertex has a privileged role. In a rooted tree, every vertex is understood by its position relative to the root. This turns a tree into a hierarchy.

Rooted trees are used to model ancestry, containment, dependency, syntax, search, decisions, and recursive computation.

28.1 Definition

Let

T=(V,E) T = (V,E)

be a tree.

A rooted tree is a pair

(T,r), (T,r),

where rVr \in V is a distinguished vertex called the root.

The underlying graph TT remains an ordinary tree. The choice of root adds extra structure.

If vv is any vertex of TT, then there is a unique simple path from rr to vv. This path determines the position of vv in the rooted tree.

28.2 Parent and Child

Let vv be a vertex different from the root.

Since there is a unique path from the root rr to vv, there is a unique vertex immediately before vv on that path. This vertex is called the parent of vv.

If uu is the parent of vv, then vv is called a child of uu.

The root has no parent.

A vertex may have zero, one, or many children. A vertex with no children is called a leaf. A vertex with at least one child is called an internal vertex.

28.3 Ancestors and Descendants

Let uu and vv be vertices in a rooted tree.

The vertex uu is an ancestor of vv if uu lies on the unique path from the root to vv.

The vertex vv is then called a descendant of uu.

Every vertex is often counted as both an ancestor and a descendant of itself. When this convention is inconvenient, one uses the terms proper ancestor and proper descendant.

The root is an ancestor of every vertex. Every vertex is a descendant of the root.

28.4 Depth and Height

The depth of a vertex vv is the length of the unique path from the root to vv.

Thus the root has depth

0. 0.

If vv has parent pp, then

depth(v)=depth(p)+1. \operatorname{depth}(v) = \operatorname{depth}(p)+1.

The height of a rooted tree is the maximum depth of any vertex:

height(T)=maxvVdepth(v). \operatorname{height}(T) = \max_{v \in V} \operatorname{depth}(v).

A tree with one vertex has height 00.

The height measures how far the tree extends from its root.

28.5 Levels

The vertices at the same depth form a level.

The level ii of a rooted tree is the set

Li={vV:depth(v)=i}. L_i = \{v \in V : \operatorname{depth}(v)=i\}.

The root is the only vertex at level 00:

L0={r}. L_0 = \{r\}.

Levels provide a natural horizontal decomposition of the tree. They are especially useful in breadth-first search, shortest-path trees, and hierarchical data structures.

28.6 Subtrees

Let vv be a vertex in a rooted tree.

The subtree rooted at vv is the subgraph induced by vv and all descendants of vv.

This subtree is itself a rooted tree with root vv.

Subtrees are the basic units of recursive reasoning. Many proofs and algorithms on rooted trees work by applying the same argument to each subtree.

If the children of vv are

c1,c2,,ck, c_1,c_2,\ldots,c_k,

then the subtree rooted at vv consists of vv together with the subtrees rooted at those children.

28.7 Recursive Description

A rooted tree may be described recursively.

It consists of a root together with a finite collection of rooted subtrees attached to the root.

If the root has children

c1,c2,,ck, c_1,c_2,\ldots,c_k,

then each cic_i is the root of a smaller rooted tree.

This gives the schematic form

T=r(T1,T2,,Tk), T = r(T_1,T_2,\ldots,T_k),

where TiT_i is the subtree rooted at cic_i.

If k=0k=0, then the tree consists only of the root, or the root is a leaf.

This recursive structure explains why rooted trees are central in algorithms and formal languages.

28.8 Ordered and Unordered Rooted Trees

A rooted tree may be ordered or unordered.

In an unordered rooted tree, the children of a vertex form a set. Their order has no meaning.

In an ordered rooted tree, the children of each vertex are arranged in a specified order, usually from left to right.

For example, if a vertex has children aa, bb, and cc, then the ordered list

(a,b,c) (a,b,c)

differs from

(b,a,c). (b,a,c).

Ordered rooted trees appear in syntax trees, expression trees, XML documents, JSON structures, and many parsing systems.

Unordered rooted trees appear in abstract hierarchy, taxonomy, and many mathematical counting problems.

28.9 Rooted Trees as Directed Graphs

A rooted tree can be converted into a directed graph by orienting every edge away from the root.

If uu is the parent of vv, then the undirected edge

{u,v} \{u,v\}

becomes the directed edge

uv. u \to v.

The resulting directed graph has these properties:

PropertyMeaning
Root in-degreeThe root has in-degree 00
Non-root in-degreeEvery other vertex has in-degree 11
AcyclicityThere are no directed cycles
ReachabilityEvery vertex is reachable from the root

Such a directed rooted tree is often called an arborescence.

One may also orient every edge toward the root. In that convention, every non-root vertex points to its parent.

28.10 Lowest Common Ancestor

Let uu and vv be two vertices in a rooted tree.

A common ancestor of uu and vv is a vertex that is an ancestor of both.

The lowest common ancestor, often written

LCA(u,v), \operatorname{LCA}(u,v),

is the common ancestor of uu and vv with greatest depth.

The lowest common ancestor is unique.

Example

If two files lie in different subdirectories, their lowest common ancestor is the deepest directory containing both files.

The lowest common ancestor is important in algorithms, tree queries, phylogenetics, compilers, and network routing.

28.11 Paths in Rooted Trees

In an unrooted tree, every two vertices are joined by a unique simple path.

In a rooted tree, this path can be described using ancestors.

Let uu and vv be vertices, and let

w=LCA(u,v). w = \operatorname{LCA}(u,v).

Then the path from uu to vv goes upward from uu to ww, then downward from ww to vv.

Thus

dist(u,v)=depth(u)+depth(v)2depth(w). \operatorname{dist}(u,v) = \operatorname{depth}(u) + \operatorname{depth}(v) - 2\operatorname{depth}(w).

This formula is widely used in tree algorithms.

28.12 Rooted Tree Traversals

A traversal is a systematic procedure for visiting the vertices of a tree.

For rooted trees, common traversals include:

TraversalRule
PreorderVisit root, then subtrees
PostorderVisit subtrees, then root
Level orderVisit vertices by increasing depth

If the tree is ordered, the subtrees are visited according to the specified child order.

Preorder traversal is natural when a parent must be processed before its children.

Postorder traversal is natural when a parent depends on information computed from its children.

Level-order traversal is the same structure used by breadth-first search.

28.13 Size of a Rooted Tree

The size of a rooted tree is the number of vertices it contains.

The recursive formula for size is

size(T)=1+i=1ksize(Ti), \operatorname{size}(T) = 1 + \sum_{i=1}^{k} \operatorname{size}(T_i),

where T1,T2,,TkT_1,T_2,\ldots,T_k are the subtrees rooted at the children of the root.

The term 11 counts the root.

This formula reflects the decomposition of the tree into root plus child subtrees.

28.14 Height Recursion

The height of a rooted tree can also be computed recursively.

If the root has no children, then

height(T)=0. \operatorname{height}(T)=0.

If the root has child subtrees

T1,T2,,Tk, T_1,T_2,\ldots,T_k,

then

height(T)=1+max1ikheight(Ti). \operatorname{height}(T) = 1 + \max_{1 \leq i \leq k}\operatorname{height}(T_i).

Thus a tree is tall when at least one of its subtrees is tall.

This recursion is used in the analysis of search trees, decision trees, and divide-and-conquer algorithms.

28.15 Branching Factor

The number of children of a vertex is its branching factor.

A rooted tree has maximum branching factor bb if every vertex has at most bb children.

If every internal vertex has exactly bb children, the tree is called a full bb-ary tree.

For example:

Branching factorName
11Path-like rooted tree
22Binary tree
33Ternary tree
bbbb-ary tree

Branching factor controls the growth of levels.

If a rooted tree has branching factor at most bb, then level ii contains at most

bi b^i

vertices.

28.16 Counting Vertices by Height

Suppose a rooted tree has maximum branching factor bb and height hh.

Then the number of vertices is at most

1+b+b2++bh. 1 + b + b^2 + \cdots + b^h.

If b1b \neq 1, this sum is

bh+11b1. \frac{b^{h+1}-1}{b-1}.

If b=1b=1, the tree is a path and has at most

h+1 h+1

vertices.

These formulas are common in the analysis of search algorithms.

28.17 Applications

Rooted trees appear whenever structure has a distinguished origin or hierarchy.

ApplicationRooted tree interpretation
File systemRoot directory and nested folders
Syntax treeGrammar production hierarchy
Decision treeRoot question and branch outcomes
Search treeInitial state and generated states
Organization chartTop-level unit and subordinate units
Phylogenetic treeCommon ancestor and descendants
Web crawlingSeed page and discovered links

The same mathematical structure supports many different interpretations.

28.18 Summary

A rooted tree is a tree with a distinguished vertex called the root.

The root induces parent-child relations, ancestors, descendants, depths, levels, and subtrees. These notions turn a tree into a hierarchical structure.

Rooted trees are naturally recursive. A rooted tree consists of a root and the rooted subtrees attached to its children.

This recursive and hierarchical structure makes rooted trees fundamental in algorithms, data structures, parsing, search, and dependency modeling.