0% found this document useful (0 votes)
14 views3 pages

Slip 1

The document contains code for implementing a binary search tree with operations to create, insert and perform inorder traversal on nodes. It also contains code for representing a graph as an adjacency matrix, adding edges and printing the matrix and indegrees of vertices.

Uploaded by

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

Slip 1

The document contains code for implementing a binary search tree with operations to create, insert and perform inorder traversal on nodes. It also contains code for representing a graph as an adjacency matrix, adding edges and printing the matrix and indegrees of vertices.

Uploaded by

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

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.

ANS:-

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

typedef struct Node {


int data;
struct Node* left;
struct Node* right;
} Node;

Node* createNode(int data) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

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

void inorder(Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

int main() {
Node* root = NULL;
int choice, data;

do {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Inorder Traversal\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

// Consume newline character from input buffer


getchar();

switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\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 graph.

ANS:-

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

#define MAX_VERTICES 100

int adjMatrix[MAX_VERTICES][MAX_VERTICES];
int indegree[MAX_VERTICES];

void initializeGraph(int vertices) {


int i, j;
for (i = 0; i < vertices; i++) {
indegree[i] = 0;
for (j = 0; j < vertices; j++) {
adjMatrix[i][j] = 0;
}
}
}

void addEdge(int start, int end) {


adjMatrix[start][end] = 1;
indegree[end]++;
}

void displayAdjMatrix(int vertices) {


int i, j;
printf("Adjacency Matrix:\n");
for (i = 0; i < vertices; i++) {
for (j = 0; j < vertices; j++) {
printf("%d ", adjMatrix[i][j]);
}
printf("\n");
}
}

void printIndegree(int vertices) {


int i;
printf("Indegree of vertices:\n");
for (i = 0; i < vertices; i++) {
printf("Vertex %d: %d\n", i, indegree[i]);
}
}

int main() {
int vertices, edges, i;
int start, end;

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


scanf("%d", &vertices);
printf("Enter the number of edges in the graph: ");
scanf("%d", &edges);

initializeGraph(vertices);

printf("Enter the edges (start end):\n");


for (i = 0; i < edges; i++) {
scanf("%d %d", &start, &end);
addEdge(start, end);
}

displayAdjMatrix(vertices);
printIndegree(vertices);

return 0;
}

You might also like