Data Structure
Data Structure
Data Structure
UIT-RGPV
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Outline
➢Introduction
➢Basic Terminology
➢Types of Data Structures
➢Advantages of Data Structures
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Outline
• Data Structure Classifications
• Operations on Data Structure
II. Reusability: Data structures are reusable, i.e. once we have implemented a
particular data structure, we can use it at any other place. Implementation of data
structures can be compiled into libraries which can be used by different clients.
III. Abstraction: Data structure is specified by the ADT which provides a level of
abstraction. The client program uses the data structure through interface only,
without getting into the implementation details
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 9
Thank You
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Outline
Introduction
Terms used for Array
Array Memory Representation
Need of an Array
Types of Arrays
Basic Operations performed on an Array
Advantages of Arrays
Disadvantages of Arrays
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Outline
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]
Where,
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0
(zero)
N = Number of column of the given matrix
Where,
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0
(zero)
M = Number of row of the given matrix.
• Solution: As you see here the number of rows and columns are
not given in the question. So they are calculated as:
Number of rows say M = (Ur – Lr) + 1 = [10 - (- 15)] +1
= 26
Number of columns say N = (Uc - Lc) + 1 = [40 – 15)] +1
= 26
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 13
Cont…
(i). Row Major Wise Calculation of above equation
• The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15,
Lc = 15, N = 26
• Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)]
= 1500 + 1 * [26 * 30 + 5]
= 1500 + 1 * [780 + 5]
= 1500 + 785
= 2285 [Ans]
• Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J – Lc ) ]
= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)]
= 1500 + 1 * [30 + 26 * 5]
= 1500 + 1 * [160]
= 1660 [Ans]
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Outline
Note: In the above procedure BEG is used for Source Peg, AUX is used for Auxiliary
Peg and END is used for Destination Peg.
Data Structures By Asst. Prof. Praveen
9
Yadav, DoCSE, UIT-RGPV
Solution for, TOWER (N, BEG, AUX, END )
By:
Mr. Praveen Yadav
Assistant Professor
DoCSE, UIT-RGPV
Bhopal
Outline
Introduction
Asymptotic Analysis
Types of Asymptotic Notations
By:
Mr. Praveen Yadav
Assistant Professor
DoCSE, UIT-RGPV
Bhopal
Time Complexity Computation
1. Substitution Method
2. Master Method
Solution:
T(n) = T(n − 1) + 1 ----------------given equation-1
Put n = n - 1
T(n-1) = T(n-2) + 1
now put value of T(n-1) in equation-1
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 4
T(n) = T(n-2) + 1 + 1
T(n) = T(n-2) +2 --------------equation-2
Now put n = n-2 in given equation-1 to get T(n-2)
T(n-2) = T(n-3) + 1
Now put value of T(n-2) in equation-2, we get
T(n) = T(n-3) + 2 + 1
T(n) = T(n-3) + 3 --------------equation-3
Now, We can write general solution in terms of k
T(n) = T(n-k) + k
When k = n-1 we get T(1) = 1
We know that
log2(n) ≤ 1 + log2(n) ≤ 2 log2(n) ∀ n ≥ 2.
Hence
T(n) = Θ(log2n )
h(n) u(n)
1 O ( nr ) , r < 0 O (1)
3 Ω ( nr ) , r > 0 Θ ( h(n) )
Solution:
Answer:
Topic: Recursion
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Outline
Definition of Recursion
Base Condition in Recursion
Types of Recursion (Direct & Indirect)
Types of Direct Recursion
• Example:
Example:
// Code Showing Head Recursion // Driver code
#include <stdio.h> int main()
{
// Recursive function
int x = 3;
void fun(int n) fun(x);
{ return 0;
if (n > 0) { }
// First statement in the function
fun(n - 1);
printf("%d ", n);
}
}
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 9
Indirect Recursion
In this recursion, there may be more than one
function and they are calling one another in a
circular manner.
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Outline
➢Definition
➢Reason to use Sparse Matrix
➢Sparse Matrix Representations
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Linked List
• A Linked List or one-way list, is a linear collection of
data elements, called Nodes, Where the linear order
is given by means of pointers.
• Each node is divided into two parts
1. First Part: It contains the information of the
elements
2. Second Part: It contains the address of the next
node in the list.
Note:
• First Part is also called Data Field or Information Field
• Second part is also called Link field or next-pointer field.
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 2
Single Node of Linked List
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Operations Performed on Linked List
Basic Operations performed on Linked List are:
• Node Creation
• Insertion
• Deletion
• Traversing
• Searching
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Operations Performed on Linked List
• Node Creation
• Insertion
• Deletion
• Traversing
• Searching
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Types of Linked List
• Following are the types of Linked List
1. Singly Linked List
2. Doubly Linked List (Two-Way Linked List)
3. Circular Linked List
4. Circular Doubly Linked List
5. Header Linked List (Grounded Header List and
Circular Header List)
6. Two-Way Header List & Two-Way Circular Header
List
• Link-1 field stores the address of the previous node and Link-2
field stores the address of the next node.
• The Data Item field stores the actual value of that node.
Advantages:
• Doubly linked list can be traversed in both forward and
backward directions.
• To delete a node in singly linked list, the previous node is
required, while in doubly linked list, we can get the
previous node using previous pointer.
• It is very convenient than singly linked list. Doubly linked list
maintains the links for bidirectional traversing.
Disadvantages:
• In doubly linked list, each node requires extra space for
previous pointer.
• All operations such as Insert, Delete, Traverse etc. require
extra previous pointer to be maintained.
Disadvantages:
1.It is not easy to reverse the linked list.
2.If proper care is not taken, then the problem of infinite loop can
occur.
3.If we at a node and go back to the previous node, then we can
not do it in single step. Instead we have to complete the entire
circle by going through the in between nodes and then we will
reach the required node.
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 11
Circular Doubly Linked List
• Circular doubly linked list is a linked data
structure which consists of a set of sequentially
linked records called nodes.
• In doubly circular linked list, the previous link of
the first node points to the last node and the next
link of the last node points to the first node.
• In Circular doubly linked list, each node contains
three fields one data field and two link fields .
• Two link fields are used to represent references
to the previous and the next node in the
sequence of nodes.
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Stack Data Structure
• A stack is a list of elements in which an element
may be inserted or deleted only at one end,
called the TOP of the stack.
• A Stack is a linear Data Structure.
• Stacks are also called Last-In-First-Out (LIFO).
• Elements are removed from a stack in the reverse
order of that in which the were inserted into the
stack.
• Stack is said to be in Overflow state when it is
completely full and is said to be
in Underflow state if it is completely empty.
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 2
Basic Operations Associated with Stack
• Two Basic Operations are:
1. PUSH Operation
2. POP Operation
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Applications of Stack
• Stack Frames (Used in Recursion).
• Reversing a String.
• Parsing (Used in Compiler).
• Calculation of Postfix Expression.
P = 5, 6, 2, +, *, 12, 4, /, -
Solution:
First Add “)” at the end of P (Postfix expression)
P = 5, 6, 2, +, *, 12, 4, /, -, )
12 40, 12 Push 12
4 40, 12, 4 Push 4
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Implementation of Stack using Linked List
• Linked Stack: Linked Stack is a stack that is
implemented using a singly linked list.
• A Node of Linked List:
OR
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Types of Notations for an Mathematical Expression
1. Infix Notation
2. Prefix Notation (Polish Notation)
3. Postfix Notation (Reverse Polish Notation)
Example-2:
Example-3:
By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Types of Queue
• There are four types of Queue:
1. Simple Queue
2. Circular Queue
3. DEQUE (Double Ended Queue)
4. Priority Queue
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
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
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
Note:
In Binary Tree, a node may have at most 2 child nodes
(i.e. Zero or One or Two Child nodes are allowed).
In a Binary Tree Maximum degree of any node is at
most two.
Thus,
With 3 labeled nodes, 30 different labeled binary trees
are possible.
Each unlabeled structure gives rise to 3! = 6 different
labeled structures.
9 Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV, Bhopal
Each unlabeled structure gives rise to 3! = 6
different labeled structures.
We start from A, and following pre-order traversal, we first visit A itself and
then move to its left subtree B. B is also traversed pre-order.
The process goes on until all the nodes are visited.
The output of pre-order traversal of this tree will be:
A→B→D→E→C→F→G
We start from A, and following Post-order traversal, we first visit the left
subtree B. B is also traversed post-order.
The process goes on until all the nodes are visited.
The output of post-order traversal of this tree will be −
D → E → B → F → G → C →A
Write:
1. Inorder Traversal:
2. Preorder Traversal:
3. Postorder Traversal:
Write:
1. Inorder Traversal: 4, 2, 5, 1, 3
2. Preorder Traversal: 1, 2, 4, 5, 3
3. Postorder Traversal: 4, 5, 2, 3, 1
Solution:
In Preorder Traversal first Node is the Root Node
Start scanning from left to right in case of Preorder
Traversal..
Postorder Traversal: 9, 15, 25, 29, 40, 60, 95, 50, 27, 23, 10
Solution:
In Postorder Traversal last Node is the Root Node
Start scanning from right to left if Postorder Traversal is
given.
Preorder Traversal: 29, 15, 9, 10, 25, 23, 27, 40, 60, 50, 95
Note: In BST, Inorder Traversal gives Sorted List:
9, 10, 15, 23, 25, 27, 29, 40, 50, 60, 95
Balance Factor:
Let TL and TR be the left and right subtrees of T and
h(TL) and h(TR) be the heights of subtrees TL and TR
respectively then
Balance Factor = h(TL) - h(TR)
R0 Rotation
R1 Rotation
R-1 Rotation
L0 Rotation
L1 Rotation
L-1 Rotation
logm(n+1) ≤ h ≤ n
Where n is number of keys and m is the order
of tree.
3 Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV, Bhopal
M-Way Search Tree (Example)
logm(n+1) ≤ h ≤ n
Where n is number of keys and m is the order
of tree.
Topic: B-Trees
Where:
P is weighted path length (we want minimize it)
Wi and Li denotes respectively the weight and path length
of an external node Ni.
Solution:
Apply Sequential Search
Compare each array element with ITEM till
element found.
ITEM is found at LOC = 11
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares
the first two elements, and swaps since 5 > 1.
(1 5 4 2 8 ) –> (1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are
already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does
not know if it is completed. The algorithm needs
one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Forth Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) No Change List is sorted.
Note: Bubble sort take (n-1) passes to sort a list of n elements.
Solution:
In this diagram we can see at
same bucket 1 there are two
records which are maintained
by linked list or we can say
by chaining method.
For Example:
Solution:
Step-1: Construct Max Heap for given Array.
Step-2: Assign the root R to some variable ITEM and set A[N+1] =
ITEM.
Step-3: Replace the deleted node R by the last node L of H, so that H
is still a complete binary tree, but not necessarily a heap.
Step-4: (Reheap) Let L sink (go downwards) to its appropriate place
in H so that H is finally a Heap.
Step-5: Repeat step-1 to step-4 till Heap contain only one element.
Topic: Hashing
Topic: Graphs
Undirected graph:
Weighted Graph:
Unweighted Graph:
Non-Simple Graph:
Cyclic Graph:
The tree that we are making or The tree that we are making or
growing always remains connected. growing usually remains disconnected.
Prim’s Algorithm is faster for dense Kruskal’s Algorithm is faster for sparse
graphs. graphs.
The tree that we are making or The tree that we are making or
growing always remains connected. growing usually remains disconnected.
Prim’s Algorithm is faster for dense Kruskal’s Algorithm is faster for sparse
graphs. graphs.
The tree that we are making or The tree that we are making or
growing always remains connected. growing usually remains disconnected.
Prim’s Algorithm is faster for dense Kruskal’s Algorithm is faster for sparse
graphs. graphs.