0% found this document useful (0 votes)
37 views67 pages

1022 - Aryan Bhingare - DSAL A1-11

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views67 pages

1022 - Aryan Bhingare - DSAL A1-11

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 67

DSAL Assignment – 1

Name: Aryan Bhingare

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:

Inserting into hash table

FUNCTION insertData

DISPLAY "Enter number of clients: "

INPUT n

FOR i = 0 till less than n

DISPLAY "Enter name and tel no.: "

INPUT name tno

dgt = tno MOD 10

IF d[dgt].telno equals 0

assign dgt to value

ELSE

flag=1

WHILE j not equals (IF dgt equals 0 then 9 else dgt-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

DISPLAY "Collision Detected"

END IF

DISPLAY "Data added successfully"

ELSE

DISPLAY "Hash Table Full"

break

END IF

END FOR

END FUNCTION

Displaying hash table

FUNCTION displayData

DISPLAY " Sr. Tel No. Name"

FOR i = 0 till less than 10

DISPLAY d[i].telno d[i].name

END FOR END FUNCTION

Searching data into hash table

FUNCTION searchData
DISPLAY "Enter telno to be searched: "

INPUT tno

dgt = tno MOD 10

IF d[dgt].telno equals tno

DISPLAY "Found " d[dgt].name "with 1 comparisons"

ELSE

while j not equals (if dgt equals 0 then 9 else dgt-1)

IF d[j].telno equals tno


DISPLAY "Found" d[j].name "with" count " comparisons"

break

END IF

INCREMENT j

j = j MOD 10

INCREMENT count

END WHILE

IF count is greater than or equal to 10

DISPLAY " Not found"

END IF

END IF

END FUNCTION

Code:

#include<iostream>

using namespace std;

struct node{

int value;
node* next;

}*HashTable[10];

class hashing{

public:

hashing(){

for(int i=0 ; i<10 ; i++){

HashTable[i]=NULL; }

}
int HashFunction(int value){

return (value%10);

node* create_node(int x){

node* temp=new node;

temp->next=NULL;

temp->value=x; return

temp;

void display(){ for(int

i=0 ; i< 10; i++){

node * temp=new node;

temp=HashTable[i];

cout<<"a["<<i<<"] : ";

while(temp !=NULL){

cout<<" ->"<<temp->value;

temp=temp->next;

cout<<"\n";

int searchElement(int value){

bool flag = false;

int hash_val = HashFunction(value); node*

entry = HashTable[hash_val];
cout<<"\nElement found at : "; while (entry !

= NULL)

if (entry->value==value)

cout<<hash_val<<" : "<<entry->value<<endl; flag = true;

entry = entry->next;

if (!flag)

return -1;

}
void deleteElement(int value){ int

hash_val = HashFunction(value);

node* entry = HashTable[hash_val];

if (entry == NULL )

cout<<"No Element found ";

return;

if(entry->value==value){ HashTable[hash_val]=entry->next; return;

while ((entry->next)->value != value)

entry = entry->next;

entry->next=(entry->next)->next;
}

void insertElement(int value){ int

hash_val = HashFunction(value);

node* temp=new node;

node* head=new node; head

= create_node(value);

temp=HashTable[hash_val];

if (temp == NULL)

HashTable[hash_val] =head;

else{

while (temp->next != NULL)

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;

cout<<"Enter your choice: "; cin>>ch;

switch(ch){

case 1:cout<<"\nEnter phone no. to be inserted : ";

cin>>data;

h.insertElement(data);

break;

case 2:h.display();

break;

case 3:cout<<"\nEnter the no to be searched : ";

cin>>search;

if (h.searchElement(search) == -1)

cout<<"No element found at key ";

continue;

break;

case 4:cout<<"\nEnter the phno. to be deleted : ";

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

DISPLAY "Enter number of keys: ";

INPUT n

FOR i=0 till less than n

DISPLAY "Enter key and value: ";

INPUT k,v

dgt = k mod 10

Node *temp=ht[dgt]

Node *pointer=NULL

WHILE temp not equals NULL

pointer=temp

temp=temp->next

END WHILE

IF temp equals NULL

temp = new Node(k,v)


IF pointer equals NULL

ht[dgt]=temp

ELSE

pointer->next=temp

END IF

ELSE

copy string v to temp->value

END IF

DISPLAY "Data added Successfully"

END FOR
END FUNCTION

Search Data

FUNCTION searchData

Initialize flag to 0 and counter to 1

DISPLAY "Enter key: "

INPUT k

dgt = k mod 10

Node *temp=ht[dgt]

IF temp equals NULL

DISPLAY "Not Found"

return FUNCTION

END IF

WHILE temp->next not equals NULL

IF temp->key equals k

DISPLAY "Found value: ",temp->value


return FUNCTION

END IF

temp=temp->next

END WHILE

IF temp->key equals k

DISPLAY "Found value: ",temp->value

ELSE

DISPLAY "Data not found"

END IF

END FUNCTION

Displaying Data

FUNCTION displayData

FOR i=0 till less than 10


DISPLAY i,"--->"

Node *temp

IF temp equals NULL

continue

END IF

WHILE temp->next not equals NULL

DISPLAY "(",temp->key,", ",temp->value,") ---> "

temp=temp->next

END WHILE

IF temp not equals NULL

DISPLAY "(",temp->key,", ",temp->value,") ---> "

END IF

END FOR

END FUNCTION

Removing Data

FUNCTION removeData

DISPLAY "Enter key to be removed: "

INPUT k

dgt = k mod 10

Node *temp=ht[dgt]

Node *pointer=ht[dgt]

IF temp not equals NULL

IF temp->key equals k and temp->next not equals NULL

DISPLAY "Element Deleted"

pointer=pointer->next

ht[dgt]=pointer

END IF

IF temp->key equals k and temp->next equals NULL

Display "Element deleted"


ht[dgt]=NULL

END IF

WHILE temp->next not equals NULL

temp=temp->next

IF temp->key equals k

pointer->next=temp->next

temp->next=NULL

DISPLAY "Element Deleted"


return FUNCTION

END IF

temp=pointer->next

pointer=pointer->next

END WHILE

ELSE

DISPLAY "Data not found"

END IF

END FUNCTION

Code:

#include <iostream>

#include <string.h> using

namespace std;

class HashFunction

typedef struct hash {

long key;

char name[10];

}hash; hash

h[10];
public:

HashFunction();

void insert();

void display(); int


find(long); void

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)

int index=find(k); if(index==-

1)

cout<<"\n\tKey Not Found";

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)

cout<<"\n\t"<<h[i].key<<" is Found at "<<i<<" Location With Name "<<h[i].name;

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];

long k,temp; int

v,hi,cnt=0,flag=0,i;

do

if(cnt>=10)

cout<<"\n\tHash Table is FULL";

break;

cout<<"\n\tEnter a Telephone No: ";

cin>>k; cout<<"\n\tEnter a Client

Name: "; cin>>n; hi=k%10;// hash

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;

for(i=0;i<hi && flag==0;i++)

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;

}
}

for(i=0;i<hi && flag==0;i++)

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

cout<<"\n\t***** Telephone (ADT) *****";

cout<<"\n\t1. Insert\n\t2. Display\n\t3. Find\n\t4. Delete\n\t5. Exit";


cout<<"\n\t..... Enter Your Choice: "; cin>>ch; switch(ch) {

case 1: obj.insert();

break;

case 2: obj.display();

break;

case 3: cout<<"\n\tEnter a Key Which You Want to Search: ";


cin>>k;

index=obj.find(k);

if(index==-1)

cout<<"\n\tKey Not Found";

break;

case 4: cout<<"\n\tEnter a Key Which You Want to Delete: ";

cin>>k;

obj.Delete(k);

break; case 5:

break;

cout<<"\n\t..... Do You Want to Continue in Main Menu:y/n ";

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

for i=0 till less than n


DISPLAY "Enter value: "
input x
root = CALL createNode(root,x)
end for

Displaying Tree
display FUNCTION
DISPLAY "Displaying in Indorer Traversal: "
CALL inorder(root);

Searching for Value


search FUNCTION with node ipdata as
parameters IF node NOT EQUALS NULL

IF node->data EQUALS ipdata)


DISPLAY "Element found"
RETURN true
ELSE IF node->data is greater than ipdata
CALL search(node->left,ipdata);

ELSE IF node->data is less than ipdata


CALL search(node->right,ipdata);

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)

Minimum value from tree


minval FUNCTION with node as parameter
while node->left NOT EQUALS NULL
node=node->left
end while
RETURN node->data

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)

Swapping left and right nodes


mirror FUNCTION with node as parameter
IF node EQUALS NULL
RETURN
ELSE
CREATE node prt
CALL mirror(node->left)
CALL mirror(node->right)
ptr=node->left
node->left=node->right
node->right=ptr

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);
}

bool search(TreeNode * node,int ipdata)


{ if(node!=NULL){ if(node-

>data==ipdata){ return true; }

}
else if(node->data > ipdata)
search(node->left,ipdata);
else if(node->data < ipdata)
search(node->right,ipdata);
else return false;
}

void inorder(TreeNode * node){


if(node!=NULL)
{ inorder(node-
>left); cout<<node-
>data<<" ";
inorder(node->right);
}
}

int minval(TreeNode * node){


while(node->left!=NULL) node=node-
>left;

return node->data;
}
int depth(TreeNode * node){
if(node==NULL)
return 0;
return (max(depth(node->left),depth(node->right))+1); }

void mirror(TreeNode * node){


if(node==NULL)
return;
else{
TreeNode * ptr;
mirror(node->left); mirror(node-
>right); ptr=node->left;
node->left=node->right; node-
>right=ptr;
}
}
};
int main(){
int val;
BST obj;
int ch;
while(1){
cout<<"\n***********************************************";
cout<<"\n1. Insert\n2. Longest Path\n3. Minimum data\n4. Swap\n5. Display\n6.
Search\n7. Exit\nEnter choice: ";
cin>>ch;
switch(ch){
case 1: obj.input();
break;
case 2: cout<<"Depth is "<<obj.depth(obj.root);
break;
case 3: cout<<"\nMinimum value is: "<<obj.minval(obj.root); break;

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

Pushing the stacknode onto stack


FUNCTION push with prt as argument
IF top equals NULL
top=new StackNode(ptr)
ELSE
StackNode *nptr=new StackNode(ptr)
nptr->next=top
top=nptr
END IF
END FUNCTION

Poping the treenode from stack


FUNCTION *pop
IF top equals NULL
DISPLAY "Underflow"
return 0
ELSE
TreeNode *ptr=top->treeNode
top=top->next

return prt
END IF
END FUNCTION

Peek function the top from treenode


FUNCTION *peek
return top->treeNode
END FUNCTION

Insert function
FUNCTION insert with var as argument
IF call isDigit(val)

TreeNode *nptr=new TreeNode(val)


push(nptr)

ELSE IF call isOperator(val)


TreeNode *nptr = new TreeNode(val)
nptr->left=pop()

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

return ch greater than 'a' and ch less than 'z'


END FUNCTION

Function to check whether given argument is operator/symbol or not


FUNCTION isOperator with ch as argument

return ch equals '+' or ch equals '-' or ch equals '*' or ch equals '/'


END FUNCTION

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

Displaying Postfix equation


FUNCTION postfix
call postorder(call peek())
END FUNCTION

Postorder traversal of tree


FUNCTION postOrder with *ptr as argument
if ptr not equals NULL
call postOrder(ptr->left)
call postOrder(ptr->right)
DISPLAY ptr->data
END IF
END FUNCTION

Deleting the tree


FUNCTION deleteTree with *ptr as argument
delete ptr

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))
{

TreeNode *nptr = new TreeNode(val); nptr->left


= pop();

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)

{ delete ptr; } }; int main() { string s;


cout<<"Expression Tree Test"<<endl;
ExpressionTree et; cout<<"\nEnter
equation in Prefix form: "; cin>>s;
et.buildTree(s); cout<<"\n\nPostorder
Traversal is : "; et.postfix();

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>

using namespace std;

struct Node

int key;

Node *left, *right;

bool isThreaded;

};

Node *createThreaded(Node *root)

if (root == NULL)

return NULL;

if (root->left == 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);

Node *leftMost(Node *root)

while (root != NULL && root->left != NULL)

root = root->left;

return root;

void inOrder(Node *root)

if (root == NULL) return;

Node *cur = leftMost(root);


while (cur != NULL)

cout << cur->key << " ";

if (cur->isThreaded)

cur = cur->right;

else

cur = leftMost(cur->right);
}

Node *newNode(int key)

Node *temp = new Node;

temp->left = temp->right = NULL;

temp->key = key;

return temp;

int main()

Node *root = newNode(4);

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);

cout << "Inorder traversal of created "

"threaded tree is\n";

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>

using namespace std;

int adj_mat[50][50] = {0, 0};

int visited[50] = {0};

void dfs(int s, int n, string arr[])


{

visited[s] = 1;

cout << arr[s] << " ";

for (int i = 0; i < n; i++)

if (adj_mat[s][i] && !visited[i])

dfs(i, n, arr);

}
}

void bfs(int s, int n, string arr[])

bool visited[n];

for (int i = 0; i < n; i++)

visited[i] = false;

int v;

queue<int> bfsq;

if (!visited[s])

cout << arr[s] << " ";

bfsq.push(s);

visited[s] = true;

while (!bfsq.empty())

v = bfsq.front();

for (int i = 0; i < n; i++)

if (adj_mat[v][i] && !visited[i]) {

cout << arr[i] << " "; visited[i]

= true;
bfsq.push(i);

bfsq.pop();
}

int main()

cout << "Enter no. of cities: ";

int n, u;

cin >> n;

string cities[n];

for (int i = 0; i < n; i++)

cout << "Enter city name for city no." << i+1 <<" : ";

cin >> cities[i];

cout << "\nYour cities are: " << endl;

for (int i = 0; i < n; i++)

cout << "city :" << i << ": " << cities[i] << endl;

for (int i = 0; i < n; i++)

for (int j = i + 1; j < n; j++)

cout << "Enter distance between " << cities[i] << " and " << cities[j] << " : "; cin

>> adj_mat[i][j];

adj_mat[j][i] = adj_mat[i][j];

}
}

cout << endl;

for (int i = 0; i < n; i++)

cout << "\t" << cities[i] << "\t"; for

(int i = 0; i < n; i++)

cout << "\n"

<< cities[i];

for (int j = 0; j < n; j++)

cout << "\t" << adj_mat[i][j] << "\t"; cout

<< endl;

cout << "Enter Starting Vertex: ";

cin >> u;

cout << "DFS: ";

dfs(u, n, cities);

cout << endl;

cout << "BFS: ";

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>

using namespace std;

#define ROW 10

#define COL 10

#define infi 9999

class prims

int graph[ROW][COL], nodes;


public:

void createGraph ();

void primsAlgo ();

};

void prims::createGraph ()

int i, j;
cout << "Enter Total Offices: ";

cin >> nodes;

cout << "\nEnter Adjacency Matrix: \n";

for (i = 0; i < nodes; i++)

for (j = i; j < nodes; j++)

cout << "Enter distance between " << i << " and " << j <<

endl; cin >> graph[i][j];

graph[j][i] = graph[i][j];

for (i = 0; i < nodes; i++)

for (j = 0; j < nodes; j++)

if (graph[i][j] == 0)

graph[i][j] = infi; //fill infinity where path is not present }

void prims::primsAlgo ()

{
int selected[ROW], i, j, ne = 0;

int zero = 0, one = 1, min = 0, x, y;

int cost = 0;
for (i = 0; i < nodes; i++)

selected[i] = zero;

selected[0] = one; //starting vertex is always node-0 while (ne

< nodes - 1)

min = infi;

for (i = 0; i < nodes; i++)

if (selected[i] == one)

for (j = 0; j < nodes; j++)

if (selected[j] == zero)

if (min > graph[i][j])

min = graph[i][j];

x = i;

y = j;

selected[y] = one;

cout << "\n" << x << " --> " << y;

cost += graph[x][y];
ne++;
}

cout << "\nTotal cost is: " << cost << endl;

int main ()

prims MST;

cout << "\nPrims Algorithm to connect several offices\n";

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>

using namespace std;

class node

public:

char k[20];

char m[20];

class node *left;

class node * right;

};

class dict

public:
node *root;

void create();

void disp(node *);

void insert(node * root,node *temp);

int search(node *,char []);

int update(node *,char []);

node* del(node *,char []);

node * min(node *);

};

void dict :: create()

class node *temp;


int ch;

do

temp = new node;

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);

cout<<"\nDo u want to add more (y=1/n=0):";

cin>>ch;

while(ch == 1);

void dict :: insert(node * root,node *temp)

{
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);

void dict:: disp(node * root)

if( root != NULL)

disp(root->left);

cout<<"\n Key Word :"<<root->k;

cout<<"\t Meaning :"<<root->m;

disp(root->right);

int dict :: search(node * root,char k[20])

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;

if(strcmp (k, root->k) > 0)

root = root->right;

return -1;

int dict :: update(node * root,char k[20])

while(root != NULL)

if(strcmp (k,root->k) == 0)

cout<<"\nEnter New Meaning of Keyword: "<<root->k;

cin>>root->m;

return 1;

if(strcmp (k, root->k) < 0)

root = root->left;

if(strcmp (k, root->k) > 0)

root = root->right;
}

return -1;

node* dict :: del(node * root,char k[20])

{
node *temp;

if(root == NULL)

cout<<"\nElement No Found";

return root;

if (strcmp(k,root->k) < 0)

root->left = del(root->left, k);

return root;

if (strcmp(k,root->k) > 0)

root->right = del(root->right, k);

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);

root->right = del(root->right, temp->k);

return root;

node * dict :: min(node *q)

while(q->left != NULL)

q = q->left;

return q;

}
int main()

{
int ch;

dict d;

d.root = NULL;

do

cout<<"\n-------------------------------Menu-------------------------------"; cout<<"\

n1.Create\n2.Display\n3.Search\n4.Update\n5.Delete\nEnter Your choice: ";

cin>>ch;

switch(ch)

case 1: d.create();

break;

case 2: if(d.root == NULL)

cout<<"\nNo any Keyword";

else

d.disp(d.root);
}

break;

case 3: if(d.root == NULL)

cout<<"\nDictionary is Empty. First add keywords then try again

"; }

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

cout<<"\nKeyword Not Found";

break;

case 4:

if(d.root == NULL)

cout<<"\nDictionary is Empty. First add keywords then try again

"; }

else

cout<<"\nEnter Keyword which meaning want to update:";


char k[20];

cin>>k;

if(d.update(d.root,k) == 1)

cout<<"\nMeaning Updated";

else

cout<<"\nMeaning Not Found";

break;

case 5:

if(d.root == NULL)

cout<<"\nDictionary is Empty. First add keywords then try again ";


}

else

cout<<"\nEnter Keyword which You want to delete:";

char k[20];

cin>>k;

if(d.root == NULL)

cout<<"\nNo any Keyword";

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:

Implement Shell sort algorithm.

Code:

#include<iostream>

using namespace std;


// A function implementing Shell sort.

void ShellSort(int a[], int n)

int i, j, k, temp;

// Gap 'i' between index of the element to be compared, initially n/2.

for(i = n/2; i > 0; i = i/2)

for(j = i; j < n; j++)

for(k = j-i; k >= 0; k = k-i)

// If value at higher index is greater, then break the loop.

if(a[k+i] >= a[k])

break;

// Switch the values otherwise.


else

temp = a[k];

a[k] = a[k+i];

a[k+i] = temp;

int main()

int n, i;

cout<<"\nEnter the number of data element to be sorted:

"; cin>>n;
int arr[n];

for(i = 0; i < n; i++)

cout<<"Enter element "<<i+1<<": ";

cin>>arr[i];

ShellSort(arr, n);

// Printing the sorted data.

cout<<"\nSorted Data: ";


for (i = 0; i < n; i++)

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>

using namespace std;

class tel{

public:
int rollNo,roll1;

char name[10];

char div;

char address[20];

void accept()

cout<<"\n\tEnter Roll Number : ";

cin>>rollNo;

cout<<"\n\tEnter the Name : ";

cin>>name;
cout<<"\n\tEnter the Division:";

cin>>div;

cout<<"\n\tEnter the Address:";

cin>>address;

void accept2()

cout<<"\n\tEnter the Roll No. to modify : ";

cin>>rollNo;

void accept3()

cout<<"\n\tEnter the name to modify : ";

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<<<<<<<<<<<<<<<<<<<<";

cout<<"\n1.Insert\n2.Show\n3.Search & Edit(number)\n4.Search & Edit(name)\n5.Search &


Edit(onlynumber)\n6.Search & edit(only name)\n7.Delete a Student Record\n 8.Exit\nEnter
the Choice:";

cin>>ch;

switch(ch)

case 1:

f.open("StuRecord.txt",ios::out);

x:t1.accept();

f.write((char*) &t1,(sizeof(t1)));

cout<<"\nDo you want to enter more records?\n1.Yes\n2.No";

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)));

//cout<<"\n\tRoll No.\t\tName \t\t Division \t\t Address";

while(f)

t1.show();

f.read((char*) &t1,(sizeof(t1)));

f.close();

break;

case 3:

cout<<"\nEnter the roll number you want to find";

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)

cout<<"\nRecord not found";

f.close();

break;

case 4:

cout<<"\nEnter the name you want to find and edit";

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();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy