0% found this document useful (0 votes)
13 views126 pages

SLIP

The document contains several C programs demonstrating data structures and algorithms, including a Binary Search Tree (BST) implementation, graph representation using adjacency matrices and lists, and traversal methods like Depth First Search (DFS) and Breadth First Search (BFS). Each section provides code snippets for creating, inserting, and traversing these structures, along with user interaction through menu-driven programs. The document serves as a comprehensive guide for implementing basic data structures and their operations in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views126 pages

SLIP

The document contains several C programs demonstrating data structures and algorithms, including a Binary Search Tree (BST) implementation, graph representation using adjacency matrices and lists, and traversal methods like Depth First Search (DFS) and Breadth First Search (BFS). Each section provides code snippets for creating, inserting, and traversing these structures, along with user interaction through menu-driven programs. The document serves as a comprehensive guide for implementing basic data structures and their operations in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 126

1..

Implement a Binary search tree (BST) library (btree.h) with operations – create,

insert, inorder. Write a menu driven program that performs the above operations.

#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode

int data;

struct bnode *left,*right;

}bnode;

bnode *insert(bnode *,int);

bnode *create();

void inorder(bnode *T);

bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");

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

scanf("%d",&x);
root=insert(root,x);

return(root);

void inorder(bnode *T)

if(T!=NULL)

inorder(T->left);

printf("%d\t",T->data);

inorder(T->right);

bnode *insert(bnode *T,int x)

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;

return(temp);

if(x>T->data)
{

else

T->right=insert(T->right,x);

return(T);

if(x<T->data)

T->left=insert(T->left,x);

return(T);

return(T);

void main()

bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Inorder");

printf("\n\t4.Exit)");

printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)
{

case 1: root=create();break;

case 2: printf("\n Enter the Node to be inserted:-->");

scanf("%d",&x);

root=insert(root,x);

break;

case 3: inorder(root);

break;

case 4: exit(0);

}while(ch!=4);

}
1.2

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 of all vertices of

graph.

#include<stdio.h>

#define MAX 10

int adj[MAX][MAX];

int n;

int main()

int max_edges, i, j, origin, destin, ind;

int graph_type;

printf("\n1. Undirected Graph\n2. Directed Graph\n");

printf("Enter your choice: ");


scanf("%d", &graph_type);

printf("\nEnter number of vertices: ");

scanf("%d", &n);

if(graph_type==1)

max_edges = n*(n-1)/2;

else

max_edges = n*(n-1);

for(i=1; i<=max_edges; i++)

printf("\nEnter edge[%d](-1 -1 to quit): ", i);

scanf("%d %d", &origin, &destin);

if((origin == -1)&&(destin == -1))

break;

if(origin >= n || destin >= n || origin < 0 || destin < 0)

else

printf("\nInvalid vertex!\n");
i--;

adj[origin][destin] = 1;

if(graph_type == 1)

adj[destin][origin] = 1;

printf("\nThe adjacency matrix is:\n");

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

for(j=0; j<=n-1; j++)

printf("%4d", adj[i][j]);

printf("\n");

printf("\nIndegree, ");

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

for(j=0, ind=0; j<n; j++)

if(adj[j][i] == 1)

ind++;

}
printf("\nv%d %5d \n",i+1,ind);

}
3.1

. Implementation of static hash table with Linear Probing.

//Hashing using linear probing : C program

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey;

printf("\nenter a value to insert into hash table\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index] == NULL)

h[index]=key;

break;

if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");

void search()

int key,index,i,flag=0,hkey;

printf("\nenter search element\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index]==key)

printf("value is found at index %d",index);

break;

if(i == TABLE_SIZE)

printf("\n value is not found\n");

void display()

int i;

printf("\nelements in the hash table are \n");

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


printf("\nat index %d \t value = %d",i,h[i]);

main()

int opt,i;

while(1)

printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

3.2
C program to implement graph traversal method using depth first search.

#include<stdio.h>

#include<stdlib.h>

void recdfs(int m[5][5],int n ,int v)

int w;

static int visited[20]={0};

visited[v]=1;

printf("v%d",v+1);

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

if((m[v][w]==1) &&(visited[w]==0))

recdfs(m,n,w);

void main()

int m[5][5]={{0,0,1,1,0},{0,0,1,0,1},{0,1,0,0,0},{0,0,0,0,1},{0,0,0,0,0}};

printf("\n the depth first search traverse is : ");

recdfs(m,5,0);

4.1

C program to implement graph traversal method using depth first search.

#include<stdio.h>

#include<stdlib.h>
void recdfs(int m[5][5],int n ,int v)

int w;

static int visited[20]={0};

visited[v]=1;

printf("v%d",v+1);

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

if((m[v][w]==1) &&(visited[w]==0))

recdfs(m,n,w);

void main()

int m[5][5]={{0,0,1,1,0},{0,0,1,0,1},{0,1,0,0,0},{0,0,0,0,1},{0,0,0,0,0}};

printf("\n the depth first search traverse is : ");

recdfs(m,5,0);

4.2

. Write a C program that accepts the vertices and edges of a graph and stores

it as an adjacency matrix. Display the adjacency matrix.

#include<stdio.h>

#include<stdlib.h>

void create(int m[10][10],int n)

{
int i,j;

char ans;

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

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

m[i][i]=0;

if(i!=j)

printf("\n Is there an edge between %d and %d : ",i+1,j+1);

scanf("%d",&m[i][j]);

void display(int m[10][10],int n)

int i,j;

printf("\n \t Adjacency matrix is : \n");

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

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

printf("%5d",m[i][j]);

printf("\n");

void main()
{

int m[10][10],n;

printf("\n \t Enter vertices : ");

scanf("%d",&n);

create(m,n);

display(m,n);

}
5.1

#include <stdio.h>

#include <stdlib.h>

#include "btree.h"

// Function to create a new node with the given data

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

if (!newNode) {

printf("Memory allocation error.\n");

return NULL;

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

// Function to insert a new node in the binary search tree

Node* insertNode(Node* root, int data) {


if (root == NULL) {

return createNode(data);

if (data < root->data) {

root->left = insertNode(root->left, data);

} else {

root->right = insertNode(root->right, data);

return root;

// Function to perform a preorder traversal

void preorder(Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preorder(root->left);

preorder(root->right);

// Function to perform a postorder traversal

void postorder(Node* root) {

if (root != NULL) {

postorder(root->left);

postorder(root->right);
printf("%d ", root->data);

#include <stdio.h>

#include <stdlib.h>

#include "btree.h"

// Function to display the menu and get the user's choice

int displayMenu() {

int choice;

printf("\nBinary Search Tree Operations\n");

printf("1. Create and Insert Node\n");

printf("2. Preorder Traversal\n");

printf("3. Postorder Traversal\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

return choice;

int main() {

Node* root = NULL;

int choice, data;

while (1) {
choice = displayMenu();

switch (choice) {

case 1:

printf("Enter data to insert: ");

scanf("%d", &data);

root = insertNode(root, data);

break;

case 2:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 3:

printf("Postorder Traversal: ");

postorder(root);

printf("\n");

break;

case 4:

printf("Exiting...\n");

exit(0);

break;
default:

printf("Invalid choice! Please try again.\n");

return 0;

}
5.2

. 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 MAXSIZE 20

typedef struct node

int vertex;

struct node *next;

}NODE;

NODE *head[10];

typedef struct

int data[MAXSIZE];

int front, rear;

}QUEUE;

void createlist(int n)

int i, j, x;

NODE *temp, *newnode;

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

{
head[i]=NULL;

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

printf("is there any edge between %d and %d (1/0): ", i+1, j+1);

scanf("%d", &x);

if(x==1)

newnode=(NODE*)malloc(sizeof(NODE));

newnode->vertex=j+1;

newnode->next=NULL;

if(head[i]==NULL)

head[i]=temp=newnode;

else

temp->next=newnode;

temp=newnode;

void addq(QUEUE *pq, int num)

pq->data[++pq->rear]=num;

}
int removeq(QUEUE *pq)

return pq->data[++pq->front];

void initq(QUEUE *pq)

pq->front=pq->rear=-1;

int isempty(QUEUE *pq)

return (pq->rear==pq->front);

void bfs(NODE *list[10], int n)

int v;

int visited[20]={0}, x;

NODE *temp;

QUEUE pq;

initq(&pq);

v=0;

visited[v]=1;

addq(&pq, v); while(!

isempty(&pq))

v=removeq(&pq);
printf(" v%d ", v+1);

temp=list[v];

while(temp)

x=temp->vertex-1;

if(visited[x]==0)

addq(&pq, x);

visited[x]=1;

temp=temp->next;

void displaylist(int n)

NODE *temp;

printf("\nThe Adjacency list is: \n");

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

printf("\nv%d -> ", i+1);

temp=head[i];

while(temp)

printf("v%d -> ", temp->vertex);


temp=temp->next;

printf("NULL");

printf("\n");

void main()

int n;

printf("Enter how many vertices: ");

scanf("%d", &n);

printf("\nEnter data for adjacency list:\n");

createlist(n);

displaylist(n);

printf("\nBFS Traversal is:");

bfs(head, n);

6.1

. Implement a Binary search tree (BST) library (btree.h) with operations –

create, insert, preorder. Write a menu driven program that performs the above

operations

#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode


{

int data;

struct bnode *left,*right;

}bnode;

bnode *insert(bnode *,int);

bnode *create();

void preorder(bnode *T);

bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");

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

scanf("%d",&x);

root=insert(root,x);

return(root);

void preorder(bnode *T)

if(T!=NULL)
{

printf("%d\t",T->data);

preorder(T->left);

preorder(T->right);

bnode *insert(bnode *T,int x)

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;

return(temp);

if(x>T->data)

else

T->right=insert(T->right,x);

return(T);

if(x<T->data)

{
T->left=insert(T->left,x);

return(T);

return(T);

void main()

bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Display");

printf("\n\t4.Exit)");

printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)

case 1: root=create();break;

case 2: printf("\n enter the key to thr inserted:-->");

scanf("%d",&x);

root=insert(root,x);

break;

case 3: preorder(root);
break;

case 4: exit(0);

}while(ch!=4);

}
6.2

Write a C program that accepts the vertices and edges of a graph. Create

adjacency list

#include<stdio.h>

#include<stdlib.h>

typedef struct node

int vertex;

struct node *next;

} NODE;

NODE *head[10];

void createlist(int n)

int i, j, x;

NODE *temp, *newnode;

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

head[i]=NULL;

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

printf("is there any edge between %d and %d (1/0): ", i+1, j+1);

scanf("%d", &x);

if (x==1)

newnode=(NODE*)malloc(sizeof(NODE));
newnode->vertex=j+1;

newnode->next=NULL;

if (head[i]==NULL)

head[i]=temp=newnode;

else

temp->next=newnode;

temp=newnode;

void displaylist(int n)

NODE *temp;

printf("\nThe Adjacency list is: \n");

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

printf("\nv%d -> ", i+1);

temp=head[i];

while(temp)

printf("v%d -> ", temp->vertex);

temp=temp->next;
}

printf("NULL");

printf("\n");

void main()

int n;

printf("Enter how many vertices: ");

scanf("%d", &n);

createlist(n);

displaylist(n);

7.1

2. Implement a Binary search tree (BST) library (btree.h) with operations –

create, insert, preorder. Write a menu driven program that performs the above

operations

#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode

int data;

struct bnode *left,*right;

}bnode;
bnode *insert(bnode *,int);

bnode *create();

void preorder(bnode *T);

bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");

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

scanf("%d",&x);

root=insert(root,x);

return(root);

void preorder(bnode *T)

if(T!=NULL)

printf("%d\t",T->data);

preorder(T->left);

preorder(T->right);
}

bnode *insert(bnode *T,int x)

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;

return(temp);

if(x>T->data)

else

T->right=insert(T->right,x);

return(T);

if(x<T->data)

T->left=insert(T->left,x);

return(T);

return(T);
}

void main()

bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Display");

printf("\n\t4.Exit)");

printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)

case 1: root=create();break;

case 2: printf("\n enter the key to thr inserted:-->");

scanf("%d",&x);

root=insert(root,x);

break;

case 3: preorder(root);

break;

case 4: exit(0);

}while(ch!=4);
}
7..2

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>

#include <stdlib.h>

/* ADJACENCY MATRIX */

int source,V,E,time,visited[20],G[20][20];

void DFS(int i)

int j;

visited[i]=1;

printf(" %d->",i+1);

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

if(G[i][j]==1&&visited[j]==0)

DFS(j);

int main()

int i,j,v1,v2;

printf("\t\t\tGraphs\n");

printf("Enter the no of edges:");

scanf("%d",&E);
printf("Enter the no of vertices:");

scanf("%d",&V);

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

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

G[i][j]=0;

/* creating edges :P */

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

printf("Enter the edges (format: V1 V2) : ");

scanf("%d%d",&v1,&v2);

G[v1-1][v2-1]=1;

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

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

printf(" %d ",G[i][j]);

printf("\n");

printf("Enter the source: ");

scanf("%d",&source);

DFS(source-1);

return 0;

}
8.1

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>

#include <stdlib.h>

/* ADJACENCY MATRIX */

int source,V,E,time,visited[20],G[20][20];

void DFS(int i)

int j;

visited[i]=1;

printf(" %d->",i+1);

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

if(G[i][j]==1&&visited[j]==0)

DFS(j);

int main()

int i,j,v1,v2;

printf("\t\t\tGraphs\n");

printf("Enter the no of edges:");

scanf("%d",&E);
printf("Enter the no of vertices:");

scanf("%d",&V);

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

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

G[i][j]=0;

/* creating edges :P */

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

printf("Enter the edges (format: V1 V2) : ");

scanf("%d%d",&v1,&v2);

G[v1-1][v2-1]=1;

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

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

printf(" %d ",G[i][j]);

printf("\n");

printf("Enter the source: ");

scanf("%d",&source);

DFS(source-1);

return 0;

}
8.2

. Implementation of static hash table with Linear Probing.

//Hashing using linear probing : C program

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey;

printf("\nenter a value to insert into hash table\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index] == NULL)

h[index]=key;

break;

if(i == TABLE_SIZE)

printf("\nelement cannot be inserted\n");

}
void search()

int key,index,i,flag=0,hkey;

printf("\nenter search element\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index]==key)

printf("value is found at index %d",index);

break;

if(i == TABLE_SIZE)

printf("\n value is not found\n");

void display()

int i;

printf("\nelements in the hash table are \n");

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

printf("\nat index %d \t value = %d",i,h[i]);

}
main()

int opt,i;

while(1)

printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

9..1

C program to implement graph traversal method using depth first search.

#include<stdio.h>
#include<stdlib.h>

void recdfs(int m[5][5],int n ,int v)

int w;

static int visited[20]={0};

visited[v]=1;

printf("v%d",v+1);

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

if((m[v][w]==1) &&(visited[w]==0))

recdfs(m,n,w);

void main()

int m[5][5]={{0,0,1,1,0},{0,0,1,0,1},{0,1,0,0,0},{0,0,0,0,1},{0,0,0,0,0}};

printf("\n the depth first search traverse is : ");

recdfs(m,5,0);

}
9.2

C program to implement BST to perform following operations on BST- a) Create b) Counting leaf

nodes.

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

typedef struct node


{

int info;

struct node*left,*right;

} NODE;

NODE *createbst(NODE *root,int item)

if(root==NULL)

root=(NODE *)malloc(sizeof(NODE));

root->left=root->right=NULL;

root->info=item;

return root;

else

if(item<root->info)

root->left=createbst(root->left, item);

else

if(item>root->info)

root->right=createbst(root->right,item);

else

printf("Duplicate element is not allowed");

return root;
}

int countleaf(NODE* root)

if(root==NULL)

return 0;

else

if((root->left==NULL)&&(root->right==NULL))

return 1;

else

return (countleaf(root->left)+countleaf(root->right));

int main()

int i,n,choice,item,key;

NODE *temp,*root=NULL;

while(1)

printf("\nBINARY SEARCH TREE");

printf("\n1.Create bst");

printf("\n2.Count leaf nodes of bst");

printf("\n3.Exit");

printf("\nEnter your choice");

scanf("%d",&choice);

switch (choice)
{

case 1:

printf("Enter how many nodes you want to create");

scanf("%d",&n);

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

printf("\nENter the node info");

scanf("%d",&item);

root=createbst(root,item);

break;

case 2:

printf("\nTotal leaf nodes of binary tree = %d ",countleaf(root));

break;

case 3:

exit(0);

default:

printf("Wrong choice entered....");

}
10..1

C program to implement graph traversal method using depth first search.

#include<stdio.h>

#include<stdlib.h>

void recdfs(int m[5][5],int n ,int v)

int w;

static int visited[20]={0};

visited[v]=1;

printf("v%d",v+1);

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

if((m[v][w]==1) &&(visited[w]==0))

recdfs(m,n,w);

void main()

int m[5][5]={{0,0,1,1,0},{0,0,1,0,1},{0,1,0,0,0},{0,0,0,0,1},{0,0,0,0,0}};

printf("\n the depth first search traverse is : ");

recdfs(m,5,0);

}
10.2

. 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 of all vertices of

graph.
#include<stdio.h>

#define MAX 10

int adj[MAX][MAX];

int n;

int main()

int max_edges, i, j, origin, destin, ind;

int graph_type;

printf("\n1. Undirected Graph\n2. Directed Graph\n");

printf("Enter your choice: ");

scanf("%d", &graph_type);

printf("\nEnter number of vertices: ");

scanf("%d", &n);

if(graph_type==1)

max_edges = n*(n-1)/2;

else

max_edges = n*(n-1);

for(i=1; i<=max_edges; i++)

printf("\nEnter edge[%d](-1 -1 to quit): ", i);

scanf("%d %d", &origin, &destin);


if((origin == -1)&&(destin == -1))

break;

if(origin >= n || destin >= n || origin < 0 || destin < 0)

else

printf("\nInvalid vertex!\n");

i--;

adj[origin][destin] = 1;

if(graph_type == 1)

adj[destin][origin] = 1;

printf("\nThe adjacency matrix is:\n");

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

for(j=0; j<=n-1; j++)

printf("%4d", adj[i][j]);

}
printf("\n");

printf("\nIndegree, ");

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

for(j=0, ind=0; j<n; j++)

if(adj[j][i] == 1)

ind++;

printf("\nv%d %5d \n",i+1,ind);

}
11.1

. 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 of all vertices of

graph.

#include<stdio.h>

#define MAX 10

int adj[MAX][MAX];

int n;

int main()

int max_edges, i, j, origin, destin, ind;

int graph_type;

printf("\n1. Undirected Graph\n2. Directed Graph\n");


printf("Enter your choice: ");

scanf("%d", &graph_type);

printf("\nEnter number of vertices: ");

scanf("%d", &n);

if(graph_type==1)

max_edges = n*(n-1)/2;

else

max_edges = n*(n-1);

for(i=1; i<=max_edges; i++)

printf("\nEnter edge[%d](-1 -1 to quit): ", i);

scanf("%d %d", &origin, &destin);

if((origin == -1)&&(destin == -1))

break;

if(origin >= n || destin >= n || origin < 0 || destin < 0)

else

{
printf("\nInvalid vertex!\n");

i--;

adj[origin][destin] = 1;

if(graph_type == 1)

adj[destin][origin] = 1;

printf("\nThe adjacency matrix is:\n");

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

for(j=0; j<=n-1; j++)

printf("%4d", adj[i][j]);

printf("\n");

printf("\nIndegree, ");

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

for(j=0, ind=0; j<n; j++)

if(adj[j][i] == 1)

ind++;
}

printf("\nv%d %5d \n",i+1,ind);

}
11.2

Implement a Binary search tree (BST) library (btree.h) with operations – create,

insert, inorder. Write a menu driven program that performs the above operations.

#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode

int data;

struct bnode *left,*right;

}bnode;

bnode *insert(bnode *,int);

bnode *create();

void inorder(bnode *T);

bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");


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

scanf("%d",&x);

root=insert(root,x);

return(root);

void inorder(bnode *T)

if(T!=NULL)

inorder(T->left);

printf("%d\t",T->data);

inorder(T->right);

bnode *insert(bnode *T,int x)

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;
return(temp);

if(x>T->data)

else

T->right=insert(T->right,x);

return(T);

if(x<T->data)

T->left=insert(T->left,x);

return(T);

return(T);

void main()

bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Inorder");

printf("\n\t4.Exit)");
printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)

case 1: root=create();break;

case 2: printf("\n Enter the Node to be inserted:-->");

scanf("%d",&x);

root=insert(root,x);

break;

case 3: inorder(root);

break;

case 4: exit(0);

}while(ch!=4);

}
12.1

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>

#include <stdlib.h>

/* ADJACENCY MATRIX */

int source,V,E,time,visited[20],G[20][20];

void DFS(int i)

int j;
visited[i]=1;

printf(" %d->",i+1);

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

if(G[i][j]==1&&visited[j]==0)

DFS(j);

int main()

int i,j,v1,v2;

printf("\t\t\tGraphs\n");

printf("Enter the no of edges:");

scanf("%d",&E);

printf("Enter the no of vertices:");

scanf("%d",&V);

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

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

G[i][j]=0;

/* creating edges :P */

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

printf("Enter the edges (format: V1 V2) : ");


scanf("%d%d",&v1,&v2);

G[v1-1][v2-1]=1;

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

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

printf(" %d ",G[i][j]);

printf("\n");

printf("Enter the source: ");

scanf("%d",&source);

DFS(source-1);

return 0;

12.2

Implement a Binary search tree (BST) library (btree.h) with operations –

create, insert, preorder. Write a menu driven program that performs the above

operations

#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode

int data;

struct bnode *left,*right;


}bnode;

bnode *insert(bnode *,int);

bnode *create();

void preorder(bnode *T);

bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");

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

scanf("%d",&x);

root=insert(root,x);

return(root);

void preorder(bnode *T)

if(T!=NULL)

printf("%d\t",T->data);

preorder(T->left);
preorder(T->right);

bnode *insert(bnode *T,int x)

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;

return(temp);

if(x>T->data)

else

T->right=insert(T->right,x);

return(T);

if(x<T->data)

T->left=insert(T->left,x);

return(T);

}
return(T);

void main()

bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Display");

printf("\n\t4.Exit)");

printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)

case 1: root=create();break;

case 2: printf("\n enter the key to thr inserted:-->");

scanf("%d",&x);

root=insert(root,x);

break;

case 3: preorder(root);

break;

case 4: exit(0);

}
}while(ch!=4);

13.2

Write a C program that accepts the vertices and edges of a graph and stores

it as an adjacency matrix. Display the adjacency matrix.

#include<stdio.h>

#include<stdlib.h>

void create(int m[10][10],int n)

int i,j;

char ans;

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

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

m[i][i]=0;

if(i!=j)

printf("\n Is there an edge between %d and %d : ",i+1,j+1);

scanf("%d",&m[i][j]);

void display(int m[10][10],int n)

int i,j;
printf("\n \t Adjacency matrix is : \n");

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

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

printf("%5d",m[i][j]);

printf("\n");

void main()

int m[10][10],n;

printf("\n \t Enter vertices : ");

scanf("%d",&n);

create(m,n);

display(m,n);

}
14.1

Write a C program that accepts the vertices and edges of a graph and stores

it as an adjacency matrix. Display the adjacency matrix.

#include<stdio.h>

#include<stdlib.h>

void create(int m[10][10],int n)

int i,j;

char ans;

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

m[i][i]=0;

if(i!=j)

printf("\n Is there an edge between %d and %d : ",i+1,j+1);

scanf("%d",&m[i][j]);

void display(int m[10][10],int n)

int i,j;

printf("\n \t Adjacency matrix is : \n");

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

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

printf("%5d",m[i][j]);

printf("\n");

void main()

int m[10][10],n;

printf("\n \t Enter vertices : ");


scanf("%d",&n);

create(m,n);

display(m,n);

}
14.2

Write a program which uses binary search tree library and counts the total

nodes and total leaf nodes in the tree. int count Leaf(T) – returns the total

number of leaf nodes from BST.

#include<stdio.h>

#include<stdlib.h>

struct node

struct node *LC,*RC;

int data;

};

struct node *create(struct node *root,int item)

if(root==NULL)

root=(struct node *)malloc(sizeof(struct node));

root->LC=root->RC=NULL;

root->data=item;

return root;

else

{
if(item<root->data)

root->LC=create(root->LC,item);

else if(item>root->data)

root->RC=create(root->RC,item);

else

printf("Invalid item");

return root;

int totalnodes(struct node *root)

if(root==NULL)

return 0;

else

return(1+totalnodes(root->LC)+totalnodes(root->RC));

int count=0;

int leafnodes(struct node* newnode)

if(newnode != NULL)

leafnodes(newnode->LC);

if((newnode->LC == NULL) && (newnode->RC == NULL))

count++;
}

leafnodes(newnode->RC);

return count;

void main()

struct node *root=NULL;

int choice,i,n,item;

printf("1.Create\n");

printf("2.totalnodes\n");

printf("3.countLeaf\n");

printf("4.Exit\n");

while(1)

printf("\nEnter your choice : \n");

scanf("%d",&choice);

switch(choice)

case 1: root=NULL;

printf("Enter total nodes : ");

scanf("%d",&n);

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

printf("Enter %d data for nodes : ",i);


scanf("%d",&item);

root=create(root,item);

break;

case 2: printf("Number of nodes in a tree is %d",totalnodes(root));

totalnodes(root);

break;

case 3: printf("Number of leaf nodes in a tree are %d",leafnodes(root));

leafnodes(root);

break;

case 4: exit(0);

}
15.1

Implementation of static hash table with Linear Probing.

//Hashing using linear probing : C program

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey;

printf("\nenter a value to insert into hash table\n");

scanf("%d",&key);
hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index] == NULL)

h[index]=key;

break;

if(i == TABLE_SIZE)

printf("\nelement cannot be inserted\n");

void search()

int key,index,i,flag=0,hkey;

printf("\nenter search element\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index]==key)

printf("value is found at index %d",index);


break;

if(i == TABLE_SIZE)

printf("\n value is not found\n");

void display()

int i;

printf("\nelements in the hash table are \n");

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

printf("\nat index %d \t value = %d",i,h[i]);

main()

int opt,i;

while(1)

printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;
case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

15.2

Write a program which uses binary search tree library and counts the total

nodes and total leaf nodes in the tree. int count Leaf(T) – returns the total

number of leaf nodes from BST.

#include<stdio.h>

#include<stdlib.h>

struct node

struct node *LC,*RC;

int data;

};

struct node *create(struct node *root,int item)

if(root==NULL)

{
root=(struct node *)malloc(sizeof(struct node));

root->LC=root->RC=NULL;

root->data=item;

return root;

else

if(item<root->data)

root->LC=create(root->LC,item);

else if(item>root->data)

root->RC=create(root->RC,item);

else

printf("Invalid item");

return root;

int totalnodes(struct node *root)

if(root==NULL)

return 0;

else

return(1+totalnodes(root->LC)+totalnodes(root->RC));

int count=0;

int leafnodes(struct node* newnode)


{

if(newnode != NULL)

leafnodes(newnode->LC);

if((newnode->LC == NULL) && (newnode->RC == NULL))

count++;

leafnodes(newnode->RC);

return count;

void main()

struct node *root=NULL;

int choice,i,n,item;

printf("1.Create\n");

printf("2.totalnodes\n");

printf("3.countLeaf\n");

printf("4.Exit\n");

while(1)

printf("\nEnter your choice : \n");

scanf("%d",&choice);

switch(choice)
{

case 1: root=NULL;

printf("Enter total nodes : ");

scanf("%d",&n);

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

printf("Enter %d data for nodes : ",i);

scanf("%d",&item);

root=create(root,item);

break;

case 2: printf("Number of nodes in a tree is %d",totalnodes(root));

totalnodes(root);

break;

case 3: printf("Number of leaf nodes in a tree are %d",leafnodes(root));

leafnodes(root);

break;

case 4: exit(0);

}
16.1

. C program to implement graph traversal method using breadth first search.

#include<stdio.h>

#include<stdlib.h>
#define MAXSIZE 20

typedef struct

int data[MAXSIZE];

int front,rear;

}QUEUE;

void initq(QUEUE *pq)

pq->front=pq->rear=-1;

void addq(QUEUE *pq,int n)

pq->data[++pq->rear]=n;

int removeq(QUEUE *pq)

return pq->data[++pq->front];

int isempty(QUEUE *pq)

return(pq->front==pq->rear);

void createlist(int m[10][10],int n)

int i,j;
printf("\n*PRESS 1 FOR YES(edge present) & 0 FOR NO(edge

not present)*\n");

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

// list[i]=NULL;

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

if(i!=j)

printf("\nIs there edge between %d & %d: ",i+1,j+1);

scanf("%d",&m[i][j]);

void bfs(int a[10][10],int n)

int v=0;

int visited[20]={0};

QUEUE q;

initq(&q);

printf("\nThe breadth first traversal is:\n");

visited[v]=1;

addq(&q,v);

while(!isempty(&q))
{

v=removeq(&q);

printf("v%d\t",v+1);

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

if((a[v][w]==1)&&(visited[w]==0))

addq(&q,w);

visited[w]=1;

int main()

int a[10][10],n;

printf("\nEnter the no of vertex:");

scanf("%d",&n);

createlist(a,n);

bfs(a,n);

16.2

Implement a Binary search tree (BST) library (btree.h) with operations –

create, insert, preorder. Write a menu driven program that performs the above

operations
#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode

int data;

struct bnode *left,*right;

}bnode;

bnode *insert(bnode *,int);

bnode *create();

void preorder(bnode *T);

bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");

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

scanf("%d",&x);

root=insert(root,x);

return(root);
}

void preorder(bnode *T)

if(T!=NULL)

printf("%d\t",T->data);

preorder(T->left);

preorder(T->right);

bnode *insert(bnode *T,int x)

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;

return(temp);

if(x>T->data)

else
T->right=insert(T->right,x);

return(T);

if(x<T->data)

T->left=insert(T->left,x);

return(T);

return(T);

void main()

bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Display");

printf("\n\t4.Exit)");

printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)

case 1: root=create();break;

case 2: printf("\n enter the key to thr inserted:-->");


scanf("%d",&x);

root=insert(root,x);

break;

case 3: preorder(root);

break;

case 4: exit(0);

}while(ch!=4);

18.1

Write a program which uses binary search tree library and counts the total

nodes and total leaf nodes in the tree. int count Leaf(T) – returns the total

number of leaf nodes from BST.

#include<stdio.h>

#include<stdlib.h>

struct node

struct node *LC,*RC;

int data;

};

struct node *create(struct node *root,int item)

if(root==NULL)

root=(struct node *)malloc(sizeof(struct node));


root->LC=root->RC=NULL;

root->data=item;

return root;

else

if(item<root->data)

root->LC=create(root->LC,item);

else if(item>root->data)

root->RC=create(root->RC,item);

else

printf("Invalid item");

return root;

int totalnodes(struct node *root)

if(root==NULL)

return 0;

else

return(1+totalnodes(root->LC)+totalnodes(root->RC));

int count=0;

int leafnodes(struct node* newnode)

{
if(newnode != NULL)

leafnodes(newnode->LC);

if((newnode->LC == NULL) && (newnode->RC == NULL))

count++;

leafnodes(newnode->RC);

return count;

void main()

struct node *root=NULL;

int choice,i,n,item;

printf("1.Create\n");

printf("2.totalnodes\n");

printf("3.countLeaf\n");

printf("4.Exit\n");

while(1)

printf("\nEnter your choice : \n");

scanf("%d",&choice);

switch(choice)

{
case 1: root=NULL;

printf("Enter total nodes : ");

scanf("%d",&n);

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

printf("Enter %d data for nodes : ",i);

scanf("%d",&item);

root=create(root,item);

break;

case 2: printf("Number of nodes in a tree is %d",totalnodes(root));

totalnodes(root);

break;

case 3: printf("Number of leaf nodes in a tree are %d",leafnodes(root));

leafnodes(root);

break;

case 4: exit(0);

}
18.2

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 of all vertices of

graph.

#include<stdio.h>

#define MAX 10
int adj[MAX][MAX];

int n;

int main()

int max_edges, i, j, origin, destin, ind;

int graph_type;

printf("\n1. Undirected Graph\n2. Directed Graph\n");

printf("Enter your choice: ");

scanf("%d", &graph_type);

printf("\nEnter number of vertices: ");

scanf("%d", &n);

if(graph_type==1)

max_edges = n*(n-1)/2;

else

max_edges = n*(n-1);

for(i=1; i<=max_edges; i++)

printf("\nEnter edge[%d](-1 -1 to quit): ", i);

scanf("%d %d", &origin, &destin);

if((origin == -1)&&(destin == -1))

{
break;

if(origin >= n || destin >= n || origin < 0 || destin < 0)

else

printf("\nInvalid vertex!\n");

i--;

adj[origin][destin] = 1;

if(graph_type == 1)

adj[destin][origin] = 1;

printf("\nThe adjacency matrix is:\n");

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

for(j=0; j<=n-1; j++)

printf("%4d", adj[i][j]);

printf("\n");

}
printf("\nIndegree, ");

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

for(j=0, ind=0; j<n; j++)

if(adj[j][i] == 1)

ind++;

printf("\nv%d %5d \n",i+1,ind);

19.1

. Write a C program that accepts the vertices and edges of a graph. Create

adjacency list

#include<stdio.h>

#include<stdlib.h>

typedef struct node

int vertex;

struct node *next;

} NODE;

NODE *head[10];

void createlist(int n)

{
int i, j, x;

NODE *temp, *newnode;

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

head[i]=NULL;

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

printf("is there any edge between %d and %d (1/0): ", i+1, j+1);

scanf("%d", &x);

if (x==1)

newnode=(NODE*)malloc(sizeof(NODE));

newnode->vertex=j+1;

newnode->next=NULL;

if (head[i]==NULL)

head[i]=temp=newnode;

else

temp->next=newnode;

temp=newnode;
void displaylist(int n)

NODE *temp;

printf("\nThe Adjacency list is: \n");

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

printf("\nv%d -> ", i+1);

temp=head[i];

while(temp)

printf("v%d -> ", temp->vertex);

temp=temp->next;

printf("NULL");

printf("\n");

void main()

int n;

printf("Enter how many vertices: ");

scanf("%d", &n);

createlist(n);

displaylist(n);

}
19.2
. Implement a Binary search tree (BST) library (btree.h) with operations –

create, insert, preorder. Write a menu driven program that performs the above

operations

#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode

int data;

struct bnode *left,*right;

}bnode;

bnode *insert(bnode *,int);

bnode *create();

void preorder(bnode *T);

bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");

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

scanf("%d",&x);
root=insert(root,x);

return(root);

void preorder(bnode *T)

if(T!=NULL)

printf("%d\t",T->data);

preorder(T->left);

preorder(T->right);

bnode *insert(bnode *T,int x)

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;

return(temp);

if(x>T->data)
{

else

T->right=insert(T->right,x);

return(T);

if(x<T->data)

T->left=insert(T->left,x);

return(T);

return(T);

void main()

bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Display");

printf("\n\t4.Exit)");

printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)
{

case 1: root=create();break;

case 2: printf("\n enter the key to thr inserted:-->");

scanf("%d",&x);

root=insert(root,x);

break;

case 3: preorder(root);

break;

case 4: exit(0);

}while(ch!=4);

}
20.1

Implement a Binary search tree (BST) library (btree.h) with operations – create,

insert, inorder. Write a menu driven program that performs the above operations.

#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode

int data;

struct bnode *left,*right;

}bnode;

bnode *insert(bnode *,int);

bnode *create();

void inorder(bnode *T);


bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");

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

scanf("%d",&x);

root=insert(root,x);

return(root);

void inorder(bnode *T)

if(T!=NULL)

inorder(T->left);

printf("%d\t",T->data);

inorder(T->right);

bnode *insert(bnode *T,int x)


{

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;

return(temp);

if(x>T->data)

else

T->right=insert(T->right,x);

return(T);

if(x<T->data)

T->left=insert(T->left,x);

return(T);

return(T);

void main()

{
bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Inorder");

printf("\n\t4.Exit)");

printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)

case 1: root=create();break;

case 2: printf("\n Enter the Node to be inserted:-->");

scanf("%d",&x);

root=insert(root,x);

break;

case 3: inorder(root);

break;

case 4: exit(0);

}while(ch!=4);

20.2

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 of all vertices of

graph.

#include<stdio.h>

#define MAX 10

int adj[MAX][MAX];

int n;

int main()

int max_edges, i, j, origin, destin, ind;

int graph_type;

printf("\n1. Undirected Graph\n2. Directed Graph\n");

printf("Enter your choice: ");

scanf("%d", &graph_type);

printf("\nEnter number of vertices: ");

scanf("%d", &n);

if(graph_type==1)

max_edges = n*(n-1)/2;

else

max_edges = n*(n-1);

for(i=1; i<=max_edges; i++)

{
printf("\nEnter edge[%d](-1 -1 to quit): ", i);

scanf("%d %d", &origin, &destin);

if((origin == -1)&&(destin == -1))

break;

if(origin >= n || destin >= n || origin < 0 || destin < 0)

else

printf("\nInvalid vertex!\n");

i--;

adj[origin][destin] = 1;

if(graph_type == 1)

adj[destin][origin] = 1;

printf("\nThe adjacency matrix is:\n");

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

for(j=0; j<=n-1; j++)

{
printf("%4d", adj[i][j]);

printf("\n");

printf("\nIndegree, ");

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

for(j=0, ind=0; j<n; j++)

if(adj[j][i] == 1)

ind++;

printf("\nv%d %5d \n",i+1,ind);

}
22.1

#include <stdio.h>

#include <stdlib.h>

#include "btree.h"

// Function to create a new node with the given data

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

if (!newNode) {

printf("Memory allocation error.\n");

return NULL;
}

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

// Function to insert a new node in the binary search tree

Node* insertNode(Node* root, int data) {

if (root == NULL) {

return createNode(data);

if (data < root->data) {

root->left = insertNode(root->left, data);

} else {

root->right = insertNode(root->right, data);

return root;

// Function to perform a preorder traversal

void preorder(Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preorder(root->left);

preorder(root->right);
}

// Function to perform a postorder traversal

void postorder(Node* root) {

if (root != NULL) {

postorder(root->left);

postorder(root->right);

printf("%d ", root->data);

#include <stdio.h>

#include <stdlib.h>

#include "btree.h"

// Function to display the menu and get the user's choice

int displayMenu() {

int choice;

printf("\nBinary Search Tree Operations\n");

printf("1. Create and Insert Node\n");

printf("2. Preorder Traversal\n");

printf("3. Postorder Traversal\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);
return choice;

int main() {

Node* root = NULL;

int choice, data;

while (1) {

choice = displayMenu();

switch (choice) {

case 1:

printf("Enter data to insert: ");

scanf("%d", &data);

root = insertNode(root, data);

break;

case 2:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 3:

printf("Postorder Traversal: ");


postorder(root);

printf("\n");

break;

case 4:

printf("Exiting...\n");

exit(0);

break;

default:

printf("Invalid choice! Please try again.\n");

return 0;

}
22.2

. Implementation of static hash table with Linear Probing.

//Hashing using linear probing : C program

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey;

printf("\nenter a value to insert into hash table\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index] == NULL)

h[index]=key;

break;

if(i == TABLE_SIZE)

printf("\nelement cannot be inserted\n");


}

void search()

int key,index,i,flag=0,hkey;

printf("\nenter search element\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index]==key)

printf("value is found at index %d",index);

break;

if(i == TABLE_SIZE)

printf("\n value is not found\n");

void display()

int i;

printf("\nelements in the hash table are \n");

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

printf("\nat index %d \t value = %d",i,h[i]);


}

main()

int opt,i;

while(1)

printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

}
23..1
Write a C program that accepts the vertices and edges of a graph. Create

adjacency list

#include<stdio.h>

#include<stdlib.h>

typedef struct node

int vertex;

struct node *next;

} NODE;

NODE *head[10];

void createlist(int n)

int i, j, x;

NODE *temp, *newnode;

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

head[i]=NULL;

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

printf("is there any edge between %d and %d (1/0): ", i+1, j+1);

scanf("%d", &x);

if (x==1)

newnode=(NODE*)malloc(sizeof(NODE));

newnode->vertex=j+1;
newnode->next=NULL;

if (head[i]==NULL)

head[i]=temp=newnode;

else

temp->next=newnode;

temp=newnode;

void displaylist(int n)

NODE *temp;

printf("\nThe Adjacency list is: \n");

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

printf("\nv%d -> ", i+1);

temp=head[i];

while(temp)

printf("v%d -> ", temp->vertex);

temp=temp->next;

}
printf("NULL");

printf("\n");

void main()

int n;

printf("Enter how many vertices: ");

scanf("%d", &n);

createlist(n);

displaylist(n);

23.2

C program to implement BST to perform following operations on BST- a) Create b) Counting leaf

nodes.

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

typedef struct node

int info;

struct node*left,*right;

} NODE;

NODE *createbst(NODE *root,int item)

{
if(root==NULL)

root=(NODE *)malloc(sizeof(NODE));

root->left=root->right=NULL;

root->info=item;

return root;

else

if(item<root->info)

root->left=createbst(root->left, item);

else

if(item>root->info)

root->right=createbst(root->right,item);

else

printf("Duplicate element is not allowed");

return root;

int countleaf(NODE* root)

if(root==NULL)

return 0;
else

if((root->left==NULL)&&(root->right==NULL))

return 1;

else

return (countleaf(root->left)+countleaf(root->right));

int main()

int i,n,choice,item,key;

NODE *temp,*root=NULL;

while(1)

printf("\nBINARY SEARCH TREE");

printf("\n1.Create bst");

printf("\n2.Count leaf nodes of bst");

printf("\n3.Exit");

printf("\nEnter your choice");

scanf("%d",&choice);

switch (choice)

case 1:

printf("Enter how many nodes you want to create");

scanf("%d",&n);

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

{
printf("\nENter the node info");

scanf("%d",&item);

root=createbst(root,item);

break;

case 2:

printf("\nTotal leaf nodes of binary tree = %d ",countleaf(root));

break;

case 3:

exit(0);

default:

printf("Wrong choice entered....");

}
24.1

#include <stdio.h>

#include <stdlib.h>

#include "btree.h"

// Function to create a new node with the given data

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

if (!newNode) {

printf("Memory allocation error.\n");

return NULL;

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

// Function to insert a new node in the binary search tree

Node* insertNode(Node* root, int data) {

if (root == NULL) {

return createNode(data);

if (data < root->data) {

root->left = insertNode(root->left, data);

} else {
root->right = insertNode(root->right, data);

return root;

// Function to perform a preorder traversal

void preorder(Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preorder(root->left);

preorder(root->right);

// Function to perform a postorder traversal

void postorder(Node* root) {

if (root != NULL) {

postorder(root->left);

postorder(root->right);

printf("%d ", root->data);

#include <stdio.h>

#include <stdlib.h>
#include "btree.h"

// Function to display the menu and get the user's choice

int displayMenu() {

int choice;

printf("\nBinary Search Tree Operations\n");

printf("1. Create and Insert Node\n");

printf("2. Preorder Traversal\n");

printf("3. Postorder Traversal\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

return choice;

int main() {

Node* root = NULL;

int choice, data;

while (1) {

choice = displayMenu();

switch (choice) {

case 1:

printf("Enter data to insert: ");


scanf("%d", &data);

root = insertNode(root, data);

break;

case 2:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 3:

printf("Postorder Traversal: ");

postorder(root);

printf("\n");

break;

case 4:

printf("Exiting...\n");

exit(0);

break;

default:

printf("Invalid choice! Please try again.\n");

}
return 0;

}
24.2

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>

struct q

int data[20];

int front,rear;

}q1;

void add(int n)

q1.rear++;

q1.data[q1.rear]=n;

int del()

q1.front++;

return q1.data[q1.front];

void initq()

q1.front=q1.rear=-1;
}

int emptyq()

return (q1.rear==q1.front);

void create(int m[10][10],int n)

int i,j;

char ans;

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

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

m[i][i]=0;

if(i!=j)

printf("\n Is there an edge between %d and %d : ",i+1,j+1);

scanf("%d",&m[i][j]);

void display(int m[10][10],int n)

int i,j;

printf("\n \t Adjacency matrix is : \n");

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

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

printf("%5d",m[i][j]);

printf("\n");

void bfs(int m[10][10],int n)

int i,j,v,w;

int visited[20];

initq();

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

visited[i]=0;

printf("\n \t The Bfs is: \n");

v=0;

visited[v]=1;

add(v);

while(! emptyq())

v=del();

printf("\n v%d ",v+1);

printf("\n");

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

if((m[v][w]==1) &&(visited[w]==0))

{
add(w);

visited[w]=1;

void main()

int m[10][10],n;

printf("\n \t Enter vertices : ");

scanf("%d",&n);

create(m,n);

display(m,n);

bfs(m,n);

25.1

Implement a Binary search tree (BST) library (btree.h) with operations –

create, insert, preorder. Write a menu driven program that performs the above

operations

#include<stdio.h>

#include<stdlib.h>

//#include"btree14.h"

typedef struct bnode

int data;

struct bnode *left,*right;


}bnode;

bnode *insert(bnode *,int);

bnode *create();

void preorder(bnode *T);

bnode *create()

int n,x,i;

bnode *root;

root=NULL;

printf("\nEnter no. of nodes :");

scanf("%d",&n);

printf("\nEnter tree values :");

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

scanf("%d",&x);

root=insert(root,x);

return(root);

void preorder(bnode *T)

if(T!=NULL)

printf("%d\t",T->data);

preorder(T->left);
preorder(T->right);

bnode *insert(bnode *T,int x)

bnode *temp;

if(T==NULL)

temp=(bnode*)malloc(sizeof(bnode));

temp->data=x;

temp->left=NULL;

temp->right=NULL;

return(temp);

if(x>T->data)

else

T->right=insert(T->right,x);

return(T);

if(x<T->data)

T->left=insert(T->left,x);

return(T);

}
return(T);

void main()

bnode *root=NULL,*p;

int x,ch;

do

printf("\n\t1.create");

printf("\n\t2.insert");

printf("\n\t3.Display");

printf("\n\t4.Exit)");

printf("\n enter your choice:-->");

scanf("%d",&ch);

switch(ch)

case 1: root=create();break;

case 2: printf("\n enter the key to thr inserted:-->");

scanf("%d",&x);

root=insert(root,x);

break;

case 3: preorder(root);

break;

case 4: exit(0);

}
}while(ch!=4);

}
25.2

. Implementation of static hash table with Linear Probing.

//Hashing using linear probing : C program

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey;

printf("\nenter a value to insert into hash table\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index] == NULL)

h[index]=key;

break;

if(i == TABLE_SIZE)

printf("\nelement cannot be inserted\n");

}
void search()

int key,index,i,flag=0,hkey;

printf("\nenter search element\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

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

index=(hkey+i)%TABLE_SIZE;

if(h[index]==key)

printf("value is found at index %d",index);

break;

if(i == TABLE_SIZE)

printf("\n value is not found\n");

void display()

int i;

printf("\nelements in the hash table are \n");

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

printf("\nat index %d \t value = %d",i,h[i]);

}
main()

int opt,i;

while(1)

printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

You might also like