Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 68

Program: Computer Engineering

Course Name: Data structure using C

Course Code:22317

Course Outcome: Implement program to create and


traverse tree to solve problem.

Unit Outcome: Draw binary search tree.


Traverse the tree.
Construct expression tree of given data.
Represent the given graph using adjacency
matrix and adjacency list
What we will learn today
• Draw binary search tree.
• Traverse the tree.
• Construct expression tree of given data.
• Represent the given graph using adjacency
matrix and adjacency list

Key Takeaway
• Draw binary search tree.
• Traverse the tree.
• Construct expression tree of given data.
• Represent the given graph using
adjacency matrix and adjacency list
Learning Objectives/Key Learning

• Binary search tree


– Tree Traversal
• Expression tree

Graph
Adjacency Matrix
Adjacency List
Definition of Tree

Tree is a collection of elements called nodes. Tree is a non-linear data structure


which organizes data in a hierarchical structure
Tree Terminologies Root

The first node from where the tree originates is called as a root node. In any tree, there
must be only one root node. We can never have multiple root nodes in a tree data
structure.
Tree Terminologies Edge

Edge: The connecting link between any two nodes is called as an edge. In a tree
  with n number of nodes, there are exactly (n-1) number of edges.
 
Tree Terminologies Parent

Parent: The node which has a branch from it to any other node is called as a
parent node.
In other words, the node which has one or more children is called as a parent
node. In a tree, a parent node can have any number of child nodes.
 
Tree Terminologies Child

Child: The node which is a descendant of some node is called as a child node.
All the nodes except root node are child nodes.
 

Child of C
Tree Terminologies Siblings

Siblings: Nodes which belong to the same parent are called as siblings. In other
  words, nodes with the same parent are sibling nodes.
 
 
Tree Terminologies Leaf node

Leaf node: The node which does not have any child is called as a leaf node.
Leaf nodes are also called as external nodes or terminal nodes.
 
 
Tree Terminologies Degree of node

Degree of node: Degree of a node is the total number of children of that node.
 
 

Degree of node A, E: 2 Degree of node B, C: 3


 
Degree of node D, F, H, I: 0 Degree of node G: 1
 
Tree Terminologies Degree of a tree

Degree of a tree: Degree of a tree is the highest degree of a node among all the
nodes in the tree.
 

 
Degree of a tree:3
Tree Terminologies Level of tree

Level of tree: In a tree, each step from top to bottom is called as level of a tree.
The level count starts with 0 and increments by 1 at each level or step.
 

 
Level of tree 3
Tree Terminologies Height of tree

Height of tree: Total number of edges that lies on the longest path from any
leaf node to a particular node is called as height of that node.

 
Height of a tree is the height of root node.
Height of all leaf nodes = 0
 
Tree TerminologiesIn-degree of tree

In-degree: number of edges coming towards nodes is in- degree.


In-degree of A – 0 In-degree of B – 1

 
Tree Terminologies Out-degree of tree

Out-degree: number of edges going out from nodes is out-degree.


Out-degree of A- 2 Out-degree of B-3

 
Tree TerminologiesPath

Path: It is a sequence of consecutive edges from the source node to the


destination node. For ex: A-B-E-J.

 
Types of trees

A general tree is a tree where each node


may have zero or more children .
• General trees are used to model
applications such as file systems.
• In general tree, root has indegree 0
and maximum outdegree n.
• Height of a general tree is the
length of longest path from root to
the leaf of tree.
 
Types of trees

Binary tree is one of the most widely


used non-linear data structures in the
field of computer science.
• It is a restricted form of a general tree.
• The restriction that it applies to a
general tree is that its nodes can have a
maximum degree of 2.
• That means, the nodes of a binary tree
can have zero, one or two child nodes
 
but not more than that.
Types of Binary trees

Binary tree

 
Binary Search Tree
Binary Search Tree is a special kind of binary tree in which nodes are arranged in a specific order.

In a binary search tree (BST), each node contains-


•Only smaller values in its left sub tree
•Only larger values in its right sub tree
Binary Search Tree
Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
 
When elements are given in a sequence,
•Always consider the first element as the root node.
•Consider the given elements and insert them in the BST one by one.
 
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 50 

50

Insert 70-
 
•As 70 > 50, so insert 70 to the right of 50.

70
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 60 

•As 60 > 50, so insert 60 to the right of 50.


•As 60 < 70, so insert 60 to the left of 70.

70

60
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 20 

•As 20 < 50, so insert 20 to the left of 50.

20
70

60
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 90 

•As 90 > 50, so insert 90 to the right of 50.


•As 90 > 70, so insert 90 to the right of 70.

20
70

60 90
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 10 

•As 10 < 50, so insert 10 to the left of 50.


•As 10 < 20, so insert 10 to the left of 20.

20
70

10
60 90
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 40 

•As 40 < 50, so insert 40 to the left of 50.


•As 40 > 20, so insert 40 to the right of 20.

20
70

10 40

60 90
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 100 

•As 100 > 50, so insert 100 to the right of 50.


•As 100 > 70, so insert 100 to the right of 70.
•As 100 > 90, so insert 100 to the right of 90.

20
70

10 40

60 90

100
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100

20
70

10 40

60 90

100
Construct a binary search tree for following elements:
30, 100, 90, 15, 2, 25, 36, 72, 78, 10

30

15
100

25
2
90
10

36

72

78
Binary Search Tree

A binary search tree is generated by inserting in order of the following


integers-
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
 
The number of nodes in the left subtree and right subtree of the root
respectively is _____.
1.(4, 7)
2.(7, 4)
3.(8, 3)
4.(3, 8)
Binary Search Tree Traversal

Traversal is the process of visiting the various elements of a data


structure.
Binary tree traversal can be performed using three methods:
1. Preorder
2. Inorder
3. Postorder
Binary Search Tree Traversal

Binary
search tree

In order Preorder Post order


Traversal Traversal Traversal
Binary Search Tree Traversal

Preorder:
The preorder traversal method performs the following
operations:
(a) Process the root node (N).
(b) Traverse the left subtree of N (L).
(c) Traverse the right subtree of N (R).
Binary Search Tree Traversal

Inorder:
The inorder traversal method performs the following operations:
(a) Traverse the left subtree of N (L).
(b) Process the root node (N).
(c) Traverse the right subtree of N (R).
 
Binary Search Tree Traversal

Postorder:
The postorder traversal method performs the following operations:
 
(a) Traverse the left subtree of N (L).
(b) Traverse the right subtree of N (R).
(c) Process the root node (N).
Binary Search Tree Traversal

Inorder Traversal(LNR):

Q,E,F,R,D,H,B,A,I,J,K,C,L,P

Preorder Traversal(NLR):

A,B,D,E,Q,F,R,H,C,I,J,K,L,P

Postorder Traversal(LRN):

Q,R,F,E,H,D,B,K,J,I,P,L,C,A
Binary Search Tree Traversal

Inorder Traversal(LNR):

1,10,15,20,22,25,32,36,43,48,50,56,58,60,75

Preorder Traversal(NLR):

36,25,20,10,1,15,22,32,48,43,56,50,60,58,75

Postorder Traversal(LRN):
1,15,10,22,20,32,25,43,50,58,75,60,56,48,36
 
Expression Tree

• Expression tree is nothing but a binary tree containing mathematical


expression.
• The internal nodes of the tree are used to store operators while the leaf or
terminal nodes are used to store operands.
• Various compilers and parsers use expression trees for evaluating
arithmetic and logical expressions.
Expression Tree

• Expression tree is nothing but a binary tree containing mathematical


expression.
• The internal nodes of the tree are used to store operators while the leaf or
terminal nodes are used to store operands.
• Various compilers and parsers use expression trees for evaluating
arithmetic and logical expressions.
Draw Expression Tree (a – 2b + 5c)2 * (4d = 6e)5

^
^
2
+ 5

=
-

* * *
*

a
2 b 5 c 4 d 6 e
Graph Terminologies

Graph: A graph is set of vertices(V) and set of edges(E). the set of V is


finite, non-empty set of vertices. The set E is a set of pair of vertices
representing edges.

 
Graph Terminologies

Node (vertices): number of elements connected through edges in direct or


indirect way is called as node or vertices.
Arcs (edges): the link which connects two or more vertices together is
known as arcs or edges.

 
Graph Terminologies

Indirect graph: a graph containing unordered pair of vertices is known as


Indirect graph.

 
Graph Terminologies

Direct graph: a graph containing ordered pair of vertices is known as direct


graph.

 
Graph Terminologies

In-degree: In a directed graph, indegree of a vertex is the number of edges


ending at the vertex.

In degree (V3)=2
Graph Terminologies

Out-degree: In a directed graph, outdegree of a vertex is the number of


edges beginning at the vertex.

Out degree (V3)=1


Graph Terminologies

Adjacent: if e (u,v) represents an edges between u and v vertices then both


u and v are called adjacent to each other. u is adjacent to v and v is
  adjacent to u.
 
Successor: if e (u,v) represents a directed edge from u to v then v is
  successor node of u.
 
Predecessor: if e (u,v) represents a directed edge from u to v then u is
predecessor node of v.

 
Graph Terminologies

Adjacent: if e (u,v) represents an edges between u and v vertices then both


u and v are called adjacent to each other. u is adjacent to v and v is
  adjacent to u.
 
Successor: if e (u,v) represents a directed edge from u to v then v is
  successor node of u.
 
Predecessor: if e (u,v) represents a directed edge from u to v then u is
predecessor node of v.

 
Graph Terminologies

Path: A path is a sequence of vertices each adjacent to the next.

For example, in the graph shown in Fig. the path between the vertices v1
and v5 is v1–v2–v3–v5.

 
Adjacency list for Directed graph

Nodes Adjacent Nodes


A B
B D,E

  C A,E
D B
E D
Adjacency matrix for Directed graph

 
Adjacency list for Undirected graph

A B Nodes Adjacent Nodes


E A B,C
B A,D,E
C D C A,D
D B,C,E
E B,D
Adjacency list for Undirected graph

A B
E

C D
Adjacency Matrix for Undirected graph

A B
E

C D

A B C D E
A 0 1 1 0 0
B 1 0 0 1 1
C 1 0 0 1 0
D 0 1 1 0 1
E 0 1 0 1 0
Graph
From the following graph, complete the answers:

(i) Indegree of node 21 :3


(ii) Adjacent node of 19:1,21
(iii) Path of 31:1-7-21-31 ,1-12-19-21-31
(iv) Successor of node 67: 19

 
Graph
From the following graph, complete the answers:
A B

C D

i) Find all the simple paths from A to D


A-D
A-C-D
A-B-D
A-C-B-D
Graph
From the following graph, complete the answers:
A B

C D

ii. Find all the simple paths from B to D.


B-D
Graph
From the following graph, complete the answers:
A B

C D

iii. Find Indeg (B) =2


iv. Outdeg (B)=1
Graph
From the following graph, complete the answers:
A B

C D

iv. Find the adjacency matrix A of the graph G.


A B C D
A 0 1 1 1
B 0 0 0 1
C 0 1 0 1
D 0 0 1 0
Graph
From the following graph, complete the answers:
1 2
3
4 5

i. All simple path from 1 to 5


1-2-5
1-3-2-5
Graph
From the following graph, complete the answers:
1 2
3
4 5

In-degree of 4=1

Out-degree of 4=0
Graph
From the following graph, complete the answers:
1 2
3
4 5
Give adjacency list representation of the given graph.

Nodes Adjacent Nodes


1 2,3
2 5
3 2,4
4 -
5 3
Graph
From the following graph, complete the answers:
1 2
3
4 5

Find the adjacency matrix A of the graph G.

1 2 3 4 5
1 0 1 1 0 0
2 0 0 0 0 1
3 0 1 0 1 0
4 0 0 0 0 0
5 0 0 3 0 0
Difference Between Tree and Graph
Parameter Tree Graph
Path Only one between More than one
two vertices. path is allowed.
Root node It has exactly one Graph doesn't have
root node. a root node.
Loops No loops are Graph can have
permitted. loops.
Traversal Pre-order, In-order Breadth-first search
techniques and Post-order. and depth-first
search.
Number of edges n-1 (where n is the Not defined
number of nodes)
Model type Hierarchical Network
Thank You

Supriya Kadam
Department of Computer Engineering (NBA Accredited)
Vidyalankar Polytechnic
Vidyalankar College Marg, Wadala(E), Mumbai 400 037
E-mail: supriya.kadam@vpt.edu.in
67
68

You might also like