0% found this document useful (0 votes)
69 views12 pages

DS Lecture16

The document discusses different types of trees including binary trees and their properties. It provides examples of trees, non-trees, rooted trees and binary trees. It also includes exercises with solutions related to properties of trees.

Uploaded by

Usama Mushtaq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views12 pages

DS Lecture16

The document discusses different types of trees including binary trees and their properties. It provides examples of trees, non-trees, rooted trees and binary trees. It also includes exercises with solutions related to properties of trees.

Uploaded by

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

APPLICATION AREAS OF TREES

Trees are used to solve problems in a wide variety of disciplines. In


computer science trees are employed to
•construct efficient algorithms for locating items in a list.
•construct networks with the least expensive set of telephone lines linking
distributed computers.
•construct efficient codes for storing and transmitting data.
•model procedures that are carried out using a sequence of decisions, which
are valuable in in the study of sorting algorithms.
TREE

A tree is a connected graph that does not contain any nontrivial circuit. (i.e.,
it is circuit-free)
A trivial circuit is one that consists of a single vertex.

TREE

TREE

1
TREE
EXAMPLES OF NON TREES

(a) Graph with a circuit (b) Disconnected graph

(c) Graph with a circuit

SOME SPECIAL TREES

1. TRIVIAL TREE
A graph that consists of a single vertex is called a trivial
tree or degenerate tree.
2. EMPTY TREE
A tree that does not have any vertices or edges is called an
empty tree.
3. FOREST
A graph is called a forest if, and only if, it is circuit-free.
Hence, the connected components of a forest are trees.
2
A forest

In the above example of forest it has only two trees in it and not a very thick
forest.
PROPERTIES OF TREES

1. A tree with n vertices has n - 1 edges.


2. Any connected graph with n vertices and n - 1 edges is a tree.
3. A tree has no nontrivial circuit; but if one new edge is added to
it, then the resulting graph has exactly one nontrivial circuit.
4. A tree is connected, but if any edge is deleted from it, then the
resulting graph is not connected.
5. Any tree that has more than one vertex has at least two vertices
of degree 1.
6. A graph is a tree iff there is a unique path between any two of its
vertices.
3
EXERCISE

Explain why graphs with the given specification do not exist.


1. tree, twelve vertices, fifteen edges.
2. tree, five vertices, total degree 10.
SOLUTION
1. Any tree with 12 vertices will have 12 - 1 = 11 edges, not 15.
2. Any tree with 5 vertices will have 5 - 1 = 4 edges.
Since, total degree of graph = 2 (No. of edges)
= 2(4) = 8
Hence, a tree with 5 vertices would have a total degree 8, not 10.
EXERCISE

Find all non isomorphic trees with four vertices.


SOLUTION
Any tree with four vertices has three edges. Thus, the total degree of a tree
with 4 vertices must be 6.
Also, every tree with more than one vertex has at least two vertices of
degree 1, so the only possible combinations of degrees for the vertices of
the trees are 1, 1, 1, 3 and 1, 1, 2, 2.
The corresponding trees (clearly non-isomorphic) are

and

4
EXERCISE

Find all non-isomorphic trees with five vertices.


SOLUTION
There are three non-isomorphic trees with five vertices as shown.
(a)

(b)

(c)

EXERCISE

Draw a graph with six vertices, five edges that is not a tree.
SOLUTION
Two such graphs are:

(Disconnected graphs with a circuit) 5


DEFINITION

A vertex of degree 1 in a tree is called a terminal vertex or a leaf and a


vertex of degree greater than 1 in a tree is called an internal vertex or a
branch vertex.
EXAMPLE
The terminal vertices of the tree are v1, v2, v5, v6 and v8 and internal vertices
are v3, v4, v7
v2
v1 v4

v5
v3
v7

v6 v8
ROOTED TREE

A rooted tree is a tree in which one vertex is distinguished from the others
and is called the root.
The level of a vertex is the number of edges along the unique path between
it and the root.
The height of a rooted tree is the maximum level to any vertex of the tree.
The children of any internal vertex v are all those vertices that are adjacent
to v and are one level farther away from the root than v.
If w is a child of v, then v is called the parent of w.
Two vertices that are both children of the same parent are called siblings.
Given vertices v and w, if v lies on the unique path between w and the6 root,
then v is an ancestor of w and w is a descendant of v.
Examples

Consider the graph tree given below

We redraw the tree as and see what are the relations, in order to redraw the
graph we choose a vertex as root and draw the graph from upward to
downward as given below.

root level 0

u
level 1
v w level 2
level 3

level 4

v is a child of u
Vertices in enclosed region
u is the parent of v
are descendants of u, which
v and w are siblings
is an ancestor of each
height = 4

7
EXERCISE

Consider the rooted tree shown below with root v 0


a. What is the level of v8? b. What is the level of v0?
c. What is the height of this tree? d. What are the children of
v10?
e. What are the siblings of v1? v0 f. What are the descendants of v12?
Level 0

v1 v2
Level 1
v3 v5 v6
Level 2 v4 v12
Level 3 v10
v7 v8 v9 v11
v17
Level 4
v13 v14 v15 v16
Level 5
v18 v19
(a) What is the level of v8?
From above figure it is clear that the level of v 8 is 3.
(b) What is the level of v0?
Clearly its level is 0.

(c) What is the height of this tree?


Height of the tree is 5. Because maximum level is 5.
(d) What are the children of v10?
Clearly the children of v10 are v14,v13,v16.
(e) What are the siblings of v1?
8
v2 is only sibling of the v1.
(f) What are the descendants of v12? Decedents of are v17,v18,v19.
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.
A full binary tree is a binary tree in which each internal vertex has exactly
two children.
EXAMPLE
root

u w

v
x

left subtree of w right subtree of w

THEOREMS

1. If k is a positive integer and T is a full binary tree with k


internal vertices, then T has a total of 2k + 1 vertices and has k +
1 terminal vertices.
2. If T is a binary tree that has t terminal vertices and height h, then
t  2h
Equivalently,
log2 t  h
9
EXERCISE

Explain why graphs with the given specification do not exist.


1. full binary tree, nine vertices, five internal vertices.
2. binary tree, height 4, eighteen terminal vertices.
SOLUTION
1. Any full binary tree with five internal vertices has six terminal
vertices, for a total of eleven, not nine vertices in all.
Thus there is no full binary tree with the given properties.
2. Any binary tree of height 4 has at most 2 4 = 16 terminal vertices.
Hence, there is no binary tree that has height 4 and eighteen terminal
vertices.
EXERCISE

Draw a full binary tree with seven vertices.


SOLUTION
Total vertices = 2k + 1 = 7
 k=3
Hence, number of internal vertices = k = 3
and number of terminal vertices = k + 1 = 4. Hence, a full binary tree with
seven vertices is
a (root)

b c

d e f g 10
EXERCISE

Draw a binary tree with height 3 and having seven terminal vertices.
SOLUTION
a

c b

d f g
e

h i j k l m n

REPRESENTATION OF ALGEBRAIC EXPRESSIONS BY BINARY


TREES

/
/

a b
a
+

Binary tree for a/b c


d

Binary tree for a/(c+d)


11
EXERCISE

Draw a binary tree to represent the following expression


a/(b-c.d)
SOLUTION

c
d

Note that the internal vertices are arithmetic operators, the terminal vertices
are variables and the operator at each vertex acts on its left and right sub
trees in left-right order.

12

You might also like