DSA-II Practical Lab book Solutions.docx
DSA-II Practical Lab book Solutions.docx
Set A
a) Implement a Binary search tree (BST) library (btree.h) with operations –
create, search, insert, inorder, preorder and postorder. Write a menu driven
program that performs the above operations.
#include<stdio.h>
#include<stdlib.h>
struct bst
{
int data;
struct bst *lchild,*rchild;
}node;
int cnt=0,leafcnt=0,nleafcnt=0;
struct bst *create()
{
struct bst *temp=(struct bst*)malloc(sizeof(struct bst));
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}
void insert(struct bst *r, struct bst *new1)
{
if(new1->data < r->data)
{
if(r->lchild==NULL)
r->lchild=new1;
else
insert(r->lchild,new1);
}
if(new1->data > r->data)
{
if(r->rchild==NULL)
r->rchild=new1;
else
insert(r->rchild,new1);
}
}
while(r!=NULL)
{
top++;
stack[top]=r;
r=r->lchild;
}
r=stack[top];
top--;
printf("%d\t",r->data);
r=r->rchild;
if(r!=NULL)
{
top++;
stack[top]=r;
r=r->lchild;
}
}
}
}
Save this above code in the stack library file as btree.h
#include<stdio.h>
#include<stdlib.h>
#include "btree.h"
int main()
{
int ch,n,i,value,cnt;
struct bst *newnode,*root,*temp;
root=NULL;
while(1)
{
printf("\n---Binary Search Tree---\n");
printf("1.Insert\n");
printf("2.Search\n");
printf("3.Count Total Nodes\n");
printf("4.Count Leaf Nodes\n");
printf("5.Count Non Leaf Nodes\n");
printf("6.Inorder Traversal (Recursive)\n");
printf("7.Postorder Traversal (Recursive)\n");
printf("8.Preorder Traversal (Recursive)\n");
printf("9.Inorder Traversal (Non Recursive\n");
printf("10.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nHow many nodes to create:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=create();
printf("\nEnter the node data:");
scanf("%d",&newnode->data);
if(root==NULL)
root=newnode;
else
insert(root,newnode);
}
break;
case 2: printf("\nEnter the node value to be searched:");
scanf("%d",&value);
temp=search(root,value);
if(temp==NULL)
printf("\nNode Not Found\n");
else
printf("\nNode Found\n");
break;
case 3: cnt=count(root);
printf("\nTotal Nodes=%d\n",cnt);
break;
case 4: cnt=countleaf(root);
printf("\nTotal Leaf Nodes=%d\n",cnt);
break;
case 5: cnt=countnleaf(root);
printf("\nTotal Non Leaf Nodes=%d\n",cnt);
break;
case 6: printf("\nInorder Traversal=");
inorder(root);
break;
case 7: printf("\nPostorder Traversal=");
postorder(root);
break;
case 8: printf("\nPreorder Traversal=");
preorder(root);
break;
case 9: printf("\nInorder Traversal=");
inorder_n(root);
break;
case 10: exit(0);
default: printf("\nInvalid Choice\n");
}
}
}
b) Write a program which uses binary search tree library and counts the total
nodes and total leaf nodes in the tree.
int count(T) – returns the total number of nodes from BST
int countLeaf(T) – returns the total number of leaf nodes from BST
#include<stdio.h>
#include<stdlib.h>
#include "btree.h"
int main()
{
int ch,n,i,value,cnt;
struct bst *newnode,*root,*temp;
root=NULL;
while(1)
{
printf("\n---Binary Search Tree---\n");
printf("1.Insert\n");
printf("2.Search\n");
printf("3.Count Total Nodes\n");
printf("4.Count Leaf Nodes\n");
printf("5.Count Non Leaf Nodes\n");
printf("6.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nHow many nodes to create:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=create();
printf("\nEnter the node data:");
scanf("%d",&newnode->data);
if(root==NULL)
root=newnode;
else
insert(root,newnode);
}
break;
case 2: printf("\nEnter the node value to be searched:");
scanf("%d",&value);
temp=search(root,value);
if(temp==NULL)
printf("\nNode Not Found\n");
else
printf("\nNode Found\n");
break;
case 3: cnt=count(root);
printf("\nTotal Nodes=%d\n",cnt);
break;
case 4: cnt=countleaf(root);
printf("\nTotal Leaf Nodes=%d\n",cnt);
break;
case 5: cnt=countnleaf(root);
printf("\nTotal Non Leaf Nodes=%d\n",cnt);
break;
case 6: exit(0);
default: printf("\nInvalid Choice\n");
}//switch
}//while
}//main
Btree.h
#include<stdio.h>
#include<stdlib.h>
struct bst
{
int data;
struct bst *lchild,*rchild;
}node;
int cnt=0,leafcnt=0,nleafcnt=0;
//creating a new node
struct bst *create()
{
struct bst *temp=(struct bst*)malloc(sizeof(struct bst));
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}
Set B
a) Write a C program which uses Binary search tree library and implements
following function with recursion:
T copy(T) – create another BST which is exact copy of BST which is passed as
parameter. int compare(T1, T2) – compares two binary search trees and returns
1 if they are equal and 0 otherwise.
Set A
a) Write a C program which uses Binary search tree library and displays nodes
at each level, count of node at each level and total levels in the tree.
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
struct Btree
int data;
};
int front,rear;
};
q->Q[++q->rear]=t;
return(q->Q[++q->front]);
if(q->front==q->rear)
return 1;
else
return 0;
}
void LevelDisp(BNODE *h)
QUEUE q;
BNODE *temp;
int total=0,cnt=0,level=0;
q.front=q.rear=-1;
temp=h;
Addq(&q,temp);
Addq(&q,NULL);
while(!isempty(&q))
{
temp=Delq(&q);
if(temp==NULL)
{
if(!isempty(&q))
{
printf(" = %d Level-%d\n",cnt,level);
cnt=0;
level++;
Addq(&q,NULL);
}
}
else
{
total++;
cnt++;
printf("%d ",temp->data);
if(temp->lchild!=NULL)
Addq(&q,temp->lchild);
if(temp->rchild!=NULL)
Addq(&q,temp->rchild);
}
}
printf("=%d Level-%d\n",cnt,level);
printf("\nTotal Nodes:%d",total);
printf("\nTotal Levels:%d\n",level+1);
node->data = data;
node->lchild = NULL;
node->rchild = NULL;
return(node);
}
void main()
header->lchild = newNode(50);
header->rchild = newNode(200);
header->rchild->lchild=newNode(150);
header->lchild->lchild = newNode(20);
header->lchild->rchild = newNode(80);
header->lchild->rchild->lchild = newNode(70);
LevelDisp(header);
Set B
#include <stdio.h>
void main()
scanf("%d", &no);
scanf("%d", &heap[i]);
c = i;
do
root = (c - 1) / 2;
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
c = root;
} while (c != 0);
temp = heap[0];
heap[j] = temp;
root = 0;
do
{
c++;
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
root = c;
printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");
Set A
a) Write a C program that accepts the vertices and edges of a graph and stores it as an
adjacency matrix. Display the adjacency matrix.
b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement functions to print indegree, outdegree and total degree of all
vertices of graph.
int mat[10][10];
void display(int);
void degree(int);
int main()
int i, j,n;
char reply;
scanf("%d",&n);
{
{
mat[i][j] = 1;
else
mat[i][j] = 0;
}
}
display(n);
degree(n);
return 0;
}
void display(int size)
int i,j;
printf("\n--------------------------------------------------------------------\n");
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
void degree(int n)
int i,j,indegree=0,outdegree=0;
{
for(j=0;j<n;j++)
{
if (mat[i][j]==1)
outdegree++;
if (mat[j][i]==1)
indegree++;
}
printf("\nIndegree, Outdegree and Total Degree of vertex %d is %d, %d,
%d",i+1,indegree,outdegree,indegree+outdegree);
indegree=0;
outdegree=0;
} // for
printf("\n\n");
Set B
a) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement function to traverse the graph using Breadth First Search
(BFS) traversal.
#include<stdio.h>
#include<stdlib.h>
#define initial 1
#define waiting 2
#define visited 3
int n;
int mat[MAX][MAX];
int state[MAX];
void accept();
void BF_Traversal();
int delete_queue();
int isEmpty_queue();
int main()
accept();
BF_Traversal();
return 0;
void accept()
int i, j;
char reply;
scanf("%d",&n);
{
{
mat[i][j] = 1;
else
mat[i][j] = 0;
}
}
void BF_Traversal()
int v;
state[v] = initial;
scanf("%d", &v);
BFS(v);
printf("\n\n");
void BFS(int v)
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;
{
{
insert_queue(i);
state[i] = waiting;
}
}
}
printf("\n");
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
int isEmpty_queue()
{
return 1;
else
return 0;
int delete_queue()
int delete_item;
delete_item = queue[front];
front = front+1;
return delete_item;
b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency matrix. Implement function to traverse the graph using Depth First Search
(DFS) traversal.
#include<stdio.h>
void DFS(int);
int mat[10][10],visited[10],n;
void main()
int i,j,v;
char reply;
printf("How many vertices:");
scanf("%d",&n);
{
{
mat[i][j] = 1;
else
mat[i][j] = 0;
}
}
for(i=0;i<n;i++)
visited[i]=0;
scanf("%d",&v);
DFS(v);
printf("\n\n");
void DFS(int i)
{
int j;
printf("%d ",i);
visited[i]=1;
for(j=0;j<n;j++)
DFS(j);
Set A
a) Write a C program that accepts the vertices and edges of a graph. Create adjacency list
and display the adjacency list.
#include<stdio.h>
#include<stdlib.h>
int vertex;
}node;
node *ll[20];
int n;
//create adjacency list
void read_graph();
void insert(int,int);
void printlist();
void main()
read_graph();
printlist();
printf("\n");
void read_graph()
int i,vi,vj,no_of_edges;
scanf("%d",&n);//n=6
ll[0]=NULL;
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
node *p,*q;
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
if(ll[vi]==NULL)
ll[vi]=q;
else
p=ll[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}//insert()
void printlist()
int i;
{
printf("\n%d=",i);
p=ll[i];
while(p!=NULL)
{
printf("%d->",p->vertex);
p=p->next;
}
printf("NULL\n");
} // for
b) Write a C program that accepts the vertices and edges of a graph. Create adjacency list.
Implement functions to print indegree, outdegree and total degree of all vertex of graph.
#include<stdio.h>
#include<stdlib.h>
int vertex;
}node;
node *ll[20];
int n;
void read_graph();
void insert(int,int);
void degree();
void main()
int i,v;
read_graph();
degree(n);
printf("\n\n");
void read_graph()
int i,vi,vj,no_of_edges;
scanf("%d",&n);
//initialise ll[] with a null
ll[0]=NULL;
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
node *p,*q;
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
if(ll[vi]==NULL)
ll[vi]=q;
else
p=ll[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
void degree(int n)
int i,indegree[10],outdegree[10];
for(i=0;i<10;i++)indegree[i]=0;
for(i=0;i<10;i++)outdegree[i]=0;
{
p=ll[i];
while(p!=NULL)
{
indegree[p->vertex]+=1;
outdegree[i]+=1;
p=p->next;
}
} // for
for(i=0;i<n;i++)
Set B
a) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency list. Implement function to traverse the graph using Breadth First Search (BFS)
traversal.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
int vertex;
};
struct Graph {
int numVertices;
int* visited;
};
// BFS algorithm
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
temp = temp->next;
// Creating a node
newNode->vertex = v;
newNode->next = NULL;
return newNode;
// Creating a graph
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
return graph;
// Add edge
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
// Create a queue
q->front = -1;
q->rear = -1;
return q;
if (q->rear == -1)
return 1;
else
return 0;
if (q->rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
return item;
int i = q->front;
if (isEmpty(q)) {
printf("Queue is empty");
} else {
}
int main() {
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 0);
addEdge(graph, 2, 3);
addEdge(graph, 3, 3);
//addEdge(graph, 3, 4);
bfs(graph, 2);
return 0;
b) Write a C program that accepts the vertices and edges of a graph and store it as an
adjacency list. Implement function to traverse the graph using Depth First Search (DFS)
traversal.
#include<stdio.h>
#include<stdlib.h>
int vertex;
}node;
node *ll[20];
int visited[20];
int n;
void read_graph();
void insert(int,int);
void DFS(int);
void main()
int i,v;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
scanf("%d",&v);
DFS(v);
printf("\n\n");
void DFS(int i)
{
node *p;
printf("%d ",i);
p=ll[i];
visited[i]=1;
while(p!=NULL)
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
void read_graph()
int i,vi,vj,no_of_edges;
scanf("%d",&n);
ll[0]=NULL;
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
node *p,*q;
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
if(ll[vi]==NULL)
ll[vi]=q;
else
p=ll[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
Set A
a) Write a C program for the implementation of Dijkstra’s shortest path algorithm for
finding shortest path from a given source vertex using adjacency cost matrix.
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void main(){
int G[MAX][MAX], i, j, n, u;
clrscr();
printf("\nEnter the no. of vertices:: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix::\n");
for(i=0;i < n;i++)
for(j=0;j < n;j++)
scanf("%d", &G[i][j]);
printf("\nEnter the starting node:: ");
scanf("%d", &u);
dijikstra(G,n,u);
getch();
}
Set B
a) Write a C program for the implementation of Floyd Warshall’s algorithm for finding all
pairs shortest path using adjacency cost matrix.
#include<stdio.h>
int i, j, k,n,dist[10][10];
void floydWarshell ()
{
for (k = 0; k < n; k++)
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
int main()
{
int i,j;
printf("enter no of vertices :");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("dist[%d][%d]:",i,j);
scanf("%d",&dist[i][j]);
}
floydWarshell();
printf (" \n\n shortest distances between every pair of vertices \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
printf ("%d\t", dist[i][j]);
printf("\n");
}
return 0;
}
Assignment 7: Hash Table-I
Set A
a) Write a program to implement various types of hash functions which are used
to place the data in a hash table
a. Division Method
b. Mid Square Method
c. Digit Folding Method
Accept n values from the user and display appropriate message in case of
collision for each of the above functions.
Set B
a) Write a menu driven program to implement hash table using array (insert,
search, delete, display). Use any of the above-mentioned hash functions. In case
of collision apply linear probing.
b) Write a menu driven program to implement hash table using array (insert,
search, delete, display). Use any of the above-mentioned hash functions. In case
of collision apply quadratic probing.
Set A
a) Implement hash table using singly linked lists. Write a menu driven program to
perform operations on the hash table (insert, search, delete, display). Select appropriate
hashing function. In case of collision, use separate chaining.
#include<stdio.h>
#define size 7
int arr[size];
void init()
{
int i;
for(i = 0; i < size; i++)
arr[i] = -1;
}
if(arr[key] == -1)
{
arr[key] = value;
printf("%d inserted at arr[%d]\n", value,key);
}
else
{
printf("Collision : arr[%d] has element %d already!\n",key,arr[key]);
printf("Unable to insert %d\n",value);
}
}
void print()
{
int i;
for(i = 0; i < size; i++)
printf("arr[%d] = %d\n",i,arr[i]);
}
int main()
{
init();
insert(10); //key = 10 % 7 ==> 3
insert(4); //key = 4 % 7 ==> 4
insert(2); //key = 2 % 7 ==> 2
insert(3); //key = 3 % 7 ==> 3 (collision)
printf("Hash table\n");
print();
printf("\n");
return 0;
}
Set B
a) Implement hash table using doubly linked lists. Write a menu driven program to perform
operations on the hash table (insert, search, delete, display). Select appropriate hashing function.
In case of collision, use separate chaining.