Trees
Trees
Trees
What is a tree?
• Tree is a discrete structure that represents hierarchical
relationships between individual elements or nodes.
• A tree in which a parent has no more than two children is called
a binary tree.
• A tree is a connected graph containing no cycles.
• A forest is a graph containing no cycles. Note that this means that a
connected forest is a tree.
Tree and its Properties
• A Tree is a connected acyclic undirected graph.
• There is a unique path between every pair of vertices in G.
• A tree with N number of vertices contains (N−1) number of
edges.
• The vertex which is of 0 degree is called root of the tree.
• The vertex which is of 1 degree is called leaf node of the tree
and the degree of an internal node is at least 2.
Tree and its Properties
• A graph T is a tree if and only if between every pair of distinct vertices
of T there is a unique path.
• A graph F is a forest if and only if between any pair of vertices
in F there is at most one path.
Example − tree
• The following is an example of a tree −
Forest
Graphs containing no simple circuits that
are not connected, but each connected
component is a tree.
Theorem
g
a
e f e c
b
b
d
a d
c g
f
root node
a
internal vertex parent of g
b c
d e f g
leaf
siblings
h i
a
b c
d e f g
h i ancestors of h and i
a
b c
d e f g
level 2
level 4
Properties of Trees
Example:
the tree on the right has height 3
Rooted Tree
• A rooted tree G is a connected acyclic graph with a special
node that is called the root of the tree and every edge directly or
indirectly originates from the root.
• An ordered rooted tree is a rooted tree where the children of
each internal vertex are ordered.
Rooted Tree
• If every internal vertex of a rooted tree has not more than m
children, it is called an m-ary tree.
• If every internal vertex of a rooted tree has exactly m children, it
is called a full m-ary tree.
• If m=2, the rooted tree is called a binary tree.
Rooted Tree
Terminology
Parent
Ancestor
Child
Descendant
Siblings
Terminal vertices
Internal vertices
Subtrees
Internal and external vertices
An internal vertex is a
vertex that has at least one
child
A terminal vertex is a
vertex that has no children
The tree in the example
has 4 internal vertices and
4 terminal vertices
Subtrees
A subtree of a tree T is a tree T' such that
V(T') V(T) and
E(T') E(T)
Characterization of trees
Theorem
If T is a graph with n vertices, the following are
equivalent:
a) T is a tree
b) T is connected and acyclic
(“acyclic” = having no cycles)
c) T is connected and has n-1 edges
d) T is acyclic and has n-1 edges
7.3 Spanning trees
48
Introduction (Cont’d)
• To traverse a graph is to systematically visit and process each node in the
graph exactly once.
• There are two common graph traversal algorithms that are applicable to both directed and
undirected graphs :
• BreadthFirst Traversal (BFS)
• DepthFirst Traversal (DFS)
• PreOrder DepthFirst Traversal
• PostOrder DepthFirst Traversal
• We shall define General traversal algorithms [dfsAllVertices and bfsAllVertices] that will
visit each vertex of a graph G in those algorithms that require each vertex to be visited.
These algorithms will be of the form:
for(i = 0; i < numberOfVertices; i++){
if(vertexi is not visited)
dfsPreOrder(G, vertexi); // or dfsPostOrder(G, vertexi) or bsf(G, vertexi)
}
49
Introduction (Cont’d)
The BFS and DFS traversal of a graph G is not unique. A traversal depends both
on the starting vertex, and on the order of traversing the adjacent vertices of each
node.
50
Breadth-First Traversal Algorithm
• In this method, After visiting a vertex v, we must visit all its adjacent vertices w1, w2,
w3, ..., before going down to the next level to visit vertices adjacent to w1 etc.
BFS-tree:
B D E
Order of C G
52
Depth-First Traversal Algorithm
• A DFS starting at a vertex v first visits v, then some neighbour w of v, then some
neighbour x of w that has not been visited before, etc. When it gets stuck, the DFS
backtracks until it finds the first vertex that still has a neighbour that has not been
visited before. It continues with this neighbour until it has to backtrack again.
Eventually, it will visit all vertices reachable from v
• Note: Adjacent vertices can be pushed in any order; but to obtain a unique traversal, we
will push them in reverse alphabetical order.
53
Example
• Depth-first traversal using an explicit stack.
Order of
A B C F E G D H I Stack
Traversal