Professional Documents
Culture Documents
Tree and Graph
Tree and Graph
Course Code:22317
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
Graph
Adjacency Matrix
Adjacency List
Definition of Tree
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 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 TerminologiesIn-degree of tree
Tree Terminologies Out-degree of tree
Tree TerminologiesPath
Types of 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.
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
70
60
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 20
20
70
60
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 90
20
70
60 90
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 10
20
70
10
60 90
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 40
20
70
10 40
60 90
Binary Search Tree
sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Insert 100
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
Binary
search tree
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
^
^
2
+ 5
=
-
* * *
*
a
2 b 5 c 4 d 6 e
Graph Terminologies
Graph Terminologies
Graph Terminologies
Graph Terminologies
Graph Terminologies
In degree (V3)=2
Graph Terminologies
Graph Terminologies
Graph Terminologies
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
C A,E
D B
E D
Adjacency matrix for Directed graph
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:
Graph
From the following graph, complete the answers:
A B
C D
C D
C D
C D
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.
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