Chapter 6Graphs and Treesnotes.en.pdf:97-99

Trees

Rooted, branchy, no cycles

Def 6.3.4Tree (notes)Def 6.3.7Parent/child
Concept

A tree is a special kind of DAG that captures hierarchical structure. By
Definition 6.2.8 (notes p. 97), a tree is a DAG G=V,EG = \langle V, E \rangle
satisfying:
1. There is exactly one initial node vrVv_r \in V (the root): indeg(vr)=0\text{indeg}(v_r) = 0.
2. Every other node has indeg(v)=1\text{indeg}(v) = 1.

Equivalently (Lemma 6.2.10): for any node vv other than the root, there is
exactly one path from the root to vv.

Tree vocabulary (Definition 6.3.7):
- Parent of ww: vertex vv with (v,w)E(v, w) \in E (so ww is a child of vv).
- Root: the unique initial node (drawn at the top in CS).
- Leaf: a terminal node with no children.
- Ancestor / descendant: transitive closure of parent / child.
- Depth of a node: length of the unique path from the root to it.
- Height of a tree: maximum depth of any node (= length of the longest
root-to-leaf path).

Important fact (the "tree counting" lemma): an undirected tree with nn
vertices has exactly n1n - 1 edges. This makes trees the minimum connected
graph on nn vertices — sparse and acyclic.

Tree examples in AI:
- Parse trees: terminals as leaves, grammatical categories as internal nodes.
- Decision trees: each internal node is a feature test; leaves are predictions.
- Search trees (BFS, DFS): the state space explored, rooted at the start state.
- Abstract syntax trees (ASTs): programs as trees in compilers and theorem provers.

Animation — tree grow
Transcript — click a line to jump7 cues
  1. 0.0sTrees grow level by level
  2. 1.5sRoot R appears at the top
  3. 2.5sLevel 1: children A and B sprout, edges connect them to R
  4. 3.5sLevel 2: nodes C, D, and E grow under A and B
  5. 5.0sLevel 3: F under C, G under D — these are the leaves
  6. 6.0sEdges flash cyan one by one to highlight parent-child links
  7. 8.5sEach node has at most one parent; every root-to-node path is unique
Well-bracketed structures

A structure (text, program, expression) is well-bracketed when every opening bracket has a matching closing bracket of the right kind, with proper nesting. Examples:

  • Arithmetic: 3 * (a + 5) — the parentheses around a + 5 match. The expression 3 * a + 5) has an unmatched ).
  • HTML:
<book> <title>SMAI</title> <author>Kohlhase</author> </book>

Every <tag> has a matching </tag>, properly nested.

  • Python:
if x > 0: print("positive") else: print("non-positive")

The if/else blocks are properly nested.

Observation. All well-bracketed structures share a common data structure: a tree. The brackets enclose subtrees; bracket-less 'atoms' are leaves. This insight unifies expressions, markup, and programs.

Trees as the common data structure

Idea (Obs 3.2). A well-bracketed structure consists of either a bracket-less atom, or a bracket enclosing a sequence of well-bracketed sub-structures.

Example 3.3 (HTML → tree):

&lt;book&gt; &lt;title&gt;SMAI&lt;/title&gt; &lt;/book&gt;

Becomes the tree:

book
/   \\
title
 |
SMAI

Every bracket becomes a tree node; bracket-less content becomes a leaf. This same rule applies to:
- Arithmetic: (2+3)5(2 + 3) * 5 → product(sum(2, 3), 5).
- Programs: if c then a else b → if-node with three subtrees.
- Markup: any well-bracketed XML/HTML.

Definition 3.4. Such structures — inductively built from atoms and bracketed groups — are precisely the trees.

Worked example
Step 0 of 2
Practice — score 100% to advance
Multiple choice
Q1
A tree has how many edges if it has nn vertices?
Q2
The root of a tree has indegree…
Q3
A leaf in a tree is…
Q4
The height of a tree is…
Q5
What kind of tree is used to represent program structure in compilers?
Q6
Why must every non-trivial tree have at least one leaf?
Q7
Which of these is well-bracketed?
Fill in the blank
Q1
A tree on nn vertices has exactly nn - edges.
Q2
The unique vertex with indeg=0\text{indeg} = 0 in a tree is called the ___.
Q3
A vertex with no children is called a ___.
Loading…