CPDS Unit 5
CPDS Unit 5
We read the linear data structures like an array, linked list, stack and queue in
which all the elements are arranged in a sequential manner. The different data
structures are used for different kinds of data.
A tree is also one of the data structures that represent hierarchical data.
Suppose we want to show the employees and their positions in the hierarchical
form then it can be represented as shown below:
Tree Terminology-
1. 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.
Example-
2. Edge-
Example-
3. 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.
Example-
Here,
• Node A is the parent of nodes B and C
• Node B is the parent of nodes D, E and F
• Node C is the parent of nodes G and H
• Node E is the parent of nodes I and J
• Node G is the parent of node K
4. Child-
Example-
Here,
• Nodes B and C are the children of node A
• Nodes D, E and F are the children of node B
• Nodes G and H are the children of node C
• Nodes I and J are the children of node E
• Node K is the child of node G
5. Siblings-
Example-
Here,
• Nodes B and C are siblings
• Nodes D, E and F are siblings
• Nodes G and H are siblings
• Nodes I and J are siblings
6. Degree-
Example-
Here,
• Degree of node A = 2
• Degree of node B = 3
• Degree of node C = 2
• Degree of node D = 0
• Degree of node E = 2
• Degree of node F = 0
• Degree of node G = 1
• Degree of node H = 0
• Degree of node I = 0
• Degree of node J = 0
• Degree of node K = 0
7. Internal Node-
• The node which has at least one child is called as an internal node.
• Internal nodes are also called as non-terminal nodes.
• Every non-leaf node is an internal node.
Example-
8. 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.
Example-
9. Level-
Example-
10. Height-
• 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
Example-
Here,
• Height of node A = 3
• Height of node B = 2
• Height of node C = 2
• Height of node D = 0
• Height of node E = 1
• Height of node F = 0
• Height of node G = 1
• Height of node H = 0
• Height of node I = 0
• Height of node J = 0
• Height of node K = 0
11. Depth-
• Total number of edges from root node to a particular node is called as depth
of that node.
• Depth of a tree is the total number of edges from root node to a leaf node in
the longest path.
• Depth of the root node = 0
• The terms “level” and “depth” are used interchangeably.
Example-
Here,
• Depth of node A = 0
• Depth of node B = 1
• Depth of node C = 1
• Depth of node D = 2
• Depth of node E = 2
• Depth of node F = 2
• Depth of node G = 2
• Depth of node H = 2
• Depth of node I = 3
• Depth of node J = 3
• Depth of node K = 3
12. Subtree-
Example-
Binary tree
Binary tree is a special tree data structure in which each node can have at most
2 children.
Thus, in a binary tree,Each node has either 0 child or 1 child or 2 children.
A rooted binary tree is a binary tree that satisfies the following 2 properties-
• It has a root node.
• Each node has at most 2 children.
Example-
2. Full / Strictly Binary Tree-
• A binary tree in which every node has either 0 or 2 children is called as a Full
binary tree.
• Full binary tree is also called as Strictly binary tree.
Example-
Here,
• First binary tree is not a full binary tree.
• This is because node C has only 1 child.
A complete binary tree is a binary tree that satisfies the following 2 properties-
• Every internal node has exactly 2 children.
• All the leaf nodes are at the same level.
Example-
Here,
• First binary tree is not a complete binary tree.
• This is because all the leaf nodes are not at the same level.
Example-
Here,
• First binary tree is not an almost complete binary tree.
• This is because the last level is not filled from left to right.
A skewed binary tree is a binary tree that satisfies the following 2 properties-
• All the nodes except one node has one and only one child.
• The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Example-
Binary Tree Representations
A binary tree data structure is represented using two methods. Those methods
are as follows...
1. Array Representation
2. Linked List Representation
The above example of the binary tree represented using Linked list
representation is shown as follows...
Tree Traversal
Tree Traversal refers to the process of visiting each node in a tree data structure
exactly once.
1. Preorder Traversal-
Algorithm-
Visit the root
1. Traverse the left sub tree i.e. call Preorder (left sub tree)
2. Traverse the right sub tree i.e. call Preorder (right sub tree)
Root → Left → Right
Example-
Consider the following example-
2. Inorder Traversal-
Algorithm-
1. Traverse the left sub tree i.e. call Inorder (left sub tree)
2. Visit the root
3. Traverse the right sub tree i.e. call Inorder (right sub tree)
Left → Root → Right
Example-
Keep a plane mirror horizontally at the bottom of the tree and take the
projection of all the nodes.
Application-
• Inorder traversal is used to get infix expression of an expression tree.
3. Postorder Traversal-
Algorithm-
1. Traverse the left sub tree i.e. call Postorder (left sub tree)
2. Traverse the right sub tree i.e. call Postorder (right sub tree)
3. Visit the root
Left → Right → Root
Example-
Consider the following example-
Applications-
• Postorder traversal is used to get postfix expression of an expression tree.
• Postorder traversal is used to delete the tree.
• This is because it deletes the children first and then it deletes the parent.
• Breadth First Traversal of a tree prints all the nodes of a tree level by level.
• Breadth First Traversal is also called as Level Order Traversal.
Example-
Application-
• Level order traversal is used to print the data in the same order as stored in the
array representation of a complete binary tree.
A Binary search tree is shown in the above figure. As the constraint applied on
the BST, we can see that the root node 30 doesn't contain any value greater than
or equal to 30 in its left sub-tree and it also doesn't contain any value less than
30 in its right sub-tree.
▪ Searching become very efficient in a binary search tree since, we get a hint
at each step, about which sub-tree contains the desired element.
▪ The binary search tree is considered as efficient data structure in compare
to arrays and linked lists. In searching process, it removes half sub-tree at
every step. Searching for an element in a binary search tree takes o(log2n)
time. In worst case, the time it takes to search an element is 0(n).
▪ It also speed up the insertion and deletion operations as compare to that
in array and linked list.
Example: Create the binary search tree using the following data elements.
The process of creating BST by using the given elements, is shown in the image
below.
1. Search
2. Insertion
3. Deletion
Graphs
In this graph,
V={A,B,C,D,E}
E = { AB , AC , BD , CD , DE }
Types of Graphs
1. Null Graph
• A graph whose edge set is empty is called as a null graph.
• In other words, a null graph does not contain any edges in it.
Example-
Here,
• This graph consists only of the vertices and there are no edges in it.
• Since the edge set is empty, therefore it is a null graph.
2. Trivial Graph
• A graph having only one vertex in it is called as a trivial graph.
• It is the smallest possible graph.
Example
Here,
• This graph consists of only one vertex and there are no edges in it.
• Since only one vertex is present, therefore it is a trivial graph.
3. Non-Directed Graph
• A graph in which all the edges are undirected is called as a non-directed graph.
• In other words, edges of an undirected graph do not contain any direction.
Example-
Here,
• This graph consists of four vertices and four undirected edges.
• Since all the edges are undirected, therefore it is a non-directed graph.
4. Directed Graph
• A graph in which all the edges are directed is called as a directed graph.
• In other words, all the edges of a directed graph contain some direction.
• Directed graphs are also called as digraphs.
Example-
Here,
• This graph consists of four vertices and four directed edges.
• Since all the edges are directed, therefore it is a directed graph.
5. Connected Graph
• A graph in which we can visit from any one vertex to any other vertex is called
as a connected graph.
• In connected graph, at least one path exists between every pair of vertices.
Example-
Here,
• In this graph, we can visit from any one vertex to any other vertex.
• There exists at least one path between every pair of vertices.
• Therefore, it is a connected graph.
6. Disconnected Graph
• A graph in which there does not exist any path between at least one pair of
vertices is called as a disconnected graph.
Example-
Here,
• This graph consists of two independent components which are disconnected.
• It is not possible to visit from the vertices of one component to the vertices of
other component.
• Therefore, it is a disconnected graph.
7. Regular Graph
• A graph in which degree of all the vertices is same is called as a regular graph.
• If all the vertices in a graph are of degree ‘k’, then it is called as a “k-regular
graph“.
Examples-
In these graphs,
• All the vertices have degree-2.
• Therefore, they are 2-Regular graphs.
8. Complete Graph
• A graph in which exactly one edge is present between every pair of vertices is
called as a complete graph.
• A complete graph of ‘n’ vertices contains exactly nC2 edges.
• A complete graph of ‘n’ vertices is represented as Kn.
Examples-
In these graphs,
• Each vertex is connected with all the remaining vertices through exactly one
edge.
• Therefore, they are complete graphs.
9. Cycle Graph
• A simple graph of ‘n’ vertices (n>=3) and n edges forming a cycle of length ‘n’
is called as a cycle graph.
• In a cycle graph, all the vertices are of degree 2.
Examples-
In these graphs,
• Each vertex is having degree 2.
• Therefore, they are cycle graphs.
Example-
Here,
• This graph contains two cycles in it.
• Therefore, it is a cyclic graph.
Example-
Here,
• This graph do not contain any cycle in it.
• Therefore, it is an acyclic graph.
Here,
• This graph consists of finite number of vertices and edges.
• Therefore, it is a finite graph.
13. Infinite Graph
• A graph consisting of infinite number of vertices and edges is called as an
infinite graph.
Example-
Here,
• This graph consists of infinite number of vertices and edges.
• Therefore, it is an infinite graph.
14. Bipartite Graph
A bipartite graph is a graph where-
• Vertices can be divided into two sets X and Y.
• The vertices of set X only join with the vertices of set Y.
• None of the vertices belonging to the same set join each other.
Example-
Example-
Here,
• This graph can be drawn in a plane without crossing any edges.
• Therefore, it is a planar graph.
Here,
• This graph consists of three vertices and three edges.
• There are neither self loops nor parallel edges.
• Therefore, it is a simple graph.
Example-
Here,
• This graph consists of three vertices and four edges out of which one edge is a
parallel edge.
• There are no self loops but a parallel edge is present.
• Therefore, it is a multi graph.
18. Pseudo Graph
• A graph having no parallel edges but having self loop(s) in it is called as a
pseudo graph.
Example-
Here,
• This graph consists of three vertices and four edges out of which one edge is a
self loop.
• There are no parallel edges but a self loop is present.
• Therefore, it is a pseudo graph.
Example-
Here,
• This graph is a connected graph.
• The degree of all the vertices is even.
• Therefore, it is an Euler graph.
Example-
Graph Representations
Graph data structure is represented using following representations...
1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List
Adjacency Matrix
• Here, 1 represents that there is an edge from row vertex to column vertex
and 0 represents that there is no edge from row vertex to column vertex.
• That means graph with 4 vertices and 6 edges is represented using a matrix
of size 4X6.
• Here, 0 represents that the row edge is not connected to column vertex, 1
represents that the row edge is connected as the outgoing edge to column
vertex and -1 represents that the row edge is connected as the incoming
edge to column vertex.
Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.