1022 - Aryan Bhingare - DSAL A1-11
1022 - Aryan Bhingare - DSAL A1-11
Rollno: 1022
Batch: S4 Comp
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
Pseudo Code:
FUNCTION insertData
INPUT n
IF d[dgt].telno equals 0
ELSE
flag=1
IF d[j].telno equals 0
assign j to value
break
END IF
INCREMENT j by 1
j = j MOD 10
END WHILE
END IF
IF value is less than 10
d[value].name=nm
d[value].telno=tno
IF flag equals 1
END IF
ELSE
break
END IF
END FOR
END FUNCTION
FUNCTION displayData
FUNCTION searchData
DISPLAY "Enter telno to be searched: "
INPUT tno
ELSE
break
END IF
INCREMENT j
j = j MOD 10
INCREMENT count
END WHILE
END IF
END IF
END FUNCTION
Code:
#include<iostream>
struct node{
int value;
node* next;
}*HashTable[10];
class hashing{
public:
hashing(){
HashTable[i]=NULL; }
}
int HashFunction(int value){
return (value%10);
temp->next=NULL;
temp->value=x; return
temp;
temp=HashTable[i];
cout<<"a["<<i<<"] : ";
while(temp !=NULL){
cout<<" ->"<<temp->value;
temp=temp->next;
cout<<"\n";
entry = HashTable[hash_val];
cout<<"\nElement found at : "; while (entry !
= NULL)
if (entry->value==value)
entry = entry->next;
if (!flag)
return -1;
}
void deleteElement(int value){ int
hash_val = HashFunction(value);
if (entry == NULL )
return;
entry = entry->next;
entry->next=(entry->next)->next;
}
hash_val = HashFunction(value);
= create_node(value);
temp=HashTable[hash_val];
if (temp == NULL)
HashTable[hash_val] =head;
else{
temp = temp->next;
temp->next =head;
};
int main(){
int ch;
int data,search,del;
hashing h;
do{
cout<<"\nTelephone : \n1.Insert \n2.Display \n3.Search \n4.Delete \n5.Exit"<<endl;
switch(ch){
cin>>data;
h.insertElement(data);
break;
case 2:h.display();
break;
cin>>search;
if (h.searchElement(search) == -1)
continue;
break;
cin>>del;
h.deleteElement(del);
cout<<"Phno. Deleted"<<endl;
break;
while(ch!=5);
return 0;
}
Output:
DSAL Assignment – 2
Name: Aryan Bhingare
Rollno: 1022
Batch: S4 Comp
Problem Statement:
Implement all the functions of a dictionary (ADT) using hashing and handle collisions
a. using chaining with / without replacement.
b. Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys
must be unique
c. Standard Operations: Insert(key, value), Find(key), Delete(key)
Pseudo Code:
Insert Data
FUNCTION insertData
INPUT n
INPUT k,v
dgt = k mod 10
Node *temp=ht[dgt]
Node *pointer=NULL
pointer=temp
temp=temp->next
END WHILE
ht[dgt]=temp
ELSE
pointer->next=temp
END IF
ELSE
END IF
END FOR
END FUNCTION
Search Data
FUNCTION searchData
INPUT k
dgt = k mod 10
Node *temp=ht[dgt]
return FUNCTION
END IF
IF temp->key equals k
END IF
temp=temp->next
END WHILE
IF temp->key equals k
ELSE
END IF
END FUNCTION
Displaying Data
FUNCTION displayData
Node *temp
continue
END IF
temp=temp->next
END WHILE
END IF
END FOR
END FUNCTION
Removing Data
FUNCTION removeData
INPUT k
dgt = k mod 10
Node *temp=ht[dgt]
Node *pointer=ht[dgt]
pointer=pointer->next
ht[dgt]=pointer
END IF
END IF
temp=temp->next
IF temp->key equals k
pointer->next=temp->next
temp->next=NULL
END IF
temp=pointer->next
pointer=pointer->next
END WHILE
ELSE
END IF
END FUNCTION
Code:
#include <iostream>
namespace std;
class HashFunction
long key;
char name[10];
}hash; hash
h[10];
public:
HashFunction();
void insert();
Delete(long);
};
HashFunction::HashFunction()
int i;
for(i=0;i<10;i++)
h[i].key=-1;
strcpy(h[i].name,"NULL");
void HashFunction::Delete(long k)
1)
else
h[index].key=-1;
strcpy(h[index].name,"NULL"); cout<<"\n\tKey
is Deleted";
}
int HashFunction::find(long k)
int i;
for(i=0;i<10;i++)
if(h[i].key==k)
return i;
if(i==10)
return -1;
void HashFunction::display()
int i;
cout<<"\n\t\tKey\t\tName";
for(i=0;i<10;i++)
cout<<"\n\th["<<i<<"]\t"<<h[i].key<<"\t\t"<<h[i].name;
}
void HashFunction::insert()
char ans,n[10],ntemp[10];
v,hi,cnt=0,flag=0,i;
do
if(cnt>=10)
break;
function if(h[hi].key==-1)
h[hi].key=k;
strcpy(h[hi].name,n);
}
else
if(h[hi].key%10!=hi)
temp=h[hi].key;
strcpy(ntemp,h[hi].name);
h[hi].key=k; strcpy(h[hi].name,n);
for(i=hi+1;i<10;i++)
if(h[i].key==-1)
h[i].key=temp;
strcpy(h[i].name,ntemp);
flag=1;
break;
if(h[i].key==-1)
h[i].key=temp;
strcpy(h[i].name,ntemp); break; }
else
for(i=hi+1;i<10;i++)
if(h[i].key==-1)
h[i].key=k;
strcpy(h[i].name,n);
flag=1; break;
}
}
if(h[i].key==-1)
h[i].key=k;
strcpy(h[i].name,n); break; }
flag=0;
cnt++;
cout<<"\n\t..... Do You Want to Insert More Key: y/n";
cin>>ans;
}while(ans=='y'||ans=='Y');
int main()
long k; int
ch,index; char
ans;
HashFunction obj;
do
case 1: obj.insert();
break;
case 2: obj.display();
break;
index=obj.find(k);
if(index==-1)
break;
cin>>k;
obj.Delete(k);
break; case 5:
break;
cin>>ans;
}while(ans=='y'||ans=='Y');
Output:
DSAL Assignment – 3
Name: Aryan Bhingare
Rollno: 1022
Batch: S4 Comp
Problem Statement:
Beginning with an empty binary search tree, Construct binary search tree by inserting the values in
the given order.
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
Pseudo Code:
Creating New Node
createNode FUNCTION with val as parameter
CREATE new node
assign value of node
assign left and right pointers to NULL
RETURN node
Inserting Node
insertNode FUNCTION with node ipdata as parameters
IF node EQUALS NULL
node = CALL createNode(ipdata)
ELSE IF node->data is greater than ipdata
node->left=CALL createNode(node->left,ipdata)
ELSE
node->right=CALL createNode(node->right,ipdata)
RETURN node
Inputting Node
input FUNCTION
DISPLAY "Enter number of nodes to be added:
" input n
Displaying Tree
display FUNCTION
DISPLAY "Displaying in Indorer Traversal: "
CALL inorder(root);
ELSE
RETURN false
Inorder Traversal
inorder FUNCTION with node as parameter
IF node NOT EQUALS NULL
CALL inorder(node->left)
DISPLAY node->data
CALL inorder(node->right)
Depth of tree
depth FUNCTION with node as parameter
IF node EQUALS NULL
RETURN 0
RETURN add 1 to maximum from depth(node->left),depth(node->right)
Code:
#include<iostream>
using namespace std;
class TreeNode
{
public:int data;
TreeNode* left=NULL;
TreeNode* right=NULL;
};
class BST
{
public:
int n,x;
TreeNode * root=NULL;
TreeNode *createNode(int val){
TreeNode *node=new TreeNode();
node->data=val;
node->left=NULL;
node>right=NULL;
return node;
}
TreeNode *createNode(TreeNode * node,int ipdata)
{ if(node==NULL)
node=createNode(ipdata);
else if(node->data > ipdata)
node>left=createNode(node->left,ipdata);
else
node->right=createNode(node->right,ipdata);
return node;
void input(){
cout<<"Enter number of nodes to be added: ";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Enter value: ";
cin>>x;
root=createNode(root,x);
}
}
void display(){
cout<<"Displaying in Inorder Traversal: ";
inorder(root);
}
}
else if(node->data > ipdata)
search(node->left,ipdata);
else if(node->data < ipdata)
search(node->right,ipdata);
else return false;
}
return node->data;
}
int depth(TreeNode * node){
if(node==NULL)
return 0;
return (max(depth(node->left),depth(node->right))+1); }
case 4: obj.mirror(obj.root);
cout<<"\nSwapped"; break;
case 5:obj.display(); break;
case 6:cout<<"Enter value: ";
int n;
cin>>n;
if(obj.search(obj.root,n))
cout<<"Element Found";
else
cout<<"Element Not Found"; break;
case 7:return 0;
default:cout<<"\nWrong choice"; }
}
return 0;
}
Output:
DSAL Assignment – 4
Name: Aryan Bhingare
Roll no: 1022
Batch: S4 Comp
Problem Statement:
Construct an expression tree from the given prefix expression eg: +--a*bc/def and traverse it
using postorder traversal (nonrecursive) and then delete the entire tree.
Pseudo Code:
Clear function to clear top node
FUNCTION clear
top=NULL
END FUNCTION
return prt
END IF
END FUNCTION
Insert function
FUNCTION insert with var as argument
IF call isDigit(val)
nptr->right=pop()
push(nptr)
ELSE
DISPLAY "Invalid Expression"
return
END IF
END FUNCTION
Function to check whether given argument is character/digit or not
FUNCTION isDigit with ch as argument
Building tree
FUNCTION buildTree with eqn as argument
FOR i=eqn.length()-1 till less than or equal to 0
call insert(eqn[i])
END FOR
END FUNCTION
END FUNCTION
Code:
#include<bits/stdc++.h>
using namespace std; class
TreeNode
{ public:
char data;
TreeNode *left, *right;
TreeNode(char data)
{
this->data = data; this->left
= NULL; this->right =
NULL;
} };
class StackNode
{ public:
TreeNode *treeNode;
StackNode *next;
StackNode(TreeNode *treeNode)
{ this->treeNode =
treeNode; next = NULL;
} }; class
ExpressionTree {
public:
StackNode *top;
ExpressionTree()
{
top = NULL;
} void
clear()
{
top = NULL;
} void push(TreeNode
*ptr)
{ if (top == NULL) top =
new StackNode(ptr); else
{
StackNode *nptr = new StackNode(ptr);
nptr->next = top; top = nptr; }
}
TreeNode *pop()
{ if (top ==
NULL)
{
cout<<"Underflow"<<endl;
return 0; } else
{
TreeNode *ptr = top->treeNode;
top = top->next; return ptr; }
}
TreeNode *peek()
{ return top-
>treeNode; } void
insert(char val) { if
(isDigit(val))
{
TreeNode *nptr = new TreeNode(val);
push(nptr); } else if (isOperator(val))
{
nptr->right = pop();
push(nptr); } else {
cout<<"Invalid Expression"<<endl;
return; } } bool isDigit(char ch)
{
return ch >= 'a' && ch <= 'z';
} bool isOperator(char
ch)
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
} void buildTree(string
eqn)
{
for (int i = eqn.length() - 1; i >= 0; i--)
insert(eqn[i]); } void postfix() {
postOrder(peek()); }
void postOrder(TreeNode *ptr)
{ if (ptr !=
NULL) {
postOrder(ptr-
>left);
postOrder(ptr-
>right);
cout<<ptr-
>data;
} } void deleteTree(StackNode
*ptr)
et.deleteTree(et.top); cout<<"\nTree
deleted"; return 0; }
Output:
DSAL Assignment – 5
Name: Aryan Bhingare
Rollno: 1022
Batch: S4 Comp
Problem Statement:
Convert given binary tree into threaded binary tree. Analyze time and space complexity of the
algorithm.
Code:
#include <iostream>
struct Node
int key;
bool isThreaded;
};
if (root == NULL)
return NULL;
root->right == NULL)
return root;
if (root->left != NULL)
Node* l = createThreaded(root->left);
l->right = root;
l->isThreaded = true;
if (root->right == NULL)
return root;
return createThreaded(root->right);
root = root->left;
return root;
if (cur->isThreaded)
cur = cur->right;
else
cur = leftMost(cur->right);
}
temp->key = key;
return temp;
int main()
root->left = newNode(5);
root->right = newNode(19);
root->left->left = newNode(20);
root->left->right = newNode(21);
root->right->left = newNode(22);
root->right->right = newNode(30);
createThreaded(root);
inOrder(root);
return 0;
Output:
DSAL Assignment – 6
Name: Aryan Bhingare
Rollno: 1022
Batch: S4 Comp
Problem Statement:
There are flight paths between cities. If there is a flight between city A and city B then there
is an edge between the cities. The cost of the edge can be the time that flight take to reach
city B from A, or the amount of fuel used for the journey. Represent this as a graph. The node
can be represented by airport name or name of the city. Use adjacency list representation of
the graph or use adjacency matrix representation of the graph. Check whether the graph is
connected or not. Justify the storage representation used.
Code:
#include <iostream>
#include <queue>
visited[s] = 1;
dfs(i, n, arr);
}
}
bool visited[n];
visited[i] = false;
int v;
queue<int> bfsq;
if (!visited[s])
bfsq.push(s);
visited[s] = true;
while (!bfsq.empty())
v = bfsq.front();
= true;
bfsq.push(i);
bfsq.pop();
}
int main()
int n, u;
cin >> n;
string cities[n];
cout << "Enter city name for city no." << i+1 <<" : ";
cout << "city :" << i << ": " << cities[i] << endl;
cout << "Enter distance between " << cities[i] << " and " << cities[j] << " : "; cin
>> adj_mat[i][j];
adj_mat[j][i] = adj_mat[i][j];
}
}
<< cities[i];
<< endl;
cin >> u;
dfs(u, n, cities);
bfs(u, n, cities);
return 0;
}
Output:
DSAL Assignment – 7
Name: Aryan Bhingare
Rollno: 1022
Batch: S4 Comp
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.
Code:
#include<iostream>
#define ROW 10
#define COL 10
class prims
};
void prims::createGraph ()
int i, j;
cout << "Enter Total Offices: ";
cout << "Enter distance between " << i << " and " << j <<
graph[j][i] = graph[i][j];
if (graph[i][j] == 0)
void prims::primsAlgo ()
{
int selected[ROW], i, j, ne = 0;
int cost = 0;
for (i = 0; i < nodes; i++)
selected[i] = zero;
< nodes - 1)
min = infi;
if (selected[i] == one)
if (selected[j] == zero)
min = graph[i][j];
x = i;
y = j;
selected[y] = one;
cost += graph[x][y];
ne++;
}
cout << "\nTotal cost is: " << cost << endl;
int main ()
prims MST;
MST.createGraph ();
MST.primsAlgo ();
}
Output:
DSAL Assignment – 8
Name: Aryan Bhingare
Rollno: 1022
Batch: S4 Comp
Problem Statement:
A Dictionary stores keywords & its meanings. Provide facility for adding new keywords,
deleting keywords, updating values of any 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 Height balance tree and find the complexity for finding a keyword.
Code:
#include<iostream>
#include<string.h>
class node
public:
char k[20];
char m[20];
};
class dict
public:
node *root;
void create();
};
do
cout<<"\nEnter Keyword:";
cin>>temp->k;
cout<<"\nEnter Meaning:";
cin>>temp->m;
temp->left = NULL;
temp->right = NULL;
if(root == NULL)
{
root = temp;
else
insert(root, temp);
cin>>ch;
while(ch == 1);
{
if(strcmp (temp->k, root->k) < 0 )
if(root->left == NULL)
root->left = temp;
else
insert(root->left,temp);
else
{ if(root->right == NULL)
root->right = temp;
else
insert(root->right,temp);
disp(root->left);
disp(root->right);
int c=0;
while(root != NULL)
c++;
if(strcmp (k,root->k) == 0)
cout<<"\nNo of Comparisons:"<<c;
return 1;
}
if(strcmp (k, root->k) < 0)
root = root->left;
root = root->right;
return -1;
while(root != NULL)
if(strcmp (k,root->k) == 0)
cin>>root->m;
return 1;
root = root->left;
root = root->right;
}
return -1;
{
node *temp;
if(root == NULL)
cout<<"\nElement No Found";
return root;
if (strcmp(k,root->k) < 0)
return root;
if (strcmp(k,root->k) > 0)
return root;
if (root->right==NULL&&root->left==NULL)
temp = root;
delete temp;
return NULL;
}
if(root->right==NULL)
{
temp = root;
root = root->left;
delete temp;
return root;
else if(root->left==NULL)
temp = root;
root = root->right;
delete temp;
return root;
temp = min(root->right);
strcpy(root->k,temp->k);
return root;
while(q->left != NULL)
q = q->left;
return q;
}
int main()
{
int ch;
dict d;
d.root = NULL;
do
cout<<"\n-------------------------------Menu-------------------------------"; cout<<"\
cin>>ch;
switch(ch)
case 1: d.create();
break;
else
d.disp(d.root);
}
break;
"; }
else
{
cout<<"\nEnter Keyword which you want to search:";
char k[20];
cin>>k;
if( d.search(d.root,k) == 1)
cout<<"\nKeyword Found";
else
break;
case 4:
if(d.root == NULL)
"; }
else
cin>>k;
if(d.update(d.root,k) == 1)
cout<<"\nMeaning Updated";
else
break;
case 5:
if(d.root == NULL)
else
char k[20];
cin>>k;
if(d.root == NULL)
else
d.root = d.del(d.root,k);
}
}
while(ch<=5);
return 0;
}
Output:
DSAL Assignment – 9
Name: Aryan Bhingare
Rollno: 1022
Batch: S4 Comp
Problem Statement:
Code:
#include<iostream>
int i, j, k, temp;
break;
temp = a[k];
a[k] = a[k+i];
a[k+i] = temp;
int main()
int n, i;
"; cin>>n;
int arr[n];
cin>>arr[i];
ShellSort(arr, n);
cout<<"->"<<arr[i];
return 0;
Output:
DSAL Assignment – 10
Name: Aryan Bhingare
Rollno: 1022
Batch: S4 Comp
Problem Statement:
Department maintains a student information. The file contains roll number, name, division
and address. Allow user to add, delete information of student. Display information of
particular employee. If record of student does not exist an appropriate message is displayed.
If it is, then the system displays the student details. Use sequential file to main the data.
Code:
#include<iostream>
#include<fstream>
#include<cstring>
class tel{
public:
int rollNo,roll1;
char name[10];
char div;
char address[20];
void accept()
cin>>rollNo;
cin>>name;
cout<<"\n\tEnter the Division:";
cin>>div;
cin>>address;
void accept2()
cin>>rollNo;
void accept3()
cin>>name;
int getRollNo()
return rollNo;
void show()
{
cout<<"\n\t"<<rollNo<<"\t\t"<<name<<"\t\t"<<div<<"\t\t"<<address;
};
int main()
{
int
i,n,ch,ch1,rec,start,count,add,n1,add2,start2,n2,y,a,b,on,oname,add3,start3,n3,y1,add4,start4,
n4;
char name[20],name2[20];
tel t1;
count=0;
fstream g,f;
do
cout<<"\n>>>>>>>>>>>>>>>>>>>>>>MENU<<<<<<<<<<<<<<<<<<<<";
cin>>ch;
switch(ch)
case 1:
f.open("StuRecord.txt",ios::out);
x:t1.accept();
f.write((char*) &t1,(sizeof(t1)));
cin>>ch1;
if(ch1==1)
goto x;
else
{
f.close();
break;
}
case 2:
f.open("StuRecord.txt",ios::in);
f.read((char*) &t1,(sizeof(t1)));
while(f)
t1.show();
f.read((char*) &t1,(sizeof(t1)));
f.close();
break;
case 3:
cin>>rec;
f.open("StuRecord.txt",ios::in|ios::out);
f.read((char*)&t1,(sizeof(t1)));
while(f)
if(rec==t1.rollNo)
cout<<"\nRecord found";
add=f.tellg();
f.seekg(0,ios::beg);
start=f.tellg();
n1=(add-start)/(sizeof(t1));
f.seekp((n1-1)*sizeof(t1),ios::beg);
t1.accept();
f.write((char*) &t1,(sizeof(t1)));
f.close();
count++;
break;
f.read((char*)&t1,(sizeof(t1)));
if(count==0)
f.close();
break;
case 4:
cin>>name;
f.open("StuRecord.txt",ios::in|ios::out);
f.read((char*)&t1,(sizeof(t1)));
while(f)
y=(strcmp(name,t1.name));
if(y==0)
cout<<"\nName found";
add2=f.tellg();
f.seekg(0,ios::beg);
start2=f.tellg();