Trees & Applications of Trees
Circuits and trees, forests, the unique-path theorem, rooted trees and family terminology, subtrees, ordered and binary trees, and binary search trees with the insertion algorithm.
▶Lecture videos
Click any timestamp — here or inside the notes — to jump the video to that moment. Your position is remembered.
Video Lecture 1 – Trees & terminology
What a tree is, how to identify one, rooted trees and terminology.
00:01Introduction and learning outcomes01:30Connected graphs and paths (recap)03:01What a tree is: connected, no simple circuits03:43Example: which of G1–G4 are trees?07:45Applications: decision and regression trees09:24A forest: a graph whose components are trees10:27Checking a graph against the tree definition13:20Rooted trees and their applications14:17Terminology: parent and child15:40Siblings16:28Ancestors17:18Descendants18:04Leaves and internal vertices19:20Worked example: naming every part20:34Next: ordered rooted treesVideo Lecture 2 – Ordered trees & binary search trees
Ordered rooted trees, a worked terminology exercise, binary search trees, decision and game trees.
00:01Ordered rooted trees: left child, right child02:11Exercise: which graphs are trees? (connected + no simple circuit)04:32Rooted tree Q&A: root, internal vertices, leaves05:22Children, parent, siblings, ancestors, descendants07:56Binary search trees: goal and definition08:57Key > left subtree and < right subtree11:29Worked example: mathematics, physics, geography…12:59Other applications: decision trees, game trees14:24Exercise answer: banana, apple, peach…1Trees
- A circuit is a path that begins and ends at the same vertex.
- A tree is a connected graph that contains no simple circuits.
History and uses:
- Arthur Cayley (1857) used trees to count chemical compounds.
- Computer science: efficient algorithms for locating items in a list.
- Games such as checkers and chess: finding winning strategies.
- Modelling sequences of decisions, e.g. the complexity of sorting algorithms.
- Family trees (genealogical charts): vertices are people, edges are parent–child links.
A tree can't contain a simple circuit, so it can't have multiple edges or loops. Every tree is a simple graph.
| Graph | Tree? | Reason |
|---|---|---|
| G₁ | ✓ | Connected, no simple circuits |
| G₂ | ✓ | Connected, no simple circuits |
| G₃ | ✗ | e, b, a, d, e is a simple circuit |
| G₄ | ✗ | Not connected |
Forests
A graph with no simple circuits that is not necessarily connected is a forest. Each connected component is a tree. (Figure 3: one graph, three components, three trees.)
An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.
A graph with n vertices is a tree if any two of these hold:
- It is connected.
- It has no simple circuits.
- It has exactly n − 1 edges.
2Rooted trees & terminology
A rooted tree is a tree in which one vertex is designated as the root and every edge is directed away from the root.
- Any vertex can be chosen as the root, and different roots give different rooted trees (Figure 4: the same tree T rooted at a and at c).
- We draw the root at the top and omit the arrows: the choice of root determines their directions.
| Term | Definition |
|---|---|
| Parent of v | The unique vertex u with a directed edge u → v |
| Child of u | v is a child of u if u is v's parent |
| Siblings | Vertices with the same parent |
| Ancestors of v | Vertices on the path from the root to v, excluding v, including the root |
| Descendants of v | Vertices that have v as an ancestor |
| Leaf | A vertex with no children |
| Internal vertex | A vertex with children. The root is internal unless it is the only vertex (then it's a leaf) |
| Subtree rooted at a | a, all its descendants, and all edges incident to them |
Click any vertex to see its relatives.
| Question | Answer |
|---|---|
| Parent of c | b |
| Children of g | h, i, j |
| Siblings of h | i, j |
| Ancestors of e | c, b, a |
| Descendants of b | c, d, e |
| Internal vertices | a, b, c, g, h, j |
| Leaves | d, e, f, i, k, l, m |
| Subtree rooted at g | g with children h, i, j; h → k; j → l, m (Figure 6) |
3Ordered rooted trees & binary trees
| Concept | Definition |
|---|---|
| Ordered rooted tree | A rooted tree where the children of each internal vertex are ordered, drawn left to right |
| Binary tree (ordered binary tree) | If an internal vertex has two children, the first is the left child and the second is the right child |
| Left / right subtree | The tree rooted at the left / right child of a vertex |
In some applications every non-root vertex is labelled left or right even if it is an only child.
- The left child of d is f and the right child is g.
- The left subtree of c is rooted at h (h → j).
- The right subtree of c is rooted at i (i → k, l; l → m).
4Binary search trees (BST)
Goal: search a list efficiently when the items are totally ordered.
A binary tree where:
- each child is designated left or right
- no vertex has more than one left or one right child
- each vertex is labelled with a key (one of the items)
The key of each vertex is larger than every key in its left subtree and smaller than every key in its right subtree.
Building one (insertion)
- The first item becomes the root.
- For each new item, start at the root:
- Smaller → go left; larger → go right.
- Repeat until the needed child doesn't exist.
- Insert the item there as a new left or right child.
- mathematics → root
- physics > mathematics → right child of mathematics
- geography < mathematics → left child of mathematics
- zoology > mathematics, > physics → right child of physics
- meteorology > mathematics, < physics → left child of physics
- geology < mathematics, > geography → right child of geography
- psychology > mathematics, > physics, < zoology → left child of zoology
- chemistry < mathematics, < geography → left child of geography
mathematics
/ \
geography physics
/ \ / \
chemistry geology meteorology zoology
/
psychology
Algorithm 1 — locating and adding an item
procedure insertion(T: binary search tree, x: item)
v := root of T {a vertex not present in T has the value null}
while v ≠ null and label(v) ≠ x
begin
if x < label(v) then
if left child of v ≠ null then v := left child of v
else add new vertex as a left child of v and set v := null
else
if right child of v ≠ null then v := right child of v
else add new vertex as a right child of v and set v := null
end
if root of T = null then add a vertex v to the tree and label it with x
else if v is null or label(v) ≠ x then label new vertex with x and let v be this new vertex
{v = location of x}
- At mathematics: oceanography > mathematics → go right to physics.
- At physics: oceanography < physics → go left to meteorology.
- At meteorology: oceanography > meteorology. There's no right child, so add a new right child of meteorology. Set v := null and exit the loop.
- Label the new vertex oceanography.
- Decision trees: each internal vertex is a decision, with a subtree for each possible outcome.
- Game trees: vertices are positions in a game; edges are legal moves.
Expect:
- "Which of these graphs are trees?" Check connected + no simple circuit, and use n − 1 edges.
- Terminology on a given rooted tree: root, internal vertices, leaves, children, parent, siblings, ancestors, descendants, subtree.
- Left and right children and subtrees in a binary tree.
- Building a BST from a list (alphabetical or numeric), showing each comparison.
- Inserting a new item with Algorithm 1.
5Quick revision sheet
| Concept | Remember it as |
|---|---|
| Circuit | Path starting and ending at the same vertex |
| Tree | Connected + no simple circuits (always a simple graph) |
| Forest | No simple circuits, maybe disconnected; each component is a tree |
| Theorem 1 | Tree ⇔ unique simple path between any two vertices |
| Edge count | A tree with n vertices has n − 1 edges |
| Rooted tree | One root; edges directed away; drawn root on top |
| Family terms | Parent, child, siblings, ancestors (to root), descendants |
| Leaf / internal | No children / has children |
| Binary tree | Ordered; left child and right child; left and right subtrees |
| BST | Left subtree < key < right subtree; insert by comparing from the root |
| Example root | mathematics; chemistry ends up left of geography |
| Applications | Searching (BST), decision trees, game trees, chemistry (Cayley), family trees |
?Self-check quiz
11 questions on this week. Answer them all, then press Check my answers. Your best and latest scores are saved and shown on the subject page.