Unit-3: Non-Linear Data Structure
Unit-3: Non-Linear Data Structure
Unit-3: Non-Linear Data Structure
GTU # 3130702
Unit-3
Non-Linear Data Structure
Tree Part-1
1 2 x1 1 1
x3 x1 x3
v1 v2
x2 x2
(a) 2 3 2 3
x4 x5 x4 x5
4 4
1 2 5
(d) (f)
v1 v2
(b)
x1 1 x3 x1 1 x3
x2 x2
2 3 2 3
1 2
v1 v2 x4 x5 x4
4 x5
4
(c)
(e) (g)
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 2
Basic Notations of Graph Theory
Consider diagrams shown in above figure
Every diagrams represent Graphs
Every diagram consists of a set of points which are shown by dots or circles and are
sometimes labelled V1, V2, V3… OR 1,2,3…
In every diagrams, certain pairs of such points are connected by lines or arcs
Note that every arc start at one point and ends at another point
Undirected graph
A graph in which every edge is undirected is called undirected graph e.g. c & f are undirected graphs
Mixed Graph
If some of the edges are directed and some are undirected in graph then the graph is called mixed graph e.g.
d is mixed graph
Weighted Graph
A graph in which weights are assigned to every edge is called weighted graph
Isolated Node
In a graph a node which is not adjacent to any other node is called isolated node
Null Graph
A graph containing only isolated nodes are called null graph. In other words set of edges in null graph is
empty
Let G=(V, E) be a simple digraph such that the terminal node of any edge in the sequence is the
initial node of the edge, if any appearing next in the sequence defined as path of the graph.
Length of Path
The number of edges appearing in the sequence of the path is called length of path.
𝑽𝟎 Root Node
𝑽𝟏 𝑽𝟕
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗
Terminal or
𝑽𝟓 𝑽𝟔 𝑽 𝟏𝟎 Leaf Node
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree Part-1) 11
Tree– Concepts & Definitions
𝑽𝟓 𝑽𝟔 𝑽 𝟏𝟎 𝑽𝟎 𝑽𝟎
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 𝑽𝟏 𝑽𝟕 𝑽𝟕 𝑽𝟏
𝑽𝟏 𝑽𝟕 𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 𝑽𝟖 𝑽𝟗 𝑽𝟐 𝑽𝟑 𝑽𝟒
𝑽𝟎 𝑽𝟓 𝑽𝟔 𝑽 𝟏𝟎 𝑽 𝟏𝟎 𝑽 𝟓 𝑽 𝟔
(a) (b) (c)
Level of Node
The level of any node is the length of its path from the root.
Ordered Tree
In a directed tree an ordering of the nodes at each level is prescribed then such a tree is called ordered tree.
The diagrams (b) and (c) represents same directed tree but different ordered tree.
Forest
If we delete the root and its edges connecting the nodes at level 1, we obtain a set of disjoint tree. A set of
disjoint tree is a forest.
𝑽𝟓 𝑽𝟔 𝑽 𝟏𝟎
𝑽𝟎
V0
𝑽𝟏 𝑽𝟕 V1 V7
V2 V9
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 V3
V5 V10
𝑽𝟓 𝑽𝟔 𝑽 𝟏𝟎 V6
V4
V8
𝑽𝟓 𝑽𝟔 𝑽 𝟏𝟎 V7
V8
V9
V10
(V0 (V1 (V2 (V5) (V6) ) (V3) (V4) ) (V7 (V8) (V9 (V10) ) ) )
Nesting of Parenthesis
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 16
Level Format
𝑽𝟎 1 V0
2 V1
𝑽𝟏 𝑽𝟕 3 V2
4 V5
4 V6
𝑽𝟐 𝑽𝟑 𝑽𝟒 𝑽𝟖 𝑽𝟗 3 V3
3 V4
𝑽𝟓 𝑽𝟔 𝑽 𝟏𝟎 2 V7
3 V8
3 V9
4 V10
Binary Tree
If in a directed tree the out degree of every node is less than or equal to 2 then tree is called binary tree.
0 1 0 1 0 1
00 01 11 00 01 11 11 00 01 10
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree Part-1) 20
Convert any tree to Binary Tree
Every Tree can be Uniquely represented by binary tree
Let’s have an example to convert given tree into binary tree
a
a
a
b
b f b f
c f
g
d
c d g j k c d g j k
h j
e
e h i e h i i k
a g
a
b c
h i
b g
d e f
j k l
d c h
a g
e j i
b c h i
f k l
d e f j k l
Thank
You