IT202-DS-Unit 4 - Non-Linear-Data-Structures
IT202-DS-Unit 4 - Non-Linear-Data-Structures
1
Unit 4 - Syllabus
Non-Linear Data Structures
Binary tree – array and linked implementation of binary trees –
application of trees – tree traversals
Graphs – representation – breadth first search – depth first search –
spanning trees – application of graphs
Tree Structures
Parent
• If N is any node in T that has left successor S1
and right successor S2, then N is called the
parent of S1 and S2.
• Correspondingly, S1 and S2 are called the left
child and the right child of N.
• Every node other than the root node has a
parent.
III. Binary Trees - Terminology
Level number
• The left and the right child of the root node have a
level number 1.
Height of a tree
• It is the total number of nodes on the path from the
root node to the deepest node in the tree.
• A tree with only a root node has a height of 1.
III. Binary Trees - Terminology
21 – 1 = 1 • A binary tree of height h has at least h nodes
and at most 2h – 1 nodes.
• This is because every level will have at least one
Tree Height 22 – 1 = 3
node and can have at most 2 nodes.
log2n • So, if every level has two nodes then a tree with
3
2 –1=7 height h will have at the most 2h – 1 nodes as at
level 0, there is only one element called the
24 – 1 = 15 root.
• The height of a binary tree with n nodes is at
least log2(n+1) and at most n
Complete Binary Tree Left Skewed Binary Tree Right Skewed Binary Tree
Complete Binary Trees
Linked representation
of binary trees
struct node {
struct node *left;
int data;
struct node *right;
};
TRAVERSING A BINARY TREE
◦ Traversing a binary tree is the process of visiting each node in the tree exactly once in a
systematic way.
◦ Unlike linear data structures in which the elements are traversed sequentially, tree is a
nonlinear data structure in which the elements can be traversed in many different ways.
◦ There are different algorithms for tree traversals.
◦ These algorithms differ in the order in which the nodes are visited.
◦ Pre-order Traversal –> D-L-R
◦ In-order Traversal -> L-D-R
◦ Post-order Traversal -> L-R-D
Pre-order Traversal
To traverse a non-empty binary tree in pre-order, the following
operations are performed recursively at each node.
◦ 1. Visiting the root node,
◦ 2. Traversing the left sub-tree
◦ 3. Traversing the right sub-tree
Preorder(TREE)
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE-> DATA
Step 3: PREORDER(TREE->LEFT)
Step 4: PREORDER(TREE-> RIGHT)
[END OF LOOP]
Step 5: END
Preorder traversal (D-L-R)
A, B, D, G, H, L, E, C, F, I, J, and K
A, B, D, C, E, F, G, H, and I
In-order Traversal (L-D-R)
◦ 1. Traversing the left sub-tree,
◦ 2. Visiting the root node, and finally
◦ 3. Traversing the right sub-tree.
G, D, H, L, B, E, A, C, I, F, K, and J
B, D, A, E, H, G, I, F, and C
Post-order Traversal (L-R-D)
1. Traversing the left sub-tree,
2. Traversing the right sub-tree, and finally
3. Visiting the root node.
G, L, H, D, E, B, I, K, J, F, C, and A
D, B, H, I, G, F, E, C, and A
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
a
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
cfg
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
a
In : d b e In : f c g
Pre: b d e Pre: c f g
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
a
In : d b e In : f c g
Pre: b d e Pre: c f g
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
a
b c
In : d b e In : f c g
Pre: b d e d e f g Pre: c f g
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
a
b c
d e f g
(A) e d b g f c a
(B) d e f g b c a
(C) d e b f g c a
(D) d e b f g a c
Quiz
The inorder and preorder traversal of a binary tree are d b e a f c g
and a b d e c f g, respectively. The postorder traversal of the binary
tree is _________.
In order : d b e a f c g - LDR
Preorder: a b d e c f g - DLR
a
b c
d e f g
(A) e d b g f c a
(B) d e f g b c a
(C) d e b f g c a
(D) d e b f g a c
Applications of Trees
Trees are used to store simple as well as complex data
B-trees are prominently used to store data on external storage.
Trees are an important data structure used for compiler construction.
Trees are also used in database design.
Trees are used in file system directories.
Trees are also widely used for information storage and retrieval in
symbol tables
Nim Game tree
Optimal Decision Tree – 8 coins problem
Expression Trees
Binary trees are widely used to store algebraic expressions
Undirected Directed
graph graph
Graph Terminology
Adjacent nodes or neighbors
For every edge, e = (u,v) that connects nodes u and v, the nodes
u and v are the end-points and are said to be the adjacent nodes or
neighbours.
Degree of a node
Degree of a node u, deg(u), is the total number of edges containing
the node u. If deg(u) = 0, it means that u does not belong to any
edge and such a node is known as an isolated node.
Graph Terminology
Path
A path P written as P = {v0, v1, v2, ..., vn), of length n from a node u to v
is defined as a sequence of (n+1) nodes. Here, u = v0, v = vn and vi–1 is
adjacent to vi for i = 1, 2, 3, ..., n.
Closed path
A path P is known as a closed path if the edge has the same end-points.
That is, if
v0 = vn.
Graph Terminology
Simple path
A path P is known as a simple path if all the nodes in the path are distinct with an
exception that v0 may be equal to vn. If v0 = vn, then the path is called a closed
simple path.
Cycle
A path in which the first and the last vertices are same. A simple cycle has no
repeated edges or vertices (except the first and last vertices).
Graph Terminology
Connected graph
A graph is said to be connected if for any two vertices (u, v) in V, there is a path
from u to v. That is to say that there are no isolated nodes in a connected graph. A
connected graph that does not have any cycle is called a tree. Therefore, a tree is
treated as a special graph
Complete graph
A graph G is said to be complete, if all its nodes are fully connected. That is,
there is a path from one node to every other node in the graph. A complete graph
has n(n–1)/2 edges, where n is the number of nodes in G.
Graph Terminology
Labelled graph or weighted graph
A graph is said to be labelled if every edge in the graph is assigned some data. In a
weighted graph, the edges of the graph are assigned some weight or length. The
weight of an edge denoted by w(e) is a positive value which indicates the cost of
traversing the edge.
Graph Terminology
Multi-graph
A graph with multiple edges and/or loops is called a multi-graph.
Graph Terminology
Out-degree of a node
The out-degree of a node u, written as outdeg(u), is the number of edges that
originate at u.
In-degree of a node
The in-degree of a node u, written as indeg(u), is the number of edges that
terminate at u.
Degree of a node
The degree of a node, written as deg(u), is equal to the sum of in-degree and
out-degree of that node. Therefore, deg(u) = indeg(u) + outdeg(u).
Graph Terminology
Strongly connected directed graph
A digraph is said to be strongly connected if and only if there exists a path between
every pair of nodes in G. That is, if there is a path from node u to v, then there
must be a path from node v to u.
Graph Terminology
Transitive Closure of a Directed Graph
For a directed graph G = (V,E), where V is the set of vertices and E is
the set of edges, the transitive closure of G is a graph G* = (V,E*). In
G*, for every vertex pair v, w in V there is an edge (v, w) in E* if and
only if there is a valid path from v to w in G.
Graph Terminology
Transitive Closure of a Directed Graph - Applications
b c
d e f g
h i j k
DFT Vs BFT
◦ Dijkstra’s algorithm is used when the graph has positive weights only. It is faster than the
other single source shortest path algorithms.
Dijkstra’s Algorithm – Single Source Shortest Path Distance
Algorithm
45 Input Parameters
1 2 3 4 5
𝑚𝑖𝑛
𝑑𝑖𝑠𝑡𝑘 𝑤 = 𝑢𝑛𝑣𝑖𝑠𝑖𝑡𝑒𝑑 𝑢 {𝑑𝑖𝑠𝑡
𝑘−1
𝑤 , 𝑑𝑖𝑠𝑡𝑘−1 𝑢 + 𝑐𝑜𝑠𝑡 𝑢, 𝑤 } S T F F F F
dist 0 50 45 10 ∞
Dijkstra’s Algorithm – Single Source Shortest Path Distance Algorithm
45
Algorithm ShortestPaths(v, cost, n, dist)
{
for i = 1 to n do 50 10
1 2 3
{ S[i] = false; dist[i] = cost[v,i]; }
S[v] = true; dist[v] = 0; 10 35
20 15 20
for num = 2 to n-1 do 30
{ Choose u among the vertices not true in S such that dist[u] is minimum
S[u] = true; 4 15 5
for(each w adjacent to u with S[w] = false) do
if(dist[w] > (dist[u] + cost[u,w])) then 0 50 45 10 ∞
dist[w] = dist[u] + cost[u,w] ∞ 0 10 15 ∞
} Cost[5, 5]= ∞ ∞ 0 ∞ 30
} 20 ∞ ∞ 0 15
∞ 20 35 15 0
num=2 1 2 3 4 5 num=2 1 2 3 4 5
S T F F T F S T F F F F
dist 0 50 45 10 25 dist 0 50 45 10 ∞
Dijkstra’s Algorithm – Single Source Shortest Path Distance Algorithm
45
Algorithm ShortestPaths(v, cost, n, dist)
{
for i = 1 to n do 50 10
1 2 3
{ S[i] = false; dist[i] = cost[v,i]; }
S[v] = true; dist[v] = 0; 10 35
20 15
for num = 2 to n-1 do 20 30
{ Choose u among the vertices not true in S such that dist[u] is minimum
S[u] = true; 4 15 5
for(each w adjacent to u with S[w] = false) do
if(dist[w] > (dist[u] + cost[u,w])) then 0 50 45 10 ∞
dist[w] = dist[u] + cost[u,w] ∞ 0 10 15 20
} Cost[5, 5]= ∞ ∞ 0 ∞ 30
} 20 ∞ ∞ 0 15
∞ 20 35 15 0
num=3 1 2 3 4 5 num=3 1 2 3 4 5
S T F F T T S T F F T F
dist 0 45 45 10 25 dist 0 50 45 10 25
Dijkstra’s Algorithm – Single Source Shortest Path Distance Algorithm
45
Algorithm ShortestPaths(v, cost, n, dist)
{
for i = 1 to n do 50 10
1 2 3
{ S[i] = false; dist[i] = cost[v,i]; }
S[v] = true; dist[v] = 0; 10 35
20 15
for num = 2 to n-1 do 20 30
{ Choose u among the vertices not true in S such that dist[u] is minimum
S[u] = true; 4 15 5
for(each w adjacent to u with S[w] = false) do
if(dist[w] > (dist[u] + cost[u,w])) then 0 50 45 10 ∞
dist[w] = dist[u] + cost[u,w] ∞ 0 10 15 20
} Cost[5, 5]= ∞ ∞ 0 ∞ 30
} 20 ∞ ∞ 0 15
∞ 20 35 15 0
num=4 1 2 3 4 5 num=4 1 2 3 4 5
1 2 3 4 5
S T T F T T S T F F T T
dist 0 45 45 10 25
dist 0 45 45 10 25 dist 0 45 45 10 25
Output
Applications of Graph
1. In circuit networks where points of connection are drawn as vertices and component wires
become the edges of the graph.
2. In transport networks where stations are drawn as vertices and routes become the edges of the
graph.
3. In maps that draw cities/states/regions as vertices and adjacency relations as edges.
4. In program flow analysis where procedures or modules are treated as vertices and calls to these
procedures are drawn as edges of the graph.
5. A graph of a particular concept can be used for finding shortest paths, project planning, etc.
6. In flowcharts or control-flow graphs, the statements and conditions in a program are represented
as nodes and the flow of control is represented by the edges.
7. In state transition diagrams, the nodes are used to represent states and the edges represent legal
moves from one state to the other.
8. Graphs are also used to draw activity network diagrams. These diagrams are extensively used as a
project management tool to represent the interdependent relationships between groups, steps,
and tasks that have a significant impact on the project.
Spanning Tree
A spanning tree is a sub-graph of an undirected connected graph, which
includes all the vertices of the graph with a minimum possible number of
edges. If a vertex is missed, then it is not a spanning tree.
◦ (a) Insert 21, 39, 45, 54, and 63 into the tree
◦ (b) Delete values 23, 56, 2, and 45 from the tree
Practicing Exercise
Consider five cities: (1) New Delhi, (2) Mumbai, (3) Chennai, (4)
Bangalore, and (5) Kolkata, and a list of flights that connect these
cities as shown in the following table. Use the given information to
construct a graph.
Practicing Exercise
Given the following adjacency matrix, draw the weighted graph.