DSA Manual
DSA Manual
Department of Artificial
Intelligence and Data Science
Engineering
2. Problem Analysis: Identity, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
5. Modern tool usage: Create, select and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
Course Objectives:
● To understand practical implementation and usage of non linear data structures for solving
problems of different domains.
● To strengthen the ability to identify and apply the suitable data structure for the given real
world problems.
● To analyze advanced data structures including hash table, dictionary, trees, graphs, sorting
algorithms and file organization.
Course Outcomes:
After successfully completing the course students will be able to
1. Understand the ADT/libraries, hash tables and dictionary to design algorithms for a specific
problem.
2. Choose the most appropriate data structures and apply algorithms for graphical solutions of the
problems.
3. Apply and analyze non linear data structures to solve real world complex problems. 4. Apply
and analyze algorithm design techniques for indexing, sorting, multi-way searching, file
organization and compression.
5. Analyze the efficiency of the most appropriate data structure for creating efficient solutions for
engineering design situations.
SUBJECT PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 P
O
12
CO5 1 1 2
2
C. CO-PSO mapping
Course Program Specific Outcomes
Outcome
12
210252.1 1 ---
210252.2 1 ---
210252.3 1 ---
210252.4 1 ---
210252.5 1 ---
210252.6 1 ---
INDEX
Sr. Date of CAS Signatur
No. Experiment PageDate of completio eof
conduction n Faculty
13 Mini project
List Of assignments
Group Sr. Title of Assignment
No
A 1. Consider the telephone book database of N clients. Make use of a hash table
implementation to quickly look up a client's telephone number. Make use of
two collision handling techniques and compare them using number of
comparisons required to find a set of telephone numbers
3. Beginning with an empty binary search tree, Construct a binary search tree by
inserting the values in the order given. After constructing a binary tree - i. Insert
new node, ii. Find number of nodes in longest path from root, iii. Minimum data
value found in the tree, iv. Change a tree so that the roles of the left and right
pointers are swapped at every node, v. Search a value
4 Construct an expression tree from the given prefix expression eg. +--a*bc/def
B and traverse it using post order traversal (non recursive) and then delete the
entire tree.
5. A Dictionary stores keywords and its meanings. Provide facility for adding new
keyword, deleting keywords, updating values of entry. Provide facility to display
whole data sorted in ascending /Descending order. Also find how many
maximum comparisons may require for finding any keyword. Use Binary Search
Tree for implementation.
6 Represent a given graph using adjacency matrix/list to perform DFS and using
adjacency list to perform BFS. Use the map of the area around the college as
the graph. Identify the prominent landmarks as nodes and perform DFS and BFS
on that.
C
7. You have a business with several offices; you want to lease phone lines to
connect them up with each other; and the phone company charges different
amounts of money to connect different pairs of cities. You want a set of lines
that connects all your offices with a minimum total cost. Solve the problem by
suggesting appropriate data structures.
10. Consider a scenario for hospital to cater services to different kinds of patients
E as Serious (top priority), b) non-serious (medium priority), c) General Check-up
(Least priority). Implement the priority queue to cater services to the patients.
11. Department maintains student information. The file contains roll number,
name, division and address. Allow users to add, delete information about
students. Display information of a particular employee. If the record of the
F student does not exist an appropriate message is displayed. If it is, then the
system displays the student details. Use a sequential file to maintain the data.
12. Implementation of a direct access file -Insertion and deletion of a record from a
direct access file
Experiment No. 1
⮚ Title: Hash Table Implementation
Problem Statement: Consider telephone book database of N clients. Make use of a hash table
implementation to quickly look up client‘s telephone number. Make use of two collision handling
techniques and compare them using number of comparisons required to find a set of telephone
numbers.
Aim: To make the use of collision handling technique (linear probing with replacement)
Outcome: At end of this experiment, student will be able to illustrate the use of collision handling
technique in hash table implementation.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
Open Source Python, Programming tool like Google Colab, Jupyter Notebook, Pycharm, Spyder,
G++/GCC.
⮚ Theory:
Hashing:
Hashing is the process of transforming data and mapping it to a range of values which can be
efficiently looked up.
Open Addressing:
● Open addressing is a method for handling collisions. In Open Addressing, all elements are stored in
the hash table itself. So at any point, the size of the table must be greater than or equal to the total
number of keys.
Operations on open addressing:
1. Insert(k): Keep probing until an empty slot is found. Once an empty slot is found, insert k. 2.
Search(k): Keep probing until slot’s key doesn’t become equal to k or an empty slot is reached.
a) Linear Probing: In linear probing, we linearly probe for next slot. For example, the typical gap
between two probes is 1 as seen in the example below.
Let hash(x) be the slot index computed using a hash function and S be the table size
If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
..................................................
..................................................
Example: Let us consider a simple hash function as “key mod 7” and a sequence of keys as 50, 700, 76, 85,
92, 73, 101.
Conclusion:-
Thus all set operations have been studied and implemented client book record using hashing
Experiment No. 2
⮚ Title: To create an ADT that implements the "set" concept.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
Open Source Python, Programming tool like Google Colab, Jupyter Notebook, Pycharm, Spyder.
⮚ Theory:
Set in Python:
The set() function takes in an iterable and yields a list of objects which will be inserted into the set.
The {} syntax places the objects themselves into the set.
Set Size and Membership:
The len() function returns the number of elements in a set, and the ins and not in operators can be used to test
for membership:
>>> x = {'foo', 'bar', 'baz'}
>>> len(x)
3
Update() functions:
add_set = set((1, 2, 3, 4))
>>> add_set
{1, 2, 3, 4}
>>> add_set.update((1,))
>>> add_set
{1, 2, 3, 4}
Union Operations:
In Python, set union can be performed with the | operator:
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1 | x2
{'baz', 'quux', 'qux', 'bar', 'foo'}
Intersection Operations:
x1.intersection(x2) and x1 & x2 return the set of elements common to both x1 and x2:
>>> x1 = {'foo', 'bar', 'baz'}
>>> x2 = {'baz', 'qux', 'quux'}
>>> x1.intersection(x2)
{'baz'}
>>> x1 & x2
{'baz'}
Difference Operations:
The difference between the two sets in Python is equal to the difference between the number of elements in two
sets. The function difference() returns a set that is the difference between two sets.
>>> a.difference(b, c)
{1, 2, 3}
>>> a - b - c
{1, 2, 3}
Is subset operations:
In set theory, a set x1 is considered a subset of another set x2 if every element of x1 is in
x2. x1.issubset(x2) and x1 <= x2 return True if x1 is a subset of x2:
>>> x1 = {'foo', 'bar', 'baz'}
>>> x1.issubset({'foo', 'bar', 'baz', 'qux', 'quux'})
True
Input:-
Set elements
Conclusion:- The types of functions in python have been studied and the operations on students records
have been implemented using user defined functions.
Experiment No. 3
Aim: To make the use of Binary Search Tree and perform the different operations on it.
⮚ Outcome: At the end of this experiment, students will be able to illustrate the use of Binary Search
Tree implementation.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
Binary Search Tree:
A binary search tree follows some order to arrange the elements. In a Binary search tree, the value
of left node must be smaller than the parent node, and the value of right node must be greater
than the parent node. This rule is applied recursively to the left and right sub trees of the root.
Binary Search Tree Operations:
1) Insert Operation: Inserting a new node into the existing tree.
Algorithm:
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
2) Searching a Node in BST: If the value is below the root, we can say for sure that the value is not in the
right sub tree; we need to only search in the left sub tree and if the value is above the root, we can say for
sure that the value is not in the left sub tree; we need to only search in the right sub tree.
Algorithm:
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
current = current->left;
return(current->key);
}
4) Mirror() Operation:
The Mirror() operation finds the mirror of the tree that will interchange all left and right sub trees in a
linked binary tree.
Algorithm:
(1) Call Mirror for left->subtree i.e., Mirror(left->subtree) (2)
Call Mirror for right->subtree i.e., Mirror(right-subtree) (3)
Swap left and right subtrees.
temp = left->subtree
left->subtree = right->subtree
right->subtree = temp
Input:-
Output:-
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6)
inorder 7) preorder 8) postorder
1
Enter the data : 34
Do you want to insert more value :y
Enter the data : 12
Do you want to insert more value :y
Enter the data : 67
Do you want to insert more value :y
Enter the data : 2
Do you want to insert more value :n
The Total no.of nodes are:4
Experiment No. 4
⮚ Title: Implementation of Expression Tree from the given Prefix Expression.
Problem Statement: Construct an expression tree from the given prefix expression eg. +--a*bc/def and
traverse it using post order traversal (non recursive) and then delete the entire tree.
Aim: To make the use of Binary Tree and stack Data structure to construct the Expression Tree from
the given prefix expression.
⮚ Outcome: At the end of this experiment, students will be able to illustrate the use of Expression tree
implementation.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
Expression Binary Tree:
The expression tree is a binary tree in which each internal node corresponds to the operator and each
leaf node corresponds to the operand so for example expression tree for 3 + ((5+9)*2) would be:
1. The main objective of using the expression trees is to make complex expressions and can be
easily evaluated using these expression trees.
2. It is also used to find out the associativity of each operator in the expression.
3. It is also used to solve the postfix, prefix, and infix expression evaluation.
Input:-
Prefix Expression
Conclusion:-
Frequently Asked Questions:
1. What is binary tree traversal?
2. Explain the expression tree with suitable examples.
3. Construct the expression tree from the given expression: *+ab-cd”
4. Explain the delete operation on Binary Tree.
Experiment No. 5
⮚ Title: Implementation of General Tree
Problem Statement: A book consists of chapters, chapters consist of sections and sections consist
of subsections. Construct a tree and print the nodes. Find the time and space requirements of
your method.
Outcome: At end of this experiment, student will be able to illustrate the use of General Tree
Data Structure implementation.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
General Tree:
A tree T is a finite set of one or more nodes such that there is one designated node RR, called the
root of T. If the set (T−{R})(T−{R}) is not empty, these nodes are partitioned into n>0n>0
disjoint sets T0T0, T1T1, …, Tn−1Tn−1, each of which is a tree, and whose roots
R1,R2,...,RnR1,R2,...,Rn, respectively, are children of RR. The subsets Ti(0≤i<n)Ti(0≤i<n) are
said to be subtrees of T.
● allocating a uniform-sized array of node pointers and limiting the number of children a
node is allowed to have; -
● allocating a dynamically-sized array of node pointers, custom-fit to the number of
children the node currently has (and re-sizing manually as needed);
● Using a linked list of node pointers;
● Using a vector of node pointers, growing as needed.
Input:-
Book name
No of chapters in Book
Chapter name
Section names
Program code:
# include <iostream>
# include <cstdlib>
# include <string.h>
using namespace std;
struct node
{char label[10];
int ch_count;
/*
* Class Declaration
*/
class GT
{
public:
void create_tree();
void display(node * r1);
GT()
{
root = NULL;
}
};
void GT::create_tree()
{
int tbooks,tchapters,i,j,k;
root = new node;
cout<<"Enter name of book";
cin>>root->label;
cout<<"Enter no. of chapters in book";
cin>>tchapters;
root->ch_count = tchapters;
for(i=0;i<tchapters;i++)
{
root->child[i] = new node;
cout<<"Enter Chapter name\n";
cin>>root->child[i]->label;
cout<<"Enter no. of sections in Chapter: "<<root->child[i]->label;
cin>>root->child[i]->ch_count;
for(j=0;j<root->child[i]->ch_count;j++)
{
root->child[i]->child[j] = new node;
cout<<"Enter Section "<<j+1<<"name\n";
cin>>root->child[i]->child[j]->label;
//cout<<"Enter no. of subsections in "<<r1->child[i]->child[j]->label;
//cin>>r1->child[i]->ch_count;
}
cout<<"\n-----Book Hierarchy---";
cout<<"\n Book title : "<<r1->label;
tchapters = r1->ch_count;
for(i=0;i<tchapters;i++)
{
cout<<"\n Chapter "<<i+1;
cout<<" "<<r1->child[i]->label;
cout<<"\n Sections";
for(j=0;j<r1->child[i]->ch_count;j++)
{
//cin>>r1->child[i]->child[j]->label;
cout<<"\n "<<r1->child[i]->child[j]->label;
}
}
}
}
int main()
{
int choice;
GT gt;
while (1)
{
cout<<"-----------------"<<endl;
cout<<"Book Tree Creation\n1.Create\n2.Display\n3.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:gt.create_tree();
case 2: gt.display(root);
break;
case 3:exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}
Conclusion:- Thus implemented general tree and its operations.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
Graph: A Graph is a non-linear data structure consisting of nodes and edges. The nodes are
sometimes also referred to as vertices and the edges are lines or arcs that connect any two
nodes in the graph. More formally a Graph can be defined as,
“A Graph consists of a finite set of vertices (or nodes) and set of Edges which connect a pair of
nodes.”
the set of edges E = {01, 12, 23, 34, 04, 14, 13}.
Representations of a graph:
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let
the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex
j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used
to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j
with weight w.
The adjacency matrix for the above example graph is:
Adjacency List:
An array of lists is used. The size of the array is equal to the number of vertices. Let the array
i
be an array[]. An entry array[i] represents the list of vertices adjacent to the th vertex. This
representation can also be used to represent a weighted graph. The weights of edges can be
represented as lists of pairs. Following is the adjacency list representation of the above
graph.
Graph Traversal:
DFS Algorithm:
● Step 1: Insert the root node or starting node of a tree or a graph in the stack.
● Step 2: Pop the top item from the stack and add it to the visited list.
● Step 3: Find all the adjacent nodes of the node marked visited and add the ones that are not
yet visited, to the stack.
● Step 4: Repeat steps 2 and 3 until the stack is empty.
Implementation of DFS:
Algorithm DFS ( ) // This algorithm is used to traverse graphs in DFS order.
1. {
2. for(i=1 to n) do
3. visit[i]=0;
4. Read(Starting vertex in v1);
5. push(v1);
6. while(top!=-1) // until stack I s not empty
7. {
8. v1=pop();
9. if(visit[v1]==0) // v1 is unvisited
10. {
11. Write(v1);
12. visit[v1]=1;
13. for(v2=1 to n) // find neighbors
14. if(g[v1][v2]==1) // neighbor exist
15. push(v2);
16. }
17. }
18. }
BFS Algorithm:
1. The algorithm for breadth-first search traversal is:
2. Select the root of the graph.
3. Insert the root vertex into the queue.
4. Pop a vertex from the queue, mark it as visited, and output its value. 5. Visit
its unvisited neighbor vertex, push them to the queue, and mark visited. 6.
Repeat step 3 until the queue is empty.
7. When the queue is empty, end the program.
Input:-
Thus DFS and BFS algorithms have been studied and implemented for the graph Frequently
Asked Questions:
1. What is the difference between graph and tree data structure?
2. Write a short note on different representations of a graph..
3. Explain the BFS traversal of a graph.
4. Show BFS and DFS for the following graph with the starting vertex as 0.
Experiment No. 7
⮚ Title: Implementation of a Minimum Spanning Tree using Prim’s Algorithm
⮚ Problem Statement: You have a business with several offices; you want to lease phone lines to connect them up with each
other; and the phone company charges different amounts of money to connect different pairs of cities. You want a set of
lines that connects all your offices with a minimum total cost. Solve the problem by suggesting appropriate data
structures.
⮚ Aim: To make the use of weighted Graph data Structure to represent several offices
⮚ Outcome: At the end of this experiment, students will be able to illustrate the use of Prim's
algorithm to find the minimum spanning tree implementation.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
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 minimum spanning tree is a spanning tree in which the sum of the weight of the edges is as
minimum as possible.
The
minimum spanning tree from the above spanning trees is:
The minimum spanning tree from a graph is found using the following algorithms: 1. Prim's Algorithm
Prim’s Algorithm also use Greedy approach to find the minimum spanning tree. In Prim’s
Algorithm we grow the spanning tree from a starting position. Unlike an edge in Kruskal's, we
add vertex to the growing spanning tree in Prim's.
Algorithm Steps:
● Maintain two disjoint sets of vertices. One containing vertices that are in the growing
spanning tree and other that are not in the growing spanning tree.
● Select the cheapest vertex that is connected to the growing spanning tree and is not in the
growing spanning tree and add it into the growing spanning tree. This can be done using
Priority Queues. Insert the vertices, that are connected to growing spanning trees, into the
Priority Queue.
● Check for cycles. To do that, mark the nodes which have been already selected and insert
only those nodes in the Priority Queue that are not marked.
Solution:
2. Kruskal's Algorithm
Steps for finding MST using Kruskal’s algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far.
If the cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree
Example on Kruskal’s Algorithm:
Cost of minimum spanning tree:
=sum of all the edges
=1+5+3+2
=11
Input:-
1->2
1->3
3->4
Min. Cost: 24
Experiment No. 8
⮚ Aim: To make the use of optimal binary search tree data structure to calculate search
probability pi for n keys.
⮚ Outcome: At end of this experiment, student will be able to illustrate the use of optimal binary
search tree implementation.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
In the above tree, all the nodes on the left subtree are smaller than the value of the root node, and
all the nodes on the right subtree are larger than the value of the root node. The maximum time
required to search a node is equal to the minimum height of the tree, equal to log(n).
Now we will see how many binary search trees can be made from the given number of keys.
Now to calculate the cost, we will consider only the jth value.
The cost of c[0,1] is 4 (The key is 10, and the cost corresponding to key 10 is 4).
The cost of c[1,2] is 2 (The key is 20, and the cost corresponding to key 20 is 2).
The cost of c[2,3] is 6 (The key is 30, and the cost corresponding to key 30 is 6)
The cost of c[3,4] is 3 (The key is 40, and the cost corresponding to key 40 is 3)
o When i=0 and j=2, then keys 10 and 20. There are two possible trees that can be made out
from these two keys shown below:
In
the second binary tree, cost would be: 4*2 + 2*1 = 10
o When i=2 and j=4, we will consider the keys at 3 and 4, i.e., 30 and 40. There are two
possible trees that can be made out from these two keys shown as below:
o When i=0, j=3 then we will consider three keys, i.e., 10, 20, and 30.
The following are the trees that can be made if 10 is considered as a root
node.
In the above tree, 10 is the root node, 20 is the right child of node 10, and 30 is the right child of
node 20.
In the above tree, 10 is the root node, 30 is the right child of node 10, and 20 is the left child of
node 20.
In the above tree, 20 is the root node, 30 is the right child of node 20, and 10 is the left child of
node 20.
The following are the trees that can be created if 30 is considered as the root node.
In the above tree, 30 is the root node, 20 is the left child of node 30, and 10 is the left child of
node 20.
Therefore, the minimum cost is 20 which is the 3rd root. So, c[0,3] is equal to
20. o When i=1 and j=4 then we will consider the keys 20, 30, 40
= min{12, 5, 10} + 11
In this case, we will consider four keys, i.e., 10, 20, 30 and 40. The frequencies of 10, 20, 30 and
40 are 4, 2, 6 and 3 respectively.
w[0, 4] = 4 + 2 + 6 + 3 = 15
In the above cases, we have observed that 26 is the minimum cost; therefore, c[0,4] is equal to
26.
ALGORITHMS IN PSEUDOCODE
The following function builds an optimal
binary search tree
FUNCTION CONSTRUCT(R, i, j) begin
*build a new internal node N labeled (i, j)
k ← R (i, j) if i = k then
Input:-
Output:-
Experiment No. 9
⮚ Aim: To make the use of Height Balanced tree data structure to build the dictionary which
stores keywords and its meaning.
⮚ Outcome: At end of this experiment, student will be able to illustrate the use of height balance
tree implementation.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
Height Balance Tree:
A balanced binary tree is also known as height balanced tree. It is defined as binary tree in when
the difference between the height of the left subtree and right subtree is not more than m, where m
is usually equal to 1. The height of a tree is the number of edges on the longest path between the
root node and the leaf node.
Height-balancing requirement:
A node in a tree is height-balanced if the heights of its subtrees differ by no more than 1.
(That is, if the subtrees have heights h1 and h2, then |h1 − h2| ≤ 1.)
A tree is height-balanced if all of its nodes are height-balanced. (An empty tree is height-balanced by
definition.)
Operations on an AVL tree:]
Insertion operation on AVL Tree:
Insertion in AVL tree is performed in the same way as it is performed in a binary search tree. The new
node is added into AVL tree as the leaf node. However, it may lead to violation in the AVL tree
property and therefore the tree may need balancing.
S
Rotation Description
N
1 LL The new node is inserted to the left sub-tree of left sub-tree of critical node.
Rotation
2 RR The new node is inserted to the right sub-tree of the right sub-tree of the
Rotation critical node.
3 LR The new node is inserted to the right sub-tree of the left sub-tree of the
Rotation critical node.
4 RL The new node is inserted to the left sub-tree of the right sub-tree of the
Rotation critical node.
Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right
subtree, then we perform a single left rotation −
Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree. The tree
then needs a right rotation.
Left-
right Rotation
Right-left Rotation:
Input:-
Keywords and Its meaning.
Output:-
Experiment No. 10
⮚ Aim: Implement priority queue as ADT using single linked list for servicing patients in a
hospital with priorities as i) Serious (top priority) ii) medium illness (medium priority) iii)
General (Least priority).
Objective:
● To understand the concept of priority Queue.
● How data structures Queue is represented as an ADT.
Outcome: At the end of this experiment, students will be able to illustrate the use of Priority Queue as ADT.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
Queue Data Structure:
A Queue is a linear structure which follows a particular order in which the operations are performed.
The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a
resource where the consumer that came first is served first. The difference between stacks and queues
is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item
the least recently added.
Priority Queue
Priority Queue is an abstract data type, which is similar to a queue, however, in the priority queue,
every element has some priority. The priority of the elements in a priority queue determines the order
in which elements are removed from the priority queue. Therefore all the elements are either arranged
in an ascending or descending order.
ALGORITHM: Define structure for Queue (Priority, Patient Info, Next Pointer).
Empty Queue:
Step 2: Return 1
Step 3: Return 0
Insert Function:
Insert Patient in Queue with respect to the Priority.
Patient is a pointer variable of type Queue, which hold the information about new patient.
i) Patient->Next = Front;
ii) Front=Patient;
Step 3: Else
A) Temp=Front;
B) Do Steps a while Temp! = NULL and Patient->Priority <= Temp->Next->Priority
a) Temp=Temp->Next;
Step 4: Stop
Display (Front)
Else
Else
c) Temp = Temp->Next;
Step 3: Stop
Input:-
a) Serious (top priority), b) non-serious (medium priority), c) General Checkup (Least priority).
Output:-
Enter Your Choice:-
0 for Exit
0 for Exit
0 for Exit
3
deleted Element =c
Its Priority = 10
0 for Exit
0 for Exit
Experiment No. 11
Title: Implementation of sequential file organization concept using cpp .
Problem Statement: Department maintains student information. The file contains roll number,
name, division and address. Allow users to add, delete information about students. Display
information of a particular employee. If the record of the student does not exist an
appropriate message is displayed. If it is, then the system displays the student details. Use a
sequential file to maintain the data.
⮚ Aim: Implement file organization concept to maintain the student information record.
Objective:
● To understand the concept of sequential file organization.
● to understand the file handling concepts.
Outcome: At the end of this experiment, students will be able to illustrate the use of sequential files to maintain
the data.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
File – A file is a collection of related information that is recorded on secondary storage such as
magnetic disks, magnetic tables and optical disks.
File Organization:
File Organization refers to the logical relationships among various records that constitute the file,
particularly with respect to the means of identification and access to any specific record. In
simple terms, Storing the files in certain order is called file Organization. File Structure refers to
the format of the label and data blocks and of any logical control record.
Types of file organization:
● Sequential File Organization
● Heap File Organization
● Hash File Organization
● B+ Tree File Organization
● Clustered File Organization
2) Sorted File Method –In this method, As the name itself suggests, whenever a new record has
to be inserted, it is always inserted in a sorted (ascending or descending) manner.
Sorting of records may be based on any primary key or any other key.
Outcome: To implement the concept and basics of Sorting, internal and external memory
handling.
⮚ Hardware Requirement:
PC/Laptop
⮚ Software Requirement:
G++/GCC.
⮚ Theory:
External Sorting:
External Sorting is sorting the lists that are so large that the whole list cannot be contained in the
internal memory of a computer.
Assume that the list(or file) to be sorted resides on a disk. The term block refers to the unit of
data that is read form or written to a disk at one time. A block generally consists of several
records. For a disk, there are three factors contributing to read/write time:
(ii) Latency time: time until the right sector of the track is under the read/write head.
(iii) Transmission time: time to transmit the block of data to/from the disk.
Example of External Sorting: 2-way Merge Sort, k-way Merge Sort, Polyphase Merge sort etc.
External Sorting with Disks:
The most popular method for sorting on external storage devices is merge
First Phase(Run Generation Phase): Segments of the input file are sorted using a good internal sort
method. These sorted segments, known as runs, are written out onto external storage as they are
generated.
Second Phase(Merge Phase):The runs generated in phase one are merged together following the
merge tree pattern , until only one run is left.
Let M=maximum number of records that can be sorted & sorted in internal memory at one
time. Algorithm:
Repeat
Repeat
Merge runs again as often as needed until only one large run: The sorted list.
(* initialize buffers *)
loop from i = 1 to N
if
end_of_file (file i)
then buffer[i] <- invalid_key else buffer[i] <- first record of
file i;
(* merge *)
repeat
if end_of_file (file s)
close files
The size of a run and number of initial runs (nr) is dictated by the number of file blocks (b) and
the available buffer space (nb)
• Nr = b/nb
• E.g b = 1024 blocks and nb = 5 blocks then 205 initial runs will be needed The degree of
merging (dm) is the number of runs that can be merged together in each pass. • One buffer
block is needed to hold one block from each run being merged • One buffer block is needed
•
Number of passes = logdm(nr)
Analysis :This algorithm requires log(N/M) passes with initial run pass. Therefore, at each pass the
N records are processed and at last we will get a time complexity as O(N log(N/M).
Conclusion: