You are on page 1of 30

Department of Computer Science CS1005 Logic and Computation

Trees - Introduction
Lecture 18

Dr Lorraine Ayad
lorraine.ayad@brunel.ac.uk
1

Department of Computer Science Definition

A tree (T) is an undirected graph that contains no cycles and is connected.

Tree Not a tree


2

Department of Computer Science Examples

Trees can be used to represent or break down a range of problems or concepts.


3

Department of Computer Science Is it a Tree?

There are three main properties to consider to check if a graph is a tree:

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.

3. Removing any edge from a tree produces a disconnected graph.


4

Department of Computer Science Rooted Trees

Rooted trees are a very common type of tree, where one vertex is specified as a root.

This is usually the vertex at the top of a tree.

root

A tree with n vertices, will have n-1 edges.


5

Department of Computer Science Definitions

We will consider rooted trees for the remainder of this lecture.

root
level=0

C and B are children of A as C and B are adjacent to A and


level=1 have a depth higher than A by 1.

level=2 G is the parent of J and K as G is adjacent to J and K and


has a depth lower than J and K by 1.

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

Department of Computer Science Definitions

More definitions below.

level=0 A is an internal vertex as it has at least one child.

level=1

C lies on the unique path between K and the root, so C is


an ancestor of K and K is a descendant of C.
level=2

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

Department of Computer Science Definitions


A subtree of a tree T is a tree consisting of a vertex in T and all of its descendants.

A subtree has been highlighted above in green, where C is the root of this subtree.
8

Department of Computer Science Question

List all the vertices that are leaves.


9

Department of Computer Science Question

Which vertex is the parent of E?


10

Department of Computer Science Question

List all the vertices that are descendants of vertex B.


11

Department of Computer Science Binary Trees


A binary tree is a rooted tree in which every parent has at most two children.

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.

Binary tree Full binary tree Full 3-ary tree


12

Department of Computer Science Binary Tree Calculations

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

Department of Computer Science Question

A full binary tree with 6 internal vertices has how many vertices in total?
14

Department of Computer Science Binary Tree Calculations


For all integers h ≥ 0, if T is any binary tree with a height h and t leaves, then t ≤ 2h.

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

Department of Computer Science Question

A full binary tree with a height of 4 will have at most how many leaves?
16

Department of Computer Science Purpose of Binary Trees

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.

The binary tree above represents the equation (A-(B/C))x(D+E)


17

Department of Computer Science Question

What is the answer to the mathematical equation represented by the tree below?
18

Department of Computer Science Traversing a Tree

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

Department of Computer Science Preorder Traversal


Preorder(tree)
1. Visit the root
2. Preorder(left subtree)
3. Preorder(right subtree(s))

A, B, D, E, I, J, F, C, G, K, L, H
20

Department of Computer Science Preorder Traversal


Place a horizontal line on the left side of each vertex. Start from the root and draw a line around
the skeleton of the tree. When you meet a horizontal line, add its corresponding vertex to the
traversal list. Stop when you reach the top of the root.

A, B, D, E, I, J, F, C, G, K, L, H
21

Department of Computer Science Question


List the preorder traversal of the tree below.
22

Department of Computer Science Inorder Traversal


Inorder(tree)
1. Inorder(left subtree)
2. Visit the root
3. Inorder(right subtree(s))

D, B, I, E, J, F, A, K, G, L, C, H
23

Department of Computer Science Inorder Traversal


Place a vertical line under each vertex. Start from the root and draw a line around the skeleton of
the tree. When you meet a vertical line, add its corresponding vertex to the traversal list. Stop
when you reach the top of the root.

D, B, I, E, J, F, A, K, G, L, C, H
24

Department of Computer Science Question


List the inorder traversal of the tree below.
25

Department of Computer Science Postorder Traversal


Postorder(tree)
1. Postorder(left subtree)
2. Postorder(right subtree(s))
3. Visit the root

D, I, J, E, F, B, K, L, G, H, C, A
26

Department of Computer Science Postorder Traversal


Place a horizontal line on the right side of each vertex. Start from the root and draw a line around
the skeleton of the tree. When you meet a vertical line, add its corresponding vertex to the
traversal list. Stop when you reach the top of the root.

D, I, J, E, F, B, K, L, G, H, C, A
27

Department of Computer Science Question


List the postorder traversal of the tree below.
28

Department of Computer Science Summary


This lecture has covered the following topics:

 Trees and their properties.


 Binary trees and binary tree calculations.
 Preorder traversal of a tree.
 Inorder traversal of a tree.
 Postorder traversal of a tree

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:

Susanna S. Epp, Discrete Mathematics with Applications. Fourth Edition. Chapter 10 –


Graphs and Trees. p683-700.

Peter Grossman, Discrete Mathematics for Computing. Third Edition. Chapter 11 – Trees.
p206-219.

© 2021 Lorraine Ayad, All rights reserved

You might also like