Trees
Rooted, branchy, no cycles
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
satisfying:
1. There is exactly one initial node (the root): .
2. Every other node has .
Equivalently (Lemma 6.2.10): for any node other than the root, there is
exactly one path from the root to .
Tree vocabulary (Definition 6.3.7):
- Parent of : vertex with (so is a child of ).
- 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
vertices has exactly edges. This makes trees the minimum connected
graph on 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.
- 0.0sTrees grow level by level
- 1.5sRoot R appears at the top
- 2.5sLevel 1: children A and B sprout, edges connect them to R
- 3.5sLevel 2: nodes C, D, and E grow under A and B
- 5.0sLevel 3: F under C, G under D — these are the leaves
- 6.0sEdges flash cyan one by one to highlight parent-child links
- 8.5sEach node has at most one parent; every root-to-node path is unique
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 arounda + 5match. The expression3 * 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.
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):
<book>
<title>SMAI</title>
</book>
Becomes the tree:
book / \\ title | SMAI
Every bracket becomes a tree node; bracket-less content becomes a leaf. This same rule applies to:
- Arithmetic: → 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.