Professional Documents
Culture Documents
Trees - Introduction
Lecture 18
Dr Lorraine Ayad
lorraine.ayad@brunel.ac.uk
1
1. For each pair of vertices u and v in a tree, there is exactly one path from u to v that does
not repeat any edges or vertices.
2. Inserting an edge between any two vertices of a tree produces a graph that contains a
cycle.
Rooted trees are a very common type of tree, where one vertex is specified as a root.
root
root
level=0
level=3
The level (or depth) of a vertex is the number of edges along the unique path between it and the root.
The height is the maximum depth of any vertex of the tree.
6
level=1
level=3
H is a leaf as has no children. J and K are siblings as they are two distinct vertices
which have the same parent.
7
A subtree has been highlighted above in green, where C is the root of this subtree.
8
Each child in a binary tree is either a left child or a right child and every parent has at most one left
child and one right child.
A full binary tree is a binary tree in which each parent has exactly two children. A full m-ary tree is
a tree in which each parent has exactly m children.
A full binary tree with n internal vertices contains 2n+1 vertices in total.
Why? We have n parents, each parent has exactly 2 children (2n is the total number of
vertices which are children). Plus 1 which is the root.
A full m-ary tree with n internal vertices contains mn+1 vertices in total.
Why? We have n parents, each parent has exactly m children (mn is the total number of
vertices which are children). Plus 1 which is the root.
13
A full binary tree with 6 internal vertices has how many vertices in total?
14
Why? As we go up each level in the tree, we increase the number of total vertices at most
by doubling the number of vertices which were at the previous level.
Level 0
1 vertex 20=1
Level 1
2 vertices 21=2
Level 2 22=4
4 vertices
Level 3
8 vertices 23=8
15
A full binary tree with a height of 4 will have at most how many leaves?
16
A binary tree can be used to represent a mathematical or logic equation. The internal
vertices are arithmetic operators, the leaves are variables, and the operator at each vertex
acts on its left and right subtrees in left-right-order.
What is the answer to the mathematical equation represented by the tree below?
18
The way in which a computer processes the expression, corresponds to visiting the vertices
of the tree in a specific order.
A procedure for visiting all the vertices in a rooted tree is called traversing the tree.
There are three main traversals for a rooted tree that we will look at in this lecture.
19
A, B, D, E, I, J, F, C, G, K, L, H
20
A, B, D, E, I, J, F, C, G, K, L, H
21
D, B, I, E, J, F, A, K, G, L, C, H
23
D, B, I, E, J, F, A, K, G, L, C, H
24
D, I, J, E, F, B, K, L, G, H, C, A
26
D, I, J, E, F, B, K, L, G, H, C, A
27
The next lecture will look into the applications of trees and how we can use them to solve
algorithmic problems.
Department of Computer Science References
These slides were assembled using the following sources:
Peter Grossman, Discrete Mathematics for Computing. Third Edition. Chapter 11 – Trees.
p206-219.