[go: up one dir, main page]

0% found this document useful (0 votes)
4 views177 pages

1 DSA Slip Answer

The document contains multiple C programming tasks, including the implementation of Binary Search Trees (BST) with operations like insert, search, and traversal, as well as graph-related tasks such as creating an adjacency matrix and implementing Dijkstra's algorithm. It also includes the implementation of a static hash table with linear probing and depth-first search for graph traversal. Each task is accompanied by code snippets demonstrating the required functionality.

Uploaded by

sawantamruta39
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)
4 views177 pages

1 DSA Slip Answer

The document contains multiple C programming tasks, including the implementation of Binary Search Trees (BST) with operations like insert, search, and traversal, as well as graph-related tasks such as creating an adjacency matrix and implementing Dijkstra's algorithm. It also includes the implementation of a static hash table with linear probing and depth-first search for graph traversal. Each task is accompanied by code snippets demonstrating the required functionality.

Uploaded by

sawantamruta39
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/ 177

Slip 1

Q.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>

// Structure for a BST node

typedef struct Node {

int data;

struct Node* left;

struct Node* right;

} Node;

// Function to create a new node

Node* createNode(int data) {

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

if (!newNode) {

printf("Memory allocation failed!\n");

return NULL;

newNode->data = data;

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

return newNode;

// Function to insert a node into BST

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


if (root == NULL) {

return createNode(data);

if (data < root->data) {

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

} else if (data > root->data) {

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

return root;

// Function for in-order traversal (Left -> Root -> Right)

void inorder(Node* root) {

if (root != NULL) {

inorder(root->left);

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

inorder(root->right);

// Function to free memory allocated to the tree

void freeTree(Node* root) {

if (root != NULL) {

freeTree(root->left);

freeTree(root->right);

free(root);

}
}

int main() {

Node* root = NULL;

int choice, value;

while (1) {

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

printf("1. Insert\n");

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

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Inorder Traversal: ");

inorder(root);

printf("\n");

break;

case 3:

freeTree(root);

printf("Exiting program...\n");
return 0;

default:

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

return 0;

Q.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. Implement functions to print indegree
of all vertices of graph.

->

#include <stdio.h>

#define MAX 100

void createAdjMatrix(int adj[MAX][MAX], int vertices, int edges);

void displayAdjMatrix(int adj[MAX][MAX], int vertices);

void printIndegree(int adj[MAX][MAX], int vertices);

int main() {

int vertices, edges;

int adj[MAX][MAX] = {0};

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);
createAdjMatrix(adj, vertices, edges);

printf("\nAdjacency Matrix:\n");

displayAdjMatrix(adj, vertices);

printf("\nIn-degree of each vertex:\n");

printIndegree(adj, vertices);

return 0;

void createAdjMatrix(int adj[MAX][MAX], int vertices, int edges) {

int i, src, dest;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1; // Directed graph

void displayAdjMatrix(int adj[MAX][MAX], int vertices) {

int i, j;

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

for (j = 0; j < vertices; j++) {

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

printf("\n");
}

void printIndegree(int adj[MAX][MAX], int vertices) {

int i, j, indegree[MAX] = {0};

for (j = 0; j < vertices; j++) {

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

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

indegree[j]++;

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

printf("Vertex %d: %d\n", i, indegree[i]);

}
Slip 2
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations - create,
search, preorder. Write a menu driven program that performs the above operations.

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

Node* search(Node* root, int key);

void preorder(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Enter value to search: ");

scanf("%d", &value);

if (search(root, value))

printf("%d found in the BST.\n", value);

else

printf("%d not found in the BST.\n", value);

break;

case 3:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;
}

void menu() {

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

printf("1. Insert\n");

printf("2. Search\n");

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

printf("4. Exit\n");

// Function to create and insert a node into BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function to search a key in BST

Node* search(Node* root, int key) {

if (root == NULL || root->data == key)


return root;

if (key < root->data)

return search(root->left, key);

return search(root->right, key);

// Function for preorder traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

Q.2) Implementation of Dijkstra's shortest path algorithm for finding Shortest Path from
a given source vertex using adjacency cost matrix.

->

#include <stdio.h>

#include <limits.h>

#define MAX 100

#define INF INT_MAX

void dijkstra(int graph[MAX][MAX], int vertices, int src);

int minDistance(int dist[], int visited[], int vertices);

int main() {
int vertices, i, j;

int graph[MAX][MAX];

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter adjacency cost matrix (Enter 99999 for no direct path):\n");

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

for (j = 0; j < vertices; j++) {

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

if (graph[i][j] == 99999) {

graph[i][j] = INF;

int src;

printf("Enter source vertex: ");

scanf("%d", &src);

dijkstra(graph, vertices, src);

return 0;

void dijkstra(int graph[MAX][MAX], int vertices, int src) {

int dist[MAX], visited[MAX];


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

dist[i] = INF;

visited[i] = 0;

dist[src] = 0;

for (int count = 0; count < vertices - 1; count++) {

int u = minDistance(dist, visited, vertices);

visited[u] = 1;

for (int v = 0; v < vertices; v++) {

if (!visited[v] && graph[u][v] != INF && dist[u] != INF && dist[u] + graph[u][v] <
dist[v]) {

dist[v] = dist[u] + graph[u][v];

printf("\nVertex \t Shortest Distance from Source (%d)\n", src);

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

if (dist[i] == INF)

printf("%d \t INF\n", i);

else

printf("%d \t %d\n", i, dist[i]);

}
int minDistance(int dist[], int visited[], int vertices) {

int min = INF, min_index;

for (int v = 0; v < vertices; v++) {

if (!visited[v] && dist[v] <= min) {

min = dist[v], min_index = v;

return min_index;

}
Slip 3
Q1) Implementation of static hash table with Linear Probing.

->

#include <stdio.h>

#define SIZE 10

#define EMPTY -1

#define DELETED -2

int hashTable[SIZE];

void initHashTable() {

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

hashTable[i] = EMPTY;

int hashFunction(int key) {

return key % SIZE;

void insert(int key) {

int index = hashFunction(key);

int originalIndex = index;

while (hashTable[index] != EMPTY && hashTable[index] != DELETED) {

index = (index + 1) % SIZE;

if (index == originalIndex) {

printf("Hash table is full!\n");


return;

hashTable[index] = key;

printf("Inserted %d at index %d\n", key, index);

int search(int key) {

int index = hashFunction(key);

int originalIndex = index;

while (hashTable[index] != EMPTY) {

if (hashTable[index] == key) {

return index;

index = (index + 1) % SIZE;

if (index == originalIndex) {

break;

return -1;

void display() {

printf("\nHash Table:\n");

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

if (hashTable[i] == EMPTY) {

printf("[%d] : EMPTY\n", i);


} else if (hashTable[i] == DELETED) {

printf("[%d] : DELETED\n", i);

} else {

printf("[%d] : %d\n", i, hashTable[i]);

int main() {

int choice, key;

initHashTable();

do {

printf("\nHash Table Operations:\n");

printf("1. Insert\n");

printf("2. Search\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter key to insert: ");

scanf("%d", &key);

insert(key);

break;

case 2:
printf("Enter key to search: ");

scanf("%d", &key);

int index = search(key);

if (index != -1)

printf("Key %d found at index %d\n", key, index);

else

printf("Key %d not found!\n", key);

break;

case 3:

display();

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;

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

->

#include <stdio.h>

#define MAX 100

int adj[MAX][MAX];
int visited[MAX];

void createGraph(int vertices, int edges);

void DFS(int vertex, int vertices);

int main() {

int vertices, edges, startVertex;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

printf("Enter the starting vertex for DFS: ");

scanf("%d", &startVertex);

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

visited[i] = 0;

printf("DFS Traversal: ");

DFS(startVertex, vertices);

printf("\n");

return 0;
}

void createGraph(int vertices, int edges) {

int i, src, dest;

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // For undirected graph

void DFS(int vertex, int vertices) {

printf("%d ", vertex);

visited[vertex] = 1;

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

if (adj[vertex][i] == 1 && !visited[i]) {

DFS(i, vertices);

}
}
Slip 4
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations-create,
search, preorder. Write a menu driven program that performs the above operations.

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

Node* search(Node* root, int key);

void preorder(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Enter value to search: ");

scanf("%d", &value);

if (search(root, value))

printf("%d found in the BST.\n", value);

else

printf("%d not found in the BST.\n", value);

break;

case 3:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;
}

void menu() {

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

printf("1. Insert\n");

printf("2. Search\n");

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

printf("4. Exit\n");

// Function to create and insert a node into BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function to search a key in BST

Node* search(Node* root, int key) {

if (root == NULL || root->data == key)


return root;

if (key < root->data)

return search(root->left, key);

return search(root->right, key);

// Function for preorder traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

Q.2) Write C Program that accept the vertices and edges of a graph and store it is an
adjacency Matrix

->

#include <stdio.h>

#define MAX 100

void createGraph(int adj[MAX][MAX], int vertices, int edges);

void displayGraph(int adj[MAX][MAX], int vertices);

int main() {

int vertices, edges;

int adj[MAX][MAX] = {0};


printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(adj, vertices, edges);

displayGraph(adj, vertices);

return 0;

void createGraph(int adj[MAX][MAX], int vertices, int edges) {

int i, src, dest;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // For undirected graph

void displayGraph(int adj[MAX][MAX], int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {


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

printf("\n");

}
Slip 5
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations-create,
insert, postorder, Write a menu driven program that performs the above operations

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

void postorder(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Postorder Traversal: ");

postorder(root);

printf("\n");

break;

case 3:

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

break;

default:

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

} while (choice != 3);

return 0;

void menu() {

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

printf("1. Insert\n");

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

printf("3. Exit\n");

}
// Function to create and insert a node into BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function for postorder traversal

void postorder(Node* root) {

if (root) {

postorder(root->left);

postorder(root->right);

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

Q.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>

#define MAX 100

int adj[MAX][MAX];

int visited[MAX];

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

void BFS(int startVertex, int vertices);

int main() {

int vertices, edges, startVertex;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayGraph(vertices);

printf("Enter the starting vertex for BFS: ");

scanf("%d", &startVertex);

printf("BFS Traversal: ");


BFS(startVertex, vertices);

printf("\n");

return 0;

void createGraph(int vertices, int edges) {

int i, src, dest;

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // For undirected graph

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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


}

printf("\n");

void BFS(int startVertex, int vertices) {

int queue[MAX], front = 0, rear = 0;

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

visited[i] = 0;

visited[startVertex] = 1;

queue[rear++] = startVertex;

while (front < rear) {

int currentVertex = queue[front++];

printf("%d ", currentVertex);

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

if (adj[currentVertex][i] == 1 && !visited[i]) {

queue[rear++] = i;

visited[i] = 1;

}
Slip 6
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations create,
preorder and postorder. Write a menu driven program that performs the above
operations.

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

void preorder(Node* root);

void postorder(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

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 program...\n");

break;

default:

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

} while (choice != 4);

return 0;

}
void menu() {

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

printf("1. Insert\n");

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

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

printf("4. Exit\n");

// Function to create and insert a node into BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function for preorder traversal

void preorder(Node* root) {

if (root) {

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

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

// Function for postorder traversal

void postorder(Node* root) {

if (root) {

postorder(root->left);

postorder(root->right);

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

Output

Binary Search Tree Operations:

1. Insert

2. Preorder Traversal

3. Postorder Traversal

4. Exit

Enter your choice: 1

Enter value to insert: 50

Enter your choice: 1

Enter value to insert: 30

Enter your choice: 1

Enter value to insert: 70


Enter your choice: 1

Enter value to insert: 20

Enter your choice: 1

Enter value to insert: 40

Enter your choice: 1

Enter value to insert: 60

Enter your choice: 1

Enter value to insert: 80

Enter your choice: 2

Preorder Traversal: 50 30 20 40 70 60 80

Enter your choice: 3

Postorder Traversal: 20 40 30 60 80 70 50

Enter your choice: 4

Exiting program...

Q.2) 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>
// Structure for an adjacency list node

typedef struct Node {

int vertex;

struct Node* next;

} Node;

// Structure for an adjacency list

typedef struct Graph {

int numVertices;

Node** adjLists;

} Graph;

// Function prototypes

Graph* createGraph(int vertices);

void addEdge(Graph* graph, int src, int dest);

void displayGraph(Graph* graph);

Node* createNode(int vertex);

int main() {

int vertices, edges, src, dest;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

Graph* graph = createGraph(vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);
printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

addEdge(graph, src, dest);

displayGraph(graph);

return 0;

// Function to create a graph

Graph* createGraph(int vertices) {

Graph* graph = (Graph*)malloc(sizeof(Graph));

graph->numVertices = vertices;

graph->adjLists = (Node**)malloc(vertices * sizeof(Node*));

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

graph->adjLists[i] = NULL;

return graph;

// Function to create a new node

Node* createNode(int vertex) {

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


newNode->vertex = vertex;

newNode->next = NULL;

return newNode;

// Function to add an edge to the graph

void addEdge(Graph* graph, int src, int dest) {

Node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

newNode = createNode(src); // For undirected graph

newNode->next = graph->adjLists[dest];

graph->adjLists[dest] = newNode;

// Function to display the adjacency list

void displayGraph(Graph* graph) {

printf("\nAdjacency List:\n");

for (int i = 0; i < graph->numVertices; i++) {

Node* temp = graph->adjLists[i];

printf("Vertex %d: ", i);

while (temp) {

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

temp = temp->next;

printf("\n");

}
}

Output

Enter number of vertices: 5

Enter number of edges: 6

Enter edges (source destination):

01

04

12

13

14

34

Adjacency List:

Vertex 0: -> 4 -> 1

Vertex 1: -> 4 -> 3 -> 2 -> 0

Vertex 2: -> 1

Vertex 3: -> 4 -> 1

Vertex 4: -> 3 -> 1 -> 0


Slip 7
Q.1) Implement a Binary search tree (BST) library (btree.h) with operations-create,
search, preorder. Write a menu driven program that performs the above operations

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

Node* search(Node* root, int key);

void preorder(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value, key;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Enter value to search: ");

scanf("%d", &key);

if (search(root, key) != NULL)

printf("Value %d found in the BST.\n", key);

else

printf("Value %d not found in the BST.\n", key);

break;

case 3:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 4:

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

break;

default:

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

} while (choice != 4);


return 0;

void menu() {

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

printf("1. Insert\n");

printf("2. Search\n");

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

printf("4. Exit\n");

// Function to insert a node in BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function to search for a node in BST

Node* search(Node* root, int key) {


if (root == NULL || root->data == key)

return root;

if (key < root->data)

return search(root->left, key);

return search(root->right, key);

// Function for preorder traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

Q.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. [15]

->

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int adj[MAX][MAX];

int visited[MAX];

void createGraph(int vertices, int edges);


void displayGraph(int vertices);

void DFS(int vertex, int vertices);

int main() {

int vertices, edges, startVertex;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayGraph(vertices);

printf("Enter the starting vertex for DFS: ");

scanf("%d", &startVertex);

printf("DFS Traversal: ");

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

visited[i] = 0;

DFS(startVertex, vertices);

printf("\n");

return 0;

}
void createGraph(int vertices, int edges) {

int i, src, dest;

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // For undirected graph

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

void DFS(int vertex, int vertices) {


printf("%d ", vertex);

visited[vertex] = 1;

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

if (adj[vertex][i] == 1 && !visited[i]) {

DFS(i, vertices);

}
Slip 8
Q.1) C program to implement graph as adjacency matrix.

->

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int adj[MAX][MAX];

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

int main() {

int vertices, edges;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayGraph(vertices);

return 0;

void createGraph(int vertices, int edges) {


int i, src, dest;

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // For undirected graph

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

Q.2) Implementation of static hash table with Linear Probing

->
#include <stdio.h>

#include <stdlib.h>

#define TABLE_SIZE 10 // Define the size of the hash table

int hashTable[TABLE_SIZE];

// Function prototypes

void initializeTable();

int hashFunction(int key);

void insert(int key);

int search(int key);

void display();

int main() {

int choice, key, result;

initializeTable(); // Initialize hash table with -1 (indicating empty slots)

do {

printf("\nHash Table Operations:\n");

printf("1. Insert\n");

printf("2. Search\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {
case 1:

printf("Enter key to insert: ");

scanf("%d", &key);

insert(key);

break;

case 2:

printf("Enter key to search: ");

scanf("%d", &key);

result = search(key);

if (result != -1)

printf("Key %d found at index %d\n", key, result);

else

printf("Key %d not found in hash table\n", key);

break;

case 3:

display();

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;

}
// Function to initialize the hash table

void initializeTable() {

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

hashTable[i] = -1; // -1 indicates an empty slot

// Hash function (Simple Modulo Method)

int hashFunction(int key) {

return key % TABLE_SIZE;

// Function to insert a key into the hash table

void insert(int key) {

int index = hashFunction(key);

int originalIndex = index;

// Linear probing to handle collisions

while (hashTable[index] != -1) {

index = (index + 1) % TABLE_SIZE;

if (index == originalIndex) { // Table is full

printf("Hash Table is full! Cannot insert %d\n", key);

return;

hashTable[index] = key;

printf("Key %d inserted at index %d\n", key, index);

}
// Function to search for a key in the hash table

int search(int key) {

int index = hashFunction(key);

int originalIndex = index;

while (hashTable[index] != -1) {

if (hashTable[index] == key)

return index; // Key found

index = (index + 1) % TABLE_SIZE;

if (index == originalIndex) // Full loop completed

break;

return -1; // Key not found

// Function to display the hash table

void display() {

printf("\nHash Table:\n");

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

if (hashTable[i] == -1)

printf("Index %d: Empty\n", i);

else

printf("Index %d: %d\n", i, hashTable[i]);

}
Slip 9
Q.1) C program to implement graph traversal method using depth first search.

->

#include <stdio.h>

#include <stdlib.h>

#define MAX 100 // Maximum number of vertices

int adj[MAX][MAX]; // Adjacency matrix

int visited[MAX]; // Visited array for DFS

// Function prototypes

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

void DFS(int vertex, int vertices);

int main() {

int vertices, edges, startVertex;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayGraph(vertices);
printf("Enter the starting vertex for DFS: ");

scanf("%d", &startVertex);

printf("DFS Traversal: ");

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

visited[i] = 0; // Initialize visited array

DFS(startVertex, vertices);

printf("\n");

return 0;

// Function to create a graph using adjacency matrix

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize adjacency matrix with 0s

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;
adj[dest][src] = 1; // For undirected graph

// Function to display the adjacency matrix

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

// Function for Depth First Search (DFS) traversal

void DFS(int vertex, int vertices) {

printf("%d ", vertex);

visited[vertex] = 1;

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

if (adj[vertex][i] == 1 && !visited[i]) {

DFS(i, vertices);

}
Q.2) C program to implement BST to perform following operations on BST-

a) Create b) Counting leaf nodes.

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

int countLeafNodes(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {
case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Number of leaf nodes: %d\n", countLeafNodes(root));

break;

case 3:

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

break;

default:

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

} while (choice != 3);

return 0;

void menu() {

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

printf("1. Insert\n");

printf("2. Count Leaf Nodes\n");

printf("3. Exit\n");

// Function to insert a node in BST

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


if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function to count leaf nodes in BST

int countLeafNodes(Node* root) {

if (root == NULL)

return 0;

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

return 1;

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

}
Slip 10
Q.1) Write a C program to implement graph traversal method using depth first search.

->

#include <stdio.h>

#include <stdlib.h>

#define MAX 100 // Maximum number of vertices

int adj[MAX][MAX]; // Adjacency matrix

int visited[MAX]; // Visited array for DFS

// Function prototypes

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

void DFS(int vertex, int vertices);

int main() {

int vertices, edges, startVertex;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayGraph(vertices);
printf("Enter the starting vertex for DFS: ");

scanf("%d", &startVertex);

printf("DFS Traversal: ");

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

visited[i] = 0; // Initialize visited array

DFS(startVertex, vertices);

printf("\n");

return 0;

// Function to create a graph using adjacency matrix

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize adjacency matrix with 0s

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;
adj[dest][src] = 1; // For undirected graph

// Function to display the adjacency matrix

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

// Function for Depth First Search (DFS) traversal

void DFS(int vertex, int vertices) {

printf("%d ", vertex);

visited[vertex] = 1;

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

if (adj[vertex][i] == 1 && !visited[i]) {

DFS(i, vertices);

}
Q.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>

#include <stdlib.h>

#define MAX 100 // Maximum number of vertices

int adj[MAX][MAX]; // Adjacency matrix

// Function prototypes

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

void printIndegree(int vertices);

int main() {

int vertices, edges;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayGraph(vertices);

printIndegree(vertices);
return 0;

// Function to create a graph using adjacency matrix

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize adjacency matrix with 0s

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1; // Directed graph: edge from src to dest

// Function to display the adjacency matrix

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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


}

printf("\n");

// Function to calculate and print in-degree of all vertices

void printIndegree(int vertices) {

int indegree[MAX] = {0}; // Initialize indegree array with 0s

// Calculate in-degree of each vertex

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

for (int j = 0; j < vertices; j++) {

if (adj[j][i] == 1) { // Incoming edge to vertex i

indegree[i]++;

// Print in-degree of each vertex

printf("\nIn-degree of vertices:\n");

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

printf("Vertex %d: %d\n", i, indegree[i]);

}
Slip 11
*Q.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>

#define MAX 100 // Maximum number of vertices

int adj[MAX][MAX]; // Adjacency matrix

int visited[MAX]; // Visited array for DFS

// Function prototypes

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

void DFS(int vertex, int vertices);

int main() {

int vertices, edges, startVertex;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);
createGraph(vertices, edges);

displayGraph(vertices);

printf("Enter the starting vertex for DFS: ");

scanf("%d", &startVertex);

printf("DFS Traversal: ");

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

visited[i] = 0; // Initialize visited array

DFS(startVertex, vertices);

printf("\n");

return 0;

// Function to create a graph using adjacency matrix

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize adjacency matrix with 0s

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");


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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // For undirected graph

// Function to display the adjacency matrix

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

// Function for Depth First Search (DFS) traversal

void DFS(int vertex, int vertices) {

printf("%d ", vertex);

visited[vertex] = 1;

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

if (adj[vertex][i] == 1 && !visited[i]) {

DFS(i, vertices);

}
}

*Q.2)* Implement a Binary search tree (BST) library (btree.h) with operations – create,
preorder. Write a menu-driven program that performs the above operations.

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

void preorder(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 3:

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

break;

default:

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

} while (choice != 3);

return 0;

// Function to display menu

void menu() {

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

printf("1. Insert\n");

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

printf("3. Exit\n");
}

// Function to create and insert a node into BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function for preorder traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

}
Slip 12
Q.1)* Write a C program which uses Binary search tree library and implements the
following function with recursion:

int compare(T1, T2) – compares two binary search trees and returns 1 if they are equal
and 0 otherwise

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

void preorder(Node* root);

int compare(Node* T1, Node* T2);

void menu();

int main() {

Node *T1 = NULL, *T2 = NULL;

int choice, value, treeChoice;

do {

menu();

printf("\nEnter your choice: ");


scanf("%d", &choice);

switch (choice) {

case 1:

printf("Insert into which tree? (1 or 2): ");

scanf("%d", &treeChoice);

printf("Enter value to insert: ");

scanf("%d", &value);

if (treeChoice == 1)

T1 = insert(T1, value);

else if (treeChoice == 2)

T2 = insert(T2, value);

else

printf("Invalid tree choice!\n");

break;

case 2:

printf("Preorder Traversal of Tree 1: ");

preorder(T1);

printf("\n");

printf("Preorder Traversal of Tree 2: ");

preorder(T2);

printf("\n");

break;

case 3:

if (compare(T1, T2))
printf("The trees are equal.\n");

else

printf("The trees are NOT equal.\n");

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;

// Function to display menu

void menu() {

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

printf("1. Insert into Tree\n");

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

printf("3. Compare Two Trees\n");

printf("4. Exit\n");

// Function to create and insert a node into BST

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


if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function for preorder traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

// Function to compare two BSTs recursively

int compare(Node* T1, Node* T2) {

if (T1 == NULL && T2 == NULL)

return 1;

if (T1 == NULL || T2 == NULL)

return 0;
return (T1->data == T2->data) &&

compare(T1->left, T2->left) &&

compare(T1->right, T2->right);

}
Slip 12
*Q.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>

#define MAX 100

int adj[MAX][MAX]; // Adjacency Matrix

int visited[MAX]; // Visited array for DFS

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

void DFS(int vertex, int vertices);

int main() {

int vertices, edges, startVertex;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);
displayGraph(vertices);

printf("Enter the starting vertex for DFS: ");

scanf("%d", &startVertex);

printf("DFS Traversal: ");

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

visited[i] = 0; // Reset visited array before DFS

DFS(startVertex, vertices);

printf("\n");

return 0;

// Function to create a graph using adjacency matrix

void createGraph(int vertices, int edges) {

int src, dest;

// Initialize adjacency matrix with 0

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

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

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);


adj[src][dest] = 1;

adj[dest][src] = 1; // For undirected graph

// Function to display the adjacency matrix

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

// Function to perform Depth First Search (DFS)

void DFS(int vertex, int vertices) {

printf("%d ", vertex);

visited[vertex] = 1;

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

if (adj[vertex][i] == 1 && !visited[i]) {

DFS(i, vertices);

}
Q.2)* Implement a Binary search tree (BST) library (btree.h) with operations – create,
preorder. Write a menu-driven program that performs the above operations.

->

#include <stdio.h>

#include <stdlib.h>

// Structure for BST Node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function Prototypes

Node* insert(Node* root, int data);

void preorder(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:
printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 3:

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

break;

default:

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

} while (choice != 3);

return 0;

// Function to display menu options

void menu() {

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

printf("1. Insert\n");

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

printf("3. Exit\n");

}
// Function to create and insert a node into BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function for Preorder Traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

}
Slip 13
Q.1)* Write a C program which uses Binary search tree library and implements the
following function with recursion:
int compare(T1, T2) – compares two binary search trees and returns 1 if they are equal
and 0 otherwise
->

#include <stdio.h>

#include <stdlib.h>

// Structure for BST Node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function Prototypes

Node* insert(Node* root, int data);

void preorder(Node* root);

int compare(Node* T1, Node* T2);

void menu();

int main() {

Node *T1 = NULL, *T2 = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");


scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert in Tree 1: ");

scanf("%d", &value);

T1 = insert(T1, value);

break;

case 2:

printf("Enter value to insert in Tree 2: ");

scanf("%d", &value);

T2 = insert(T2, value);

break;

case 3:

printf("Preorder Traversal of Tree 1: ");

preorder(T1);

printf("\n");

break;

case 4:

printf("Preorder Traversal of Tree 2: ");

preorder(T2);

printf("\n");

break;

case 5:

if (compare(T1, T2))

printf("Both BSTs are EQUAL.\n");

else

printf("BSTs are NOT equal.\n");


break;

case 6:

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

break;

default:

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

} while (choice != 6);

return 0;

// Function to display menu options

void menu() {

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

printf("1. Insert into Tree 1\n");

printf("2. Insert into Tree 2\n");

printf("3. Preorder Traversal of Tree 1\n");

printf("4. Preorder Traversal of Tree 2\n");

printf("5. Compare Trees\n");

printf("6. Exit\n");

// Function to create and insert a node into BST

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

if (root == NULL) {

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

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function for Preorder Traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

// Function to compare two BSTs

int compare(Node* T1, Node* T2) {

if (T1 == NULL && T2 == NULL)

return 1; // Both trees are empty

if (T1 == NULL || T2 == NULL)

return 0; // One tree is empty, other is not

return (T1->data == T2->data) &&


compare(T1->left, T2->left) &&

compare(T1->right, T2->right);

Q.2)* Write a C program that accepts the vertices and edges of a graph and stores it as
an adjacency matrix. Display the adjacency matri

->

#include <stdio.h>

#define MAX 100 // Maximum number of vertices

int adjMatrix[MAX][MAX]; // Adjacency matrix

// Function prototypes

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

int main() {

int vertices, edges;

// Input number of vertices and edges

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

// Create the adjacency matrix


createGraph(vertices, edges);

// Display the adjacency matrix

displayGraph(vertices);

return 0;

// Function to create the adjacency matrix

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize the adjacency matrix with 0s

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

for (int j = 0; j < vertices; j++) {

adjMatrix[i][j] = 0;

// Read edges and update adjacency matrix

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adjMatrix[src][dest] = 1; // Mark edge from src to dest

adjMatrix[dest][src] = 1; // For undirected graph

}
// Function to display the adjacency matrix

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

}
Slip 14
Q.1)* 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>

// Structure for an adjacency list node

typedef struct Node {

int vertex;

struct Node* next;

} Node;

// Structure for an adjacency list

typedef struct Graph {

int numVertices;

Node** adjLists;

} Graph;

// Function prototypes

Graph* createGraph(int vertices);

void addEdge(Graph* graph, int src, int dest);

void displayGraph(Graph* graph);

Node* createNode(int vertex);

int main() {

int vertices, edges, src, dest;


// Input number of vertices

printf("Enter number of vertices: ");

scanf("%d", &vertices);

// Create graph

Graph* graph = createGraph(vertices);

// Input number of edges

printf("Enter number of edges: ");

scanf("%d", &edges);

// Read edges and add them to the graph

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

addEdge(graph, src, dest);

// Display the adjacency list

displayGraph(graph);

return 0;

// Function to create a graph

Graph* createGraph(int vertices) {

Graph* graph = (Graph*)malloc(sizeof(Graph));

graph->numVertices = vertices;
graph->adjLists = (Node**)malloc(vertices * sizeof(Node*));

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

graph->adjLists[i] = NULL;

return graph;

// Function to create a new node

Node* createNode(int vertex) {

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

newNode->vertex = vertex;

newNode->next = NULL;

return newNode;

// Function to add an edge to the graph

void addEdge(Graph* graph, int src, int dest) {

// Add edge from src to dest

Node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

// Add edge from dest to src (for undirected graph)

newNode = createNode(src);

newNode->next = graph->adjLists[dest];

graph->adjLists[dest] = newNode;
}

// Function to display the adjacency list

void displayGraph(Graph* graph) {

printf("\nAdjacency List:\n");

for (int i = 0; i < graph->numVertices; i++) {

Node* temp = graph->adjLists[i];

printf("Vertex %d: ", i);

while (temp) {

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

temp = temp->next;

printf("\n");

Q.2)* Write a program which uses binary search tree library and counts the total nodes
in the tree. int count(T) – returns the total number of nodes from BST

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;
// Function prototypes

Node* insert(Node* root, int data);

void preorder(Node* root);

int count(Node* root);

void menu();

int main() {

Node *root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 3:

printf("Total nodes in BST: %d\n", count(root));


break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;

void menu() {

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

printf("1. Insert\n");

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

printf("3. Count Nodes\n");

printf("4. Exit\n");

// Function to insert a node into BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

}
if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function for preorder traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

// Function to count total nodes in BST

int count(Node* root) {

if (root == NULL)

return 0;

return 1 + count(root->left) + count(root->right);

}
Slip 15
Q.1)* Implementation of static hash table with Linear Probing.

->

#include <stdio.h>

#define SIZE 10

int hashTable[SIZE];

// Initialize all slots to -1 (indicates empty)

void initialize() {

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

hashTable[i] = -1;

// Hash function

int hashFunction(int key) {

return key % SIZE;

// Insert using linear probing

void insert(int key) {

int index = hashFunction(key);

int i = 0;

while (hashTable[(index + i) % SIZE] != -1 && i < SIZE) {

i++;

}
if (i < SIZE) {

hashTable[(index + i) % SIZE] = key;

printf("Inserted %d at index %d\n", key, (index + i) % SIZE);

} else {

printf("Hash table is full!\n");

// Search key

void search(int key) {

int index = hashFunction(key);

int i = 0;

while (hashTable[(index + i) % SIZE] != key && hashTable[(index + i) % SIZE] != -1 && i <


SIZE) {

i++;

if (hashTable[(index + i) % SIZE] == key)

printf("Key %d found at index %d\n", key, (index + i) % SIZE);

else

printf("Key %d not found\n", key);

// Display hash table

void display() {

printf("\nHash Table:\n");

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


if (hashTable[i] != -1)

printf("[%d] = %d\n", i, hashTable[i]);

else

printf("[%d] = EMPTY\n", i);

// Menu

void menu() {

printf("\nMenu:\n");

printf("1. Insert\n");

printf("2. Search\n");

printf("3. Display\n");

printf("4. Exit\n");

int main() {

int choice, key;

initialize();

do {

menu();

printf("Enter choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter key to insert: ");


scanf("%d", &key);

insert(key);

break;

case 2:

printf("Enter key to search: ");

scanf("%d", &key);

search(key);

break;

case 3:

display();

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;

Q.2)* Write a program which uses binary search tree library and counts the total nodes
in the tree. int count(T) – returns the total number of nodes from BST.

->

#include <stdio.h>

#include <stdlib.h>
// Structure for a BST node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function prototypes

Node* insert(Node* root, int data);

void preorder(Node* root);

int count(Node* root);

void menu();

int main() {

Node* root = NULL;

int choice, value;

do {

menu();

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;
case 2:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 3:

printf("Total number of nodes: %d\n", count(root));

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;

// Menu display

void menu() {

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

printf("1. Insert\n");

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

printf("3. Count Total Nodes\n");

printf("4. Exit\n");

}
// Insert function for BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Preorder traversal

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

// Count total nodes in BST

int count(Node* root) {

if (root == NULL)

return 0;
return 1 + count(root->left) + count(root->right);

}
Slip 16
*Q.1)* C program to implement graph traversal method using Breadth First Search

->

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int adj[MAX][MAX];

int visited[MAX];

// Function Prototypes

void createGraph(int vertices, int edges);

void displayGraph(int vertices);

void BFS(int startVertex, int vertices);

int main() {

int vertices, edges, startVertex;

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

scanf("%d", &vertices);

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

scanf("%d", &edges);

createGraph(vertices, edges);

displayGraph(vertices);
printf("Enter the starting vertex for BFS: ");

scanf("%d", &startVertex);

printf("BFS Traversal: ");

BFS(startVertex, vertices);

printf("\n");

return 0;

// Function to create the graph

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize adjacency matrix

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

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

adj[i][j] = 0;

// Input edges

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // Undirected graph

}
// Function to display adjacency matrix

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

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

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

printf("\n");

// Function for Breadth-First Search

void BFS(int startVertex, int vertices) {

int queue[MAX], front = 0, rear = 0;

// Initialize visited array

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

visited[i] = 0;

visited[startVertex] = 1;

queue[rear++] = startVertex;

while (front < rear) {

int current = queue[front++];

printf("%d ", current);

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

if (adj[current][i] && !visited[i]) {


queue[rear++] = i;

visited[i] = 1;

Q.2)* Implement a Binary Search Tree (BST) library (btree.h) with operations – create,
preorder. Write a menu-driven program that performs the above operations

->

#include <stdio.h>

#include <stdlib.h>

// BST Node Definition

typedef struct Node {

int data;

struct Node* left;

struct Node* right;

} Node;

// BST Operations (Simulating btree.h)

// Function to create a new node

Node* createNode(int data) {

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

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

return newNode;

// Function to insert a node in BST

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

if (root == NULL) {

return createNode(data);

if (data < root->data)

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

else if (data > root->data)

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

return root;

// Preorder Traversal

void preorder(Node* root) {

if (root != NULL) {

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

preorder(root->left);

preorder(root->right);

// Menu-driven main program


int main() {

Node* root = NULL;

int choice, value;

do {

printf("\nBinary Search Tree Menu:\n");

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

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

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Preorder Traversal: ");

preorder(root);

printf("\n");

break;

case 3:

printf("Exiting program.\n");

break;

default:

printf("Invalid choice. Try again.\n");


}

} while (choice != 3);

return 0;

}
Slip 17
*Q.1)* Write a program which uses binary search tree library and counts the total
leaf nodes in the tree. int countLeaf(T) – returns the total number of leaf nodes
from BST

->

#include <stdio.h>

#include <stdlib.h>

// Definition for a BST Node

typedef struct Node {

int data;

struct Node* left;

struct Node* right;

} Node;

// Function to create a new BST node

Node* createNode(int data) {

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

newNode->data = data;

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

return newNode;

// Function to insert a node into BST

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

if (root == NULL)

return createNode(data);
if (data < root->data)

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

else if (data > root->data)

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

return root;

// Function to count leaf nodes

int countLeaf(Node* root) {

if (root == NULL)

return 0;

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

return 1;

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

// Function for preorder traversal

void preorder(Node* root) {

if (root != NULL) {

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

preorder(root->left);

preorder(root->right);
}

// Menu-driven main program

int main() {

Node* root = NULL;

int choice, value;

do {

printf("\nBinary Search Tree Menu:\n");

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

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

printf("3. Count Leaf Nodes\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Preorder Traversal: ");

preorder(root);
printf("\n");

break;

case 3:

printf("Total Leaf Nodes: %d\n", countLeaf(root));

break;

case 4:

printf("Exiting program.\n");

break;

default:

printf("Invalid choice. Try again.\n");

} while (choice != 4);

return 0;

*Q.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>

#define MAX 100

int adj[MAX][MAX]; // Adjacency matrix

void createGraph(int vertices, int edges);

void displayGraph(int vertices);


int main() {

int vertices, edges;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayGraph(vertices);

return 0;

// Function to create the graph

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize matrix to 0

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

}
printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // Remove this line if the graph is directed

// Function to display the adjacency matrix

void displayGraph(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

*Q.3)* Write a C program to traverse the graph using Depth First Search (DFS).

->

#include <stdio.h>

#include <stdlib.h>

#define MAX 100


int adj[MAX][MAX]; // Adjacency matrix

int visited[MAX]; // Visited array

void createGraph(int vertices, int edges);

void DFS(int vertex, int vertices);

void displayAdjMatrix(int vertices);

int main() {

int vertices, edges, startVertex;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayAdjMatrix(vertices);

// Initialize visited array to 0

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

visited[i] = 0;

printf("Enter starting vertex for DFS: ");


scanf("%d", &startVertex);

printf("DFS Traversal: ");

DFS(startVertex, vertices);

printf("\n");

return 0;

// Function to create the graph

void createGraph(int vertices, int edges) {

int src, dest;

// Initialize matrix to 0

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1;

adj[dest][src] = 1; // Remove this line for directed graph

}
}

// Function to perform DFS traversal

void DFS(int vertex, int vertices) {

visited[vertex] = 1;

printf("%d ", vertex);

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

if (adj[vertex][i] == 1 && !visited[i]) {

DFS(i, vertices);

// Function to display adjacency matrix

void displayAdjMatrix(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

}
Slip 18
Q.1)* Write a program which uses binary search tree library and counts the total
leaf nodes in the tree. int countLeaf(T) - returns the total number of leaf nodes
from BST

->

#include <stdio.h>

#include <stdlib.h>

// Structure of a BST Node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function Prototypes

Node* insert(Node* root, int data);

int countLeaf(Node* root);

void inorder(Node* root);

void menu();

int main() {

Node* root = NULL;

int choice, value;

do {

menu();

printf("Enter your choice: ");


scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Inorder Traversal: ");

inorder(root);

printf("\n");

break;

case 3:

printf("Total Leaf Nodes: %d\n", countLeaf(root));

break;

case 4:

printf("Exiting program.\n");

break;

default:

printf("Invalid choice. Try again.\n");

} while(choice != 4);

return 0;

}
// Menu Function

void menu() {

printf("\nBinary Search Tree Menu:\n");

printf("1. Insert\n");

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

printf("3. Count Leaf Nodes\n");

printf("4. Exit\n");

// Function to insert a new node in BST

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

if (root == NULL) {

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

newNode->data = data;

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

return newNode;

if (data < root->data)

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

else if (data > root->data)

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

return root;

}
// Function to perform inorder traversal

void inorder(Node* root) {

if (root != NULL) {

inorder(root->left);

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

inorder(root->right);

// Function to count leaf nodes

int countLeaf(Node* root) {

if (root == NULL)

return 0;

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

return 1;

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

Q.2)* Write a C program that accepts the vertices and edges of a graph and
stores it as an adjacency matrix. Implement functions to print the indegree of all
vertices of the graph.

->

#include <stdio.h>

#define MAX 100


int adj[MAX][MAX];

// Function to create the graph using adjacency matrix

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize matrix to 0

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1; // Directed edge

// Function to display the adjacency matrix

void displayMatrix(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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


}

printf("\n");

// Function to compute and print indegree of each vertex

void computeIndegree(int vertices) {

printf("\nIndegree of each vertex:\n");

for (int col = 0; col < vertices; col++) {

int indegree = 0;

for (int row = 0; row < vertices; row++) {

if (adj[row][col] == 1)

indegree++;

printf("Vertex %d: %d\n", col, indegree);

int main() {

int vertices, edges;

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

scanf("%d", &vertices);

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

scanf("%d", &edges);
createGraph(vertices, edges);

displayMatrix(vertices);

computeIndegree(vertices);

return 0;

}
Slip 19
Q.1)* 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>

// Structure for an adjacency list node

typedef struct Node {

int vertex;

struct Node* next;

} Node;

// Structure for the graph

typedef struct Graph {

int numVertices;

Node** adjLists; // Array of linked lists

} Graph;

// Function to create a node

Node* createNode(int vertex) {

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

newNode->vertex = vertex;

newNode->next = NULL;

return newNode;

}
// Function to create a graph with a given number of vertices

Graph* createGraph(int vertices) {

Graph* graph = (Graph*)malloc(sizeof(Graph));

graph->numVertices = vertices;

graph->adjLists = (Node**)malloc(vertices * sizeof(Node*));

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

graph->adjLists[i] = NULL;

return graph;

// Function to add an edge (undirected)

void addEdge(Graph* graph, int src, int dest) {

// Add edge from src to dest

Node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

// Add edge from dest to src (since it's undirected)

newNode = createNode(src);

newNode->next = graph->adjLists[dest];

graph->adjLists[dest] = newNode;
}

// Function to display the adjacency list

void displayGraph(Graph* graph) {

printf("\nAdjacency List:\n");

for (int i = 0; i < graph->numVertices; i++) {

Node* temp = graph->adjLists[i];

printf("Vertex %d:", i);

while (temp) {

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

temp = temp->next;

printf("\n");

// Main function

int main() {

int vertices, edges, src, dest;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

Graph* graph = createGraph(vertices);

printf("Enter number of edges: ");


scanf("%d", &edges);

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

addEdge(graph, src, dest);

displayGraph(graph);

return 0;

*Q.2)* Implement a Binary search tree (BST) library (btree.h) with operations –
create, preorder. Write a menu-driven program that performs the above
operations.

->

#include <stdio.h>

#include <stdlib.h>

// Structure for BST Node

typedef struct Node {

int data;

struct Node* left;

struct Node* right;


} Node;

// Function to create a new node

Node* createNode(int value) {

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

newNode->data = value;

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

return newNode;

// Function to insert a node into the BST

Node* insert(Node* root, int value) {

if (root == NULL) {

return createNode(value);

if (value < root->data)

root->left = insert(root->left, value);

else if (value > root->data)

root->right = insert(root->right, value);

return root;

// Function to perform preorder traversal

void preorder(Node* root) {


if (root != NULL) {

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

preorder(root->left);

preorder(root->right);

// Function to show the menu

void showMenu() {

printf("\n--- Binary Search Tree Operations ---\n");

printf("1. Insert node\n");

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

printf("3. Exit\n");

printf("Enter your choice: ");

// Main function

int main() {

Node* root = NULL;

int choice, value;

do {

showMenu();

scanf("%d", &choice);

switch (choice) {
case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Preorder traversal: ");

preorder(root);

printf("\n");

break;

case 3:

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

break;

default:

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

} while (choice != 3);

return 0;

}
Slip 20
Q.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>

// Structure for BST Node

typedef struct Node {

int data;

struct Node* left;

struct Node* right;

} Node;

// Function to create a new node

Node* createNode(int value) {

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

newNode->data = value;

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

return newNode;

// Function to insert a node into the BST

Node* insert(Node* root, int value) {

if (root == NULL) {

return createNode(value);
}

if (value < root->data)

root->left = insert(root->left, value);

else if (value > root->data)

root->right = insert(root->right, value);

return root;

// Function to perform inorder traversal

void inorder(Node* root) {

if (root != NULL) {

inorder(root->left);

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

inorder(root->right);

// Menu to display options

void showMenu() {

printf("\n--- Binary Search Tree Operations ---\n");

printf("1. Insert node\n");

printf("2. Inorder traversal\n");

printf("3. Exit\n");

printf("Enter your choice: ");


}

// Main program

int main() {

Node* root = NULL;

int choice, value;

do {

showMenu();

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Inorder traversal: ");

inorder(root);

printf("\n");

break;

case 3:

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

default:

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

} while (choice != 3);

return 0;

Q.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. Implement
functions to print indegree of all vertices of the graph

->

#include <stdio.h>

#define MAX 100

int adj[MAX][MAX]; // Adjacency matrix

int indegree[MAX]; // Array to store indegree of vertices

// Function to create adjacency matrix

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize matrix to 0
for (i = 0; i < vertices; i++) {

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1; // For directed graph

// Function to display adjacency matrix

void displayMatrix(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

// Function to calculate indegree of all vertices

void calculateIndegree(int vertices) {


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

indegree[i] = 0; // Initialize

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

for (int j = 0; j < vertices; j++) {

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

indegree[i]++;

printf("\nIndegree of vertices:\n");

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

printf("Vertex %d: %d\n", i, indegree[i]);

int main() {

int vertices, edges;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);
createGraph(vertices, edges);

displayMatrix(vertices);

calculateIndegree(vertices);

return 0;

}
Slip 21
Q.1)* C program to implement graph traversal method using depth first search.

->

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int adj[MAX][MAX]; // Adjacency matrix

int visited[MAX]; // Visited array

// Function to create the graph

void createGraph(int vertices, int edges) {

int i, src, dest;

// Initialize adjacency matrix to 0

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

for (int j = 0; j < vertices; j++) {

adj[i][j] = 0;

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

adj[src][dest] = 1; // For directed graph

// adj[dest][src] = 1; // Uncomment this line for undirected graph


}

// Function to perform DFS traversal

void DFS(int v, int vertices) {

printf("%d ", v);

visited[v] = 1;

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

if (adj[v][i] == 1 && !visited[i]) {

DFS(i, vertices);

// Function to display adjacency matrix

void displayMatrix(int vertices) {

printf("\nAdjacency Matrix:\n");

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

for (int j = 0; j < vertices; j++) {

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

printf("\n");

}
int main() {

int vertices, edges, start;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

createGraph(vertices, edges);

displayMatrix(vertices);

// Initialize visited array

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

visited[i] = 0;

printf("\nEnter starting vertex for DFS: ");

scanf("%d", &start);

printf("DFS Traversal: ");

DFS(start, vertices);

printf("\n");

return 0;

}
Q.2)* Implementation of Dijkstra’s shortest path algorithm for finding the shortest
path from a given source vertex using an adjacency cost matrix.

->

#include <stdio.h>

#include <limits.h>

#define MAX 100

#define INF 9999

void dijkstra(int graph[MAX][MAX], int n, int start) {

int distance[MAX], visited[MAX] = {0}, i, j, count, min, nextNode;

// Initialize distances

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

distance[i] = graph[start][i];

visited[start] = 1;

distance[start] = 0;

for (count = 1; count < n - 1; count++) {

min = INF;

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

if (!visited[i] && distance[i] < min) {

min = distance[i];

nextNode = i;
}

visited[nextNode] = 1;

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

if (!visited[i] && graph[nextNode][i] != INF &&

distance[nextNode] + graph[nextNode][i] < distance[i]) {

distance[i] = distance[nextNode] + graph[nextNode][i];

printf("\nShortest distances from source vertex %d:\n", start);

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

printf("To %d: %d\n", i, distance[i]);

int main() {

int graph[MAX][MAX], n, i, j, source;

printf("Enter number of vertices: ");

scanf("%d", &n);

printf("Enter adjacency cost matrix (use 9999 for no edge):\n");


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

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

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

printf("Enter source vertex: ");

scanf("%d", &source);

dijkstra(graph, n, source);

return 0;

}
Slip 22
Q.1)* Implement a Binary Search Tree (BST) library (btree.h) with operations –
create, insert, postorder. Write a menu-driven program that performs the above
operations.

->

#include <stdio.h>

#include <stdlib.h>

// Define BST Node

typedef struct Node {

int data;

struct Node *left, *right;

} Node;

// Function to create a new node

Node* createNode(int data) {

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

newNode->data = data;

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

return newNode;

// Insert function

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

if (root == NULL) {

return createNode(data);

}
if (data < root->data)

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

else if (data > root->data)

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

return root;

// Postorder Traversal

void postorder(Node* root) {

if (root != NULL) {

postorder(root->left);

postorder(root->right);

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

// Menu

void menu() {

printf("\n=== Binary Search Tree Operations ===\n");

printf("1. Insert Element\n");

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

printf("3. Exit\n");

printf("Enter your choice: ");

// Main Function
int main() {

Node* root = NULL;

int choice, value;

do {

menu();

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Postorder Traversal: ");

postorder(root);

printf("\n");

break;

case 3:

printf("Exiting program.\n");

break;

default:

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

} while (choice != 3);


return 0;

*Q.2)* Implementation of static hash table with Linear Probing method

->

#include <stdio.h>

#define SIZE 10

int hashTable[SIZE];

// Initialize hash table with -1 (indicating empty)

void initialize() {

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

hashTable[i] = -1;

// Hash function

int hash(int key) {

return key % SIZE;

// Insert using linear probing

void insert(int key) {

int index = hash(key);


int startIndex = index;

while (hashTable[index] != -1) {

index = (index + 1) % SIZE;

if (index == startIndex) {

printf("Hash Table is Full! Cannot insert %d\n", key);

return;

hashTable[index] = key;

printf("Inserted %d at index %d\n", key, index);

// Display the hash table

void display() {

printf("\nHash Table:\n");

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

if (hashTable[i] != -1)

printf("Index %d: %d\n", i, hashTable[i]);

else

printf("Index %d: Empty\n", i);

// Menu

void menu() {
printf("\n=== Hash Table Menu ===\n");

printf("1. Insert\n");

printf("2. Display\n");

printf("3. Exit\n");

printf("Enter your choice: ");

// Main function

int main() {

int choice, key;

initialize();

do {

menu();

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter key to insert: ");

scanf("%d", &key);

insert(key);

break;

case 2:

display();

break;
case 3:

printf("Exiting program.\n");

break;

default:

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

} while (choice != 3);

return 0;

}
Slip 23
Q.1)* C program to implement graph as adjacency List.

->

#include <stdio.h>

#include <stdlib.h>

// Structure for a node in adjacency list

typedef struct Node {

int vertex;

struct Node* next;

} Node;

// Structure for a graph

typedef struct Graph {

int numVertices;

Node** adjLists;

} Graph;

// Function to create a node

Node* createNode(int vertex) {

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

newNode->vertex = vertex;

newNode->next = NULL;

return newNode;

}
// Function to create a graph

Graph* createGraph(int vertices) {

Graph* graph = (Graph*)malloc(sizeof(Graph));

graph->numVertices = vertices;

graph->adjLists = (Node**)malloc(vertices * sizeof(Node*));

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

graph->adjLists[i] = NULL;

return graph;

// Function to add edge (undirected graph)

void addEdge(Graph* graph, int src, int dest) {

// Add edge from src to dest

Node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

// Add edge from dest to src

newNode = createNode(src);

newNode->next = graph->adjLists[dest];

graph->adjLists[dest] = newNode;

}
// Function to display the adjacency list

void displayGraph(Graph* graph) {

printf("\nAdjacency List of the Graph:\n");

for (int v = 0; v < graph->numVertices; v++) {

Node* temp = graph->adjLists[v];

printf("Vertex %d: ", v);

while (temp) {

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

temp = temp->next;

printf("\n");

int main() {

int vertices, edges, src, dest;

printf("Enter number of vertices: ");

scanf("%d", &vertices);

Graph* graph = createGraph(vertices);

printf("Enter number of edges: ");

scanf("%d", &edges);

printf("Enter edges (source destination):\n");


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

scanf("%d %d", &src, &dest);

addEdge(graph, src, dest);

displayGraph(graph);

return 0;

*Q.2)* C program to implement BST to perform following operations on BST:

a) Create

b) Counting Total nodes

->

#include <stdio.h>

#include <stdlib.h>

// Define structure for a node in BST

typedef struct Node {

int data;

struct Node* left;

struct Node* right;

} Node;

// Function to create a new node

Node* createNode(int value) {


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

newNode->data = value;

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

return newNode;

// Function to insert a value into BST

Node* insert(Node* root, int value) {

if (root == NULL) {

return createNode(value);

if (value < root->data) {

root->left = insert(root->left, value);

} else if (value > root->data) {

root->right = insert(root->right, value);

return root;

// Function to count total nodes in BST

int countTotalNodes(Node* root) {

if (root == NULL)

return 0;

return 1 + countTotalNodes(root->left) + countTotalNodes(root->right);

}
// Function to display inorder traversal

void inorder(Node* root) {

if (root != NULL) {

inorder(root->left);

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

inorder(root->right);

// Menu display function

void menu() {

printf("\n--- Binary Search Tree Operations ---\n");

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

printf("2. Display Inorder Traversal\n");

printf("3. Count Total Nodes\n");

printf("4. Exit\n");

int main() {

Node* root = NULL;

int choice, value;

do {

menu();

printf("Enter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Inorder Traversal: ");

inorder(root);

printf("\n");

break;

case 3:

printf("Total Nodes in BST: %d\n", countTotalNodes(root));

break;

case 4:

printf("Exiting program.\n");

break;

default:

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

} while (choice != 4);

return 0;

}
Slip 24
Q.1)* Implement a Binary Search Tree (BST) library (btree.h) with operations –
create, insert, postorder. Write a menu-driven program that performs the above
operations

->

#include <stdio.h>

#include <stdlib.h>

// Define structure for a BST node

typedef struct Node {

int data;

struct Node* left;

struct Node* right;

} Node;

// Function to create a new node

Node* createNode(int value) {

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

newNode->data = value;

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

return newNode;

// Function to insert a node into the BST

Node* insert(Node* root, int value) {

if (root == NULL)

return createNode(value);
if (value < root->data)

root->left = insert(root->left, value);

else if (value > root->data)

root->right = insert(root->right, value);

return root;

// Function for postorder traversal

void postorder(Node* root) {

if (root != NULL) {

postorder(root->left);

postorder(root->right);

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

// Display menu options

void menu() {

printf("\n--- Binary Search Tree Menu ---\n");

printf("1. Insert\n");

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

printf("3. Exit\n");

int main() {
Node* root = NULL;

int choice, value;

do {

menu();

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

break;

case 2:

printf("Postorder Traversal: ");

postorder(root);

printf("\n");

break;

case 3:

printf("Exiting program.\n");

break;

default:

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

}
} while (choice != 3);

return 0;

Q.2)* Write a C program that accepts the vertices and edges of a graph and store
it as an adjacency matrix. Implement a function to traverse the graph using
Breadth First Search (BFS) traversal.

->

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int adjMatrix[MAX][MAX];

int visited[MAX];

int queue[MAX];

int front = -1, rear = -1;

int numVertices;

// Function to add an edge to the graph

void addEdge(int start, int end) {

adjMatrix[start][end] = 1;

adjMatrix[end][start] = 1; // For undirected graph

// Queue operations
void enqueue(int vertex) {

if (rear == MAX - 1) {

printf("Queue overflow\n");

return;

if (front == -1)

front = 0;

queue[++rear] = vertex;

int dequeue() {

if (front == -1 || front > rear) {

return -1;

return queue[front++];

// BFS traversal

void bfs(int startVertex) {

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

visited[i] = 0;

enqueue(startVertex);

visited[startVertex] = 1;

printf("BFS Traversal: ");


while (front <= rear) {

int currentVertex = dequeue();

printf("%d ", currentVertex);

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

if (adjMatrix[currentVertex][i] == 1 && !visited[i]) {

enqueue(i);

visited[i] = 1;

printf("\n");

int main() {

int edges, startVertex, src, dest;

printf("Enter number of vertices: ");

scanf("%d", &numVertices);

// Initialize adjacency matrix to 0

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

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

adjMatrix[i][j] = 0;

printf("Enter number of edges: ");


scanf("%d", &edges);

printf("Enter edges (source destination):\n");

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

scanf("%d %d", &src, &dest);

addEdge(src, dest);

printf("Enter start vertex for BFS: ");

scanf("%d", &startVertex);

bfs(startVertex);

return 0;

}
Slip 25
*Q.1* Implement a Binary Search Tree (BST) library (btree.h) with operations –
create, preorder, and postorder. Write a menu-driven program that performs the
above operations

->

#include <stdio.h>

#include <stdlib.h>

// Define the structure of a BST node

typedef struct Node {

int data;

struct Node* left;

struct Node* right;

} Node;

// Function to create a new node

Node* createNode(int value) {

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

newNode->data = value;

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

return newNode;

// Insert node into BST

Node* insert(Node* root, int value) {

if (root == NULL)
return createNode(value);

if (value < root->data)

root->left = insert(root->left, value);

else if (value > root->data)

root->right = insert(root->right, value);

return root;

// Preorder traversal (Root-Left-Right)

void preorder(Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

// Postorder traversal (Left-Right-Root)

void postorder(Node* root) {

if (root) {

postorder(root->left);

postorder(root->right);

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

}
// Menu

void menu() {

printf("\n--- Binary Search Tree Operations ---\n");

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

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

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

printf("4. Exit\n");

printf("------------------------------------\n");

int main() {

Node* root = NULL;

int choice, value;

do {

menu();

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

root = insert(root, value);

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 program...\n");

break;

default:

printf("Invalid choice. Try again.\n");

} while (choice != 4);

return 0;

*Q.2* Implementation of static hash table with Linear Probing.

->

#include <stdio.h>

#define SIZE 10
int hashTable[SIZE];

// Initialize all slots as -1 (empty)

void initHashTable() {

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

hashTable[i] = -1;

// Hash function

int hashFunction(int key) {

return key % SIZE;

// Insert using Linear Probing

void insert(int key) {

int index = hashFunction(key);

int startIndex = index;

// Search for empty slot

while (hashTable[index] != -1) {

index = (index + 1) % SIZE;

if (index == startIndex) {

printf("Hash table is full! Cannot insert %d\n", key);

return;
}

hashTable[index] = key;

printf("Inserted %d at index %d\n", key, index);

// Search key

int search(int key) {

int index = hashFunction(key);

int startIndex = index;

while (hashTable[index] != -1) {

if (hashTable[index] == key)

return index;

index = (index + 1) % SIZE;

if (index == startIndex)

break;

return -1;

// Display hash table

void display() {

printf("\nHash Table:\n");

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

if (hashTable[i] != -1)
printf("Index %d: %d\n", i, hashTable[i]);

else

printf("Index %d: Empty\n", i);

int main() {

int choice, key;

initHashTable();

do {

printf("\n--- Hash Table Menu ---\n");

printf("1. Insert\n");

printf("2. Search\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter key to insert: ");

scanf("%d", &key);

insert(key);

break;

case 2:
printf("Enter key to search: ");

scanf("%d", &key);

int result = search(key);

if (result != -1)

printf("Key %d found at index %d\n", key, result);

else

printf("Key %d not found\n", key);

break;

case 3:

display();

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;

You might also like