0% found this document useful (0 votes)
28 views

Lecture-15-DM Trees

A spanning tree of a graph is a subgraph that is a tree and connects all the vertices together. It uses the minimum possible number of edges to connect all vertices without forming any cycles. Some key properties of spanning trees are: - They must connect all vertices of the original graph together - They cannot contain any cycles (must be a tree) - They use exactly n-1 edges in a graph with n vertices

Uploaded by

Areeba Khaliq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lecture-15-DM Trees

A spanning tree of a graph is a subgraph that is a tree and connects all the vertices together. It uses the minimum possible number of edges to connect all vertices without forming any cycles. Some key properties of spanning trees are: - They must connect all vertices of the original graph together - They cannot contain any cycles (must be a tree) - They use exactly n-1 edges in a graph with n vertices

Uploaded by

Areeba Khaliq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Trees

1
Connected
Graph
■ An undirected graph is called connected if there is a path between
every pair of distinct vertices of the graph.
• The graph G1 is connected.
• The graph G2 is not connected. (For
instance, there is no path in G2
between vertices a and d.)

2
Introduction to
Trees
■ In this lecture we will focus on a particular type of graph called a tree,
so named because such graphs resemble trees.
■ For example, family trees are graphs that represent
genealogical
■ charts.
A tree is an undirected graph T such
that
– T has no simple circuits Tre
■ A– tree
T iscannot
connected e tree
contain multiple edges or loops. Therefore any
must be a simple graph.
■ A tree with n vertices has n - 1 edges (where n ≥0).
■ Theorem: An undirected graph is a tree iff there is a unique simple
path between any two of its vertices.

3
Example 1
Which of the graphs are
trees?

■ G1 and G2 are trees, because both are connected graphs with no simple
circuits.
■ G3 is not a tree because e, b, a, d, e is a simple circuit in this graph.
■ G4 is not a tree because it is not connected.

4
Fores
t■ A–forest is an undirected graph such that
It has no simple circuits
– It is not necessarily connected
■ The connected components of a forest are
trees A Forest
One graph with 3 trees

5
Rooted
Tree
■ A rooted tree is a tree in which one vertex has been designated as the
root and every edge is directed away from the root.
■ The terminology for trees has botanical and genealogical origins,
like parent, child, siblings, descendants and ancestors.

With root With root


c
a
a c
a
b c d b
e
e f d
f g
g
Tree and Rooted
Tree

6
Rooted Tree
■ Suppose that T is a rooted tree. If v is a vertex in T other than the root, the parent of v is the unique vertex u
Terminologies
such that there is a directed edge from u to v.
■ When u is the parent of v, v is called a child of u. Vertices with the same parent are called siblings.
■ The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex,
excluding the vertex itself and including the root.
■ The descendants of a vertex v are those vertices that have v as an ancestor.
■ A vertex of a rooted tree is called a leaf if it has no children.
■ Vertices that have children are called internal vertices.
■ The root is an internal vertex unless it is the only vertex in the graph, in which case it is a leaf.
■ If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree consisting of a and its
descendants and all edges incident to these descendants.
■ The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. The level
of the root is defined to be zero.
■ The height of a rooted tree is the maximum of the levels of vertices. In other words, the height of a rooted
tree is the length of the longest path from the root to any vertex.

7
Rooted
Tree
Leaf

Internal Vertex

The height of a rooted tree is the maximum level


of
any vertex of the tree.

8
Subtre
e

A Rooted Tree The Subtree Rooted at


T g

9
Example
■ In the rooted tree T (with root a) shown in Figure, find the parent of c,
2the children of g, the siblings of h, all ancestors of e, all descendants
of b, all internal vertices, and all leaves. What is the subtree rooted at
g?

10
Example
■ In the rooted tree T (with root a) shown in Figure, find the parent of c,
2the children of g, the siblings of h, all ancestors of e, all descendants
of b, all internal vertices, and all leaves. What is the subtree rooted at
g?
■ SOLUTION:
■ Parent of c is b
■ The children of g are h, i, j
■ The siblings of h are i, j
■ All ancestors of e are c, b, a
■ All descendants of b are c, d, e
■ All internal vertices are a, b, g, c, h, j
■ All leaves are d, e, f, k, l, i, m
■ Subtree rooted at g is
11
Example
3

12
Example
3

13
Example
■ Find the level of each vertex in the rooted tree shown in Figure. What is
4 the height of this tree?

14
Example
■ Find the level of each vertex in the rooted tree shown in Figure. What is
4 the height of this tree?
The root a is at level 0.
Vertices b, j , and k are at level 1.
Vertices c, e, f , and l are at level
2.
Vertices d, g, i, m, and n are at level
3. Finally, vertex h is at level 4.
Because the largest level of any vertex is 4,
this tree has height 4.

15
Rooted
Tree
■ A rooted tree is called an m-ary tree if every internal vertex has no more
than m children.
■ The tree is called a full m-arytree if every internal vertex has
exactly m children.
■ An m-ary tree with m = 2 is called a binary tree.
■ In an ordered binary tree (usually called just a binary tree), if an internal
vertex has two children, the first child is called the left child and the second
child is called the right child.
■ The tree rooted at the left child of a vertex is called the left subtree of this
vertex, and the tree rooted at the right child of a vertex is called the right
subtree of the vertex.

16
Example
5■ Are the rooted trees in Figure full m-ary trees for some positive integer
m?

17
Example
5 m?
■ Are the rooted trees in Figure full m-ary trees for some positive integer

 T1 is a full binary tree because each of its internal vertices has two children.
 T2 is a full 3-ary tree because each of its internal vertices has three children.
 In T3 each internal vertex has five children, so T3 is a full 5-ary tree.
 T4 is not a full m-ary tree for any m because some of its internal vertices have
two children and others have three children so its just 3-ary tree.

18
Binary
Tree
■ A binary tree is a rooted tree in which every internal vertex has at most two
children.
■ Every child in a binary tree is designated either a left child or a right child (but not
both).
■ A full binary tree is a binary tree in which each internal vertex has exactly two
children.

19
Example
6■ What are the left and right children of d in the binary tree T shown in
Figure (where the order is that implied by the drawing)? What are the left
and right subtrees of c?

20
Example
6 order is that implied by the drawing)? What are the left and right subtrees of c?
■ What are the left and right children of d in the binary tree T shown in Figure (where the

Solution:
The left child of d is f and the right
child is g. We show the left and right
subtrees of c in Figures below
respectively.

Left Subtree of c
Right Subtree of
c
21
Balanced
Tree
■ A rooted m-ary tree of height h is balanced if all leaves are at levels h
or h−1.

22
Example
7■ Which of the rooted trees shown in Figure are
balanced?

23
Example
7 balanced?
■ Which of the rooted trees shown in Figure are

 T1 is balanced, because all its leaves are at levels 3 and 4.


 However, T2 is not balanced, because it has leaves at levels 2, 3, and
4.
 Finally, T3 is balanced, because all its leaves are at level 3.

24
Spanning
Tree
■ Suppose it is required to develop a system of roads between six major cities. A survey
of the area revealed that only the roads shown in the graph could be constructed.

■ For economic reasons, it is desired to construct the least possible number of roads to
connect the six cities. One such set of roads is

Note that the subgraph representing these roads is a tree, it is connected & circuit-free
(six vertices and five edges)

25
Spanning
■Tree
Let G be a simple graph. A spanning tree of G is a subgraph of G that is a
tree containing every vertex of G.
REMARKS:
■ Every connected graph has a spanning tree.
■ A graph may have more than one spanning trees.
■ Any two spanning trees for a graph have the same number of edges.

26
Example
8 Figure.
■ Find a spanning tree of the simple graph G shown in

• The graph G is connected, but it is not a tree because it contains simple circuits.
• Remove the edge {a, e}. This eliminates one simple circuit, and the
resulting subgraph is still connected and still contains every vertex of G.
• Next remove the edge {e, f } to eliminate a second simple circuit.
• Finally, remove edge {c, g} to produce a simple graph with no simple circuits.
• This subgraph (the last figure on right) is one of the many spanning trees that can
be formed, because it is a tree that contains every vertex of G.
27
Example
8
Each of the trees shown below is a spanning tree of
G.

28
Tree
Traversal
■ Ordered rooted trees are often used to store information.
■ We need procedures for visiting each vertex of an ordered rooted tree
to access data.
■ We will study some important algorithms for visiting all the vertices of
an ordered rooted tree.
■ Ordered rooted trees can also be used to represent various types of
expressions such as arithmetic expressions involving numbers,
variables, and operations.

29
Universal Address
Systems
Procedures for traversing all vertices of an ordered rooted tree rely on the orderings of
children. In ordered rooted trees, the children of an internal vertex are shown from left to
right in the drawings.
We will describe one way we can totally order the vertices of an ordered rooted tree.
To
produce this ordering, we must first label all the vertices. We do this recursively:
1.Label the root with the integer 0. Then label its k children (at level 1) from left to
right with 1, 2, 3, . . . , k.
2.For each vertex v at level n with label A, label its kv children, as they are drawn from
left to right, with A.1,A.2, . . . , A.kv.

30
Example
10
■ Display the labeling of the universal address system next to
the vertices in the ordered rooted tree shown in Figure.

31
Example 9
■ Display the labeling of the universal address system next to
the
vertices in the ordered rooted tree shown in Figure.

32
Tree Traversal
Algorithms
■ Procedures for systematically visiting every vertex of an
ordered
rooted tree are called traversal algorithms.
■ We will describe three of the most commonly used such algorithm:
1. Preorder traversal
2. Inorder traversal
3. Postorder traversal

33
Preorder
Traversal
■ In this traversal method, the root node is visited first, then the left subtree and
finally the right subtree.
■ For example, we might wish to make sure that we visit any given
node before we visit its children. This is called a preorder traversal.

34
Preorder
Traversal
■ We start from A, and following pre-order traversal, we first visit A itself and then
move to its left subtree B. B is also traversed pre-order. The process goes on until
all the nodes are visited. The output of pre-order traversal of this tree will be
■ A→ B → D → E → C → F → G

35
Example
11T shown in Figure?
■ In which order does a preorder traversal visit the vertices in the ordered rooted tree

■ Solution:
Preorder traversal: Visit root,
visit subtrees left to right.

36
Inorder
Traversal
■ If a binary tree is traversed in-order, the output will produce sorted key
values in an ascending order.
■ An inorder traversal first visits the left child (including its entire subtree),
then visits the node, and finally visits the right child (including its entire
subtree). The binary search tree makes use of this traversal to print all
nodes in ascending order of value.

37
Inorder
Traversal
■ We start from A, and following in-order traversal, we move to its left
subtree B. B is also traversed in-order. The process goes on until all
the nodes are visited. The output of inorder traversal of this tree will
be
■ D → B → E → A→ F → C → G

38
Example
12T shown in Figure?
■ In which order does a inorder traversal visit the vertices in the ordered rooted tree

Solution:

Inorder traversal: Visit leftmost subtree,


visit root, visit other subtrees left to right

39
Postorder
Traversal
Alternatively, we might wish to visit each node only after we visit its children (and
their subtrees). For example, this would be necessary if we wish to return all nodes
in the tree to free store. We would like to delete the children of a node before
deleting the node itself. But to do that requires that the children's children be deleted
first, and so on. This is called a postorder traversal.

40
Postorder
Traversal
■ We start from A, and following pre-order traversal, we first visit the left subtree B. B is
also traversed post-order. The process goes on until all the nodes are visited. The
output of post-order traversal of this tree will be
■ D → E→ B→ F → G→ C→ A

41
Example
13ordered rooted tree T shown in Figure?
■ In which order does a postorder traversal visit the vertices in the

Solution:

Postorder traversal: Visit subtrees left to


right; visit root.

42
Example
14

43
Example
14

44
Example
15

45
Example
15

 In-order: 4,13,14,15,34,40,45,47,48,49,55

 Pre-order : 40,4,34,14,13,15,45,55,48,47,49

 Post-order: 13, 15, 14, 34, 4, 47, 49, 48, 55, 45,
40
46
Practice Problems
https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeTr
aversal.html
REFERENCE
S
■ Kenneth Rosen Discrete Mathematics and Its Applications – Chapter #
11

48

You might also like