AQA 7517, section 4.2.5.1
A tree is a special kind of graph. It is connected (there is a path between every pair of vertices), undirected (edges have no direction) and has no cycles (you can't follow edges and end up back where you started without retracing your steps).
A tree with vertices always has exactly edges. Note that a tree doesn't have to have a root.
Rooted trees
A rooted tree is a tree in which one vertex has been chosen as the root. This gives the tree a top-down shape, with parent-child relationships.
| Term | Meaning |
|---|---|
| Root | The top node, the only one with no parent |
| Parent | A node directly above another |
| Child | A node directly below another |
| Leaf | A node with no children |
| Subtree | A node and all its descendants |
| Descendant | Any node below a given node |
Every node except the root has exactly one parent, and all other nodes are descendants of the root.
Binary trees
A binary tree is a rooted tree in which each node has at most two children, usually called the left child and the right child.
A common use is the binary search tree, where for every node, everything in the left subtree is smaller and everything in the right subtree is larger. This makes searching quick, because each comparison rules out half of what's left.
Representing a binary tree
Each node stores a value and pointers to its left and right children.
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
root = Node(8)
root.left = Node(3)
root.right = Node(10)
root.left.left = Node(1)
A binary tree can also be stored in an array, or in three parallel arrays holding the value, left pointer and right pointer.
Typical uses of rooted trees
- File systems: folders contain files and other folders.
- Binary search trees: fast searching and sorting.
- Syntax trees: a compiler shows the structure of an expression or a program.
- Decision trees: each branch is a choice.
- Organisation charts and family trees: things with parent-child relationships.
- Game trees: all the possible moves in a game.
Worked example
Task: insert the values 5, 2, 8, 1, 9 (in that order) into an empty binary search tree.
- 5 becomes the root.
- 2 is less than 5, so it goes to the left of 5.
- 8 is greater than 5, so it goes to the right of 5.
- 1 is less than 5, then less than 2, so it goes to the left of 2.
- 9 is greater than 5, then greater than 8, so it goes to the right of 8.
The leaves are 1 and 9. The tree has 5 nodes and so 4 edges, which is as expected.
Exam tip: be ready to quote the definition exactly: a tree is a connected, undirected graph with no cycles. Describe a binary tree as a rooted tree where each node has at most two children.
Check yourself
What is a tree?
A connected, undirected graph with no cycles.
What is a rooted tree, and which node has no parent?
A tree in which one vertex has been chosen as the root, which gives parent-child relationships between the nodes. The root is the only node with no parent.
What is a binary tree?
A rooted tree in which each node has at most two children.
Give two uses of rooted trees
Any two of: file systems, binary search trees, syntax trees in compilers, decision trees, organisation charts, game trees.