Uploaded by av980852

DS Lab 5

advertisement
PRACTICAL JOURNAL OF Data Structures
BE: II Year
Department of Computer Science Engineering
Name of the Student: Shreyas soni
Branch & section: CS 4
Roll No.
: 0827CS191224
Session
: July-Dec 2020
Department of Computer Science Engineering
AITR, Indore.
ACROPOLIS INSTITUTE OF TECHNOLOGY & RESEARCH, INDORE
Department of Computer Science Engineering
Certificate
This is to certify that the experimental work entered in this journal as
per the BE II year syllabus prescribed by the RGPV was done by Mr.
/ Ms. …Shreyas soni…………………………………..BE II year III
semester CS4 section in the Data Structure Laboratory of this institute
during the academic year 2020- 2021.
Signature of Faculty
Ms. Nisha Rathi
(Asst. Prof. CSE Dept.)
Vision of the Institute
To be an academic leader for the development of human potential so as to meet the global
challenges.
Mission of the Institute
1.
2.
3.
4.
To create an intellectually stimulating learning environment.
To impart value based, innovative, and research oriented education.
To develop positive attitude with communication skills.
To increase employability and entrepreneurship through collaboration with industries and
professional organizations.
Vision of CSE Department
The department strives to produce competent and qualified computer professionals and
researchers to serve the community and profession with moral values and ethics.
Mission of CSE Department
1. To impart professional education emphasizing intellectual ability creation
2. To provide learning ambience for enhancing innovations, research, and values among the
students
3. To collaborate with industries for giving opportunities to students to develop their
employability and entrepreneurship skills
4. To inculcate professional behavior, positive attitude, and communication skills
Program Educational Objectives (PEOs) of Computer Science and Engineering Department
PEO1: Preparation
Graduates will be prepared for successful technical career, higher studies, research and
development and will be able to take up the challenges for innovations, idea generation, and
values.
PEO2: Professionalism
Graduates will work as an individual as well as a team on multi-disciplinary projects with
effective communication skills and positive attitude.
PEO3: Lifelong learning
Graduates will have good communication skill and social concerns to meet the challenges of
national and multi-national companies.
Program Specific Outcomes (PSOs) of CSE Department:
Engineering Graduates will be able to:
PSO1
Make use of modern languages and tools such as HTML5, CSS, MongoDB, IOS, IOT,
Android, Hadoop; Work on newer computing paradigms such as Cloud Computing, Big Data
; Acquire globally accepted certification in latest technologies such as Google Applied CS
with Android, HTML5, MongoDB, Big Data, SQL, Data analytics.
PSO2
Apply standard Software Engineering practices and strategies in software project
development. Learn making use of open source libraries in software development.
PSO3
Emphasize on solution oriented approach by problem identification, analysis and design of
computer based system for solving the problems of different complexities.
Program Outcomes
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
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.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of,
and need for sustainable development.
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.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
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.
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 Objective:
The subject/course objectives support the program objectives and outcomes. This subject will
help students to learn and understand the concept of
303.CEO1
Basics of Data Structures Arrays and Linked list, its representation, application and
implementation, time and space complexity,
303.CEO2
Linear Data Structures such as Stacks and Queues-types, applications and
implementation,
303.CEO3
Trees and its varied types such as BINARY, BST AVL Trees, B-trees etc.
303.CEO4
Graphs -their representation, applications and Implementation,
303.CEO5
Sorting, Searching and hashing techniques
Course Outcome:
Upon completion of this subject / course the student will be able to:
Bloom’s levels Course Outcomes
Unit
C303.1
Apply
Analyze
Understand
Specify, represent, classify, implement,
analyze and develop applications using linear
Data Structures like arrays and linked list using
linked list, stacks and queues and their variants
I, II
C303.2
Apply Analyze
Understand
Specify, represent, classify, implement,
analyze and develop applications using non linear
Data Structures like trees and Graphs
III,IV
C303.3
Apply Analyze
understand
Understand, classify, implement various types of
sorting and searching methods; understand and
analyze the application of various data structures
in operating system and DBMS.
V (Presentations
on case study)
C303.4
Create
Evaluate
Analyze Apply
Given the algorithmic problem statement,
Judge the efficient data structures , justify the
reason for choosing it and apply them to provide
efficient algorithmic solutions/approaches to
existing/ new problems
CodeChef
Hackerrank
C303.5
Analyze
Analyze the efficiency of programs and usage of
data structure based on space and time
complexity.
Tutorials on time
complexities of
algorithms
GENERAL INSTRUCTIONS FOR LABORATORY CLASSES
DO’S







Without Prior permission do not enter into the Laboratory.
While entering into the LAB students should wear their ID cards.
The Students should come with proper uniform.
Students should sign in the LOGIN REGISTER before entering into the laboratory.
Students should come with observation and record note book to the laboratory.
Students should maintain silence inside the laboratory.
After completing the laboratory exercise, make sure to shutdown the system properly
DONT’S





Students bringing the bags inside the laboratory..
Students using the computers in an improper way.
Students scribbling on the desk and mishandling the chairs.
Students using mobile phones inside the laboratory.
Students making noise inside the laboratory.
SYLLABUS
I
Review of C programming language. Introduction to Data Structure: Concepts of Data
and Information, Classification of Data structures, Abstract Data Types,
Implementation aspects: Memory representation. Data structures operations and its cost
estimation. Introduction to linear data structures- Arrays, Linked List: Representation
of linked list in memory, different implementation of linked list. Circular linked list,
doubly linked list, etc. Application of linked list: polynomial manipulation using linked
list, etc.
II
Stacks: Stacks as ADT, Different implementation of stack, multiple stacks.
Application of Stack: Conversion of infix to postfix notation using stack,
evaluation of postfix expression, Recursion. Queues: Queues as ADT, Different
implementation of queue, Circular queue, Concept of Dqueue and Priority Queue,
Queue simulation, Application of queues.
III Tree: Definitions - Height, depth, order, degree etc. Binary Search Tree Operations, Traversal, Search. AVL Tree, Heap, Applications and comparison of
various types of tree; Introduction to forest, multi-way Tree, B tree, B+ tree, B* tree
and red-black tree.
IV
Graphs: Introduction, Classification of graph: Directed and Undirected graphs, etc,
Representation, Graph Traversal: Depth First Search (DFS), Breadth First Search
(BFS), Graph algorithm: Minimum Spanning Tree (MST)- Kruskal, Prim’s algorithms.
Dijkstra’s shortest path algorithm; Comparison between different graph algorithms.
Application of graphs.
V
Sorting: Introduction, Sort methods like: Bubble Sort, Quick sort. Selection sort, Heap
sort, Insertion sort, Shell sort, Merge sort and Radix sort; comparison of various sorting
techniques. Searching: Basic Search Techniques: Sequential search, Binary search,
Comparison of search methods. Hashing & Indexing. Case Study: Application of
various data structures in operating system, DBMS etc.
HARDWARE REQUIREMENTS:
Processors - 2.0 GHz or Higher
RAM - 256 MB or Higher
Hard Disk - 20 GB or Higher
SOFTWARE REQUIREMENTS
Operating System: WINDOWS XP, Windows 7/8
Compiler: Dev-Cpp 5.11 TDM-GCC 4.9.2
PREREQUISITE:Basics of Data Structure (DS-I CS 113 ) , Computer Programming & utilization
Teaching and Examination Scheme
Sub
ject
Co
de
CS30
3
Sub
ject
Na
me
DS
E
n
d
S
e
m
.
Maximum Marks Allotted
Theory
Practical
M
Quiz,
E
L
Assign
id
n
a
ment/
S
Assig
d
b
Quiz/
e
nment
S
w
Term
m
e
or
paper
.
m
k
T
.
es
t
Hours/W
eek
To L
T P
tal
M
ar
ks
70
20
150
10
30
10
10
3
1
2
Cre
dits
6
RATIONALE:
Data structure is a subject of primary importance in Information and Communication Technology.
Organizing or structuring data is important for implementation of efficient algorithms and program
development. Efficient problem solving needs the application of appropriate data structure during
program development.
Data structures play a central role in modern computer science. In addition, data structures are
essential building blocks in obtaining efficient algorithms. Understanding of advanced data
structures is essential and this facilitates the understanding of the language. The practice and
assimilation of data structure techniques is essential for programming. The knowledge of C++
language and data structures will be reinforced by practical exercises during the course of study.
The course will help students to develop the capability of selecting a particular data structure and
augmenting it for generating new applications.
Submission 5
Date of Submission: 18 November 2020
th
S.
No.
Name of Experiment
1
Write program for implementation of Heap Sort
2.
Write program for implementation of insertion and traverse operation of Binary search
tree
Name of Student
Shreyas soni
Program 1
Q1. Write program for implementation of Heap Sort.
Algorithm
Step 1 - Construct a Binary Tree with given list of Elements.
Step 2 - Transform the Binary Tree into Min Heap.
Step 3 - Delete the root element from Min Heap using Heapify method.
Step 4 - Put the deleted element into the Sorted list.
Step 5 - Repeat the same until Min Heap becomes empty.
Step 6 - Display the sorted list.
Program
#include <iostream>
using namespace std;
void MaxHeap(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;
while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void HeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
MaxHeap(a, 1, i - 1);
}
}
void BuildHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MaxHeap(a, i, n);
}
int main()
{
int n, i;
cout<<"Enter the number of element to be sorted: ";
cin>>n;
n++;
int arr[n];
for(i = 1; i < n; i++)
{
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
BuildHeap(arr, n-1);
HeapSort(arr, n-1);
cout<<"Sorted Data ";
for (i = 1; i < n; i++)
cout<<" "<<arr[i];
return 0;
}
Output
Enter the number of element to be sorted: 5
Enter element 1: 8
Enter element 2: 55
Enter element 3: 49
Enter element 4: 10
Enter element 5: 2
Sorted Data 2 8 10 49 55
Time complexity
Best Case
Worst case
Type of input
Time complexity
Type of input
Heap sort
O(1)
Heap sort
Name-Shreyas soni
Time complexity
O(1)
Program 2
Q2.Write program for implementation of insertion and traverse operation of Binary search tree
Algorithm
Algorithm for creating a node
Create a newNode with given value and set its left and right to NULL.
Algorithm for insertion
Step 1 - Create a newNode with given value and set its left and right to NULL.
Step 2 - Check whether tree is Empty.
Step 3 - If the tree is Empty, then set root to newNode.
Step 4 - If the tree is Not Empty, then check whether the value of newNode is smaller or larger than
the
node (here it is root node).
Step 5 - If newNode is smaller than or equal to the node then move to its left child. If newNode
is larger than the node then move to its right child.
Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to NULL).
Step 7 - After reaching the leaf node, insert the newNode as left child if the newNode is smaller or
equal to
that leaf node or else insert it as right child.
Algorithm for inorder traversal
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
Algorithm for preorder traversal
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Algorithm for postorder traversal
Step 1- Recursively traverse left subtree.
Step 2- Recursively traverse right subtree.
Step 3- Visit root node.
Program
#include<iostream>
using namespace std;
struct BSTree
{
BSTree* left;
int info;
BSTree* right;
}
*Newn,*root=NULL;
void getnode();
void rootnode();
void Insert();
void TraverseInorder(BSTree*);
void TraversePreorder(BSTree*);
void TraversePostorder(BSTree*);
int main()
{
int ch1;
char ch2;
do{
cout<<"Enter your choice \n1.Insert \n2.TraverseInorder \n3.TraversePreorder
\n4.TraversePostorder \n5.Exit"<<endl;
cin>>ch1;
switch(ch1)
{
case 1:
{
if(root==NULL)
{
rootnode();
}
else
{
getnode();
Insert();
}
break;
}
case 2:
{
cout<<"Inorder Traversal: ";
TraverseInorder(root);
break;
}
case 3:
{
cout<<"Preorder Traversal: ";
TraversePreorder(root);
break;
}
case 4:
{
cout<<"Postorder Traversal: ";
TraversePostorder(root);
break;
}
case 5:
{
exit(0);
}
default:
{
cout<<"Invalid Choice"<<endl;
}
}
cout<<"Do you want to continue(press y)"<<endl;
cin>>ch2;
}while(ch2='y');
}
void getnode()
{
Newn= new BSTree;
cout<<"Enter the number"<<endl;
cin>>Newn->info;
Newn->left=NULL;
Newn->right=NULL;
}
void rootnode()
{
getnode();
root=Newn;
}
void Insert()
{
BSTree *ptr=root,*ptr1=NULL;
while(ptr!=NULL)
{
if(Newn->info>ptr->info)
{
ptr1=ptr;
ptr=ptr->right;
}
else
{
ptr1=ptr;
ptr=ptr->left;
}
}
if(ptr1->info>Newn->info)
{
ptr1->left=Newn;
}
else
{
ptr1->right=Newn;
}
}
void TraverseInorder(BSTree *ptr)
{
if(ptr==NULL)
{
return;
}
TraverseInorder(ptr->left);
cout<<ptr->info<<" ";
TraverseInorder(ptr->right);
}
void TraversePreorder(BSTree *ptr)
{
if(ptr==NULL)
{
return;
}
cout<<ptr->info<<" ";
TraversePreorder(ptr->left);
TraversePreorder(ptr->right);
}
void TraversePostorder(BSTree *ptr)
{
if(ptr==NULL)
{
return;
}
TraversePostorder(ptr->left);
TraversePostorder(ptr->right);
cout<<ptr->info<<" ";
}
Output
Enter your choice
1.Insert
2.TraverseInorder
3.TraversePreorder
4.TraversePostorder
5.Exit
1
Enter the number
44
Do you want to continue(press y)
y
Enter your choice
1.Insert
2.TraverseInorder
3.TraversePreorder
4.TraversePostorder
5.Exit
1
Enter the number
77
Do you want to continue(press y)
y
Enter your choice
1.Insert
2.TraverseInorder
3.TraversePreorder
4.TraversePostorder
5.Exit
1
Enter the number
86
Do you want to continue(press y)
y
Enter your choice
1.Insert
2.TraverseInorder
3.TraversePreorder
4.TraversePostorder
5.Exit
1
Enter the number
67
Do you want to continue(press y)
y
Enter your choice
1.Insert
2.TraverseInorder
3.TraversePreorder
4.TraversePostorder
5.Exit
2
Inorder Traversal: 44 67 77 86 Do you want to continue(press y)
y
Enter your choice
1.Insert
2.TraverseInorder
3.TraversePreorder
4.TraversePostorder
5.Exit
3
Preorder Traversal: 44 77 67 86 Do you want to continue(press y)
y
Enter your choice
1.Insert
2.TraverseInorder
3.TraversePreorder
4.TraversePostorder
5.Exit
4
Postorder Traversal: 67 86 77 44 Do you want to continue(press y)
Time complexity
Best Case
Type of input
Time complexity
Binary search tree O(log n)
Name-Shreyas soni
Worst case
Type of input
Binary search
tree
Time complexity
O(n)
Download