0% found this document useful (0 votes)
10 views

Data Structures and Algorithms Lab Assessment 5

DSA assessment
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)
10 views

Data Structures and Algorithms Lab Assessment 5

DSA assessment
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/ 11

23BKT0031

GAUTAM KRISHNA KUMAR

Data Structures and Algorithms Lab Assessment 5:


Mark: 10 Deadline: 23-10-2024

1. You are given a pointer to the root of a binary search tree and values to be inserted into the tree.
Apply the insert, delete and display operation in the binary search tree.

#include <stdio.h>

#include <stdlib.h>

// Definition of the node structure

struct Node {

int data;

struct Node *left, *right;

};

// Function to create a new node

struct Node* createNode(int data) {

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

newNode->data = data;

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

return newNode;

// Function to insert a value into the BST

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

if (root == NULL)

return createNode(data);
23BKT0031
GAUTAM KRISHNA KUMAR

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 find the minimum value node in the tree

struct Node* minValueNode(struct Node* node) {

struct Node* current = node;

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

current = current->left;

return current;

// Function to delete a value from the BST

struct Node* deleteNode(struct Node* root, int data) {

if (root == NULL)

return root;

if (data < root->data)

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

else if (data > root->data)

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

else {

// Node with only one child or no child

if (root->left == NULL) {

struct Node* temp = root->right;


23BKT0031
GAUTAM KRISHNA KUMAR

free(root);

return temp;

else if (root->right == NULL) {

struct Node* temp = root->left;

free(root);

return temp;

// Node with two children

struct Node* temp = minValueNode(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

return root;

// Function to do an inorder traversal of the BST

void inorder(struct Node* root) {

if (root != NULL) {

inorder(root->left);

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

inorder(root->right);

int main() {

struct Node* root = NULL;


23BKT0031
GAUTAM KRISHNA KUMAR

// Inserting values

root = insert(root, 50);

root = insert(root, 30);

root = insert(root, 20);

root = insert(root, 40);

root = insert(root, 70);

root = insert(root, 60);

root = insert(root, 80);

printf("Inorder traversal of the tree:\n");

inorder(root);

printf("\n");

// Deleting a node

root = deleteNode(root, 20);

printf("Inorder traversal after deleting 20:\n");

inorder(root);

printf("\n");

root = deleteNode(root, 30);

printf("Inorder traversal after deleting 30:\n");

inorder(root);

printf("\n");

root = deleteNode(root, 50);

printf("Inorder traversal after deleting 50:\n");

inorder(root);

printf("\n");

printf(“Gautam kk 23BKT0031”)

return 0;
23BKT0031
GAUTAM KRISHNA KUMAR

2. Write a C program to implement the following binary tree traversals using switch statement. a.
Inorder Traversal b. Preorder Traversal c. Postorder Traversal

#include <stdio.h>

#include <stdlib.h>

// Define the node structure

struct Node {

int data;

struct Node *left, *right;

};

// Function to create a new node

struct Node* createNode(int data) {

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

newNode->data = data;

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

return newNode;

// Inorder Traversal: Left, Root, Right

void inorderTraversal(struct Node* root) {

if (root != NULL) {

inorderTraversal(root->left);

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

inorderTraversal(root->right);

// Preorder Traversal: Root, Left, Right


23BKT0031
GAUTAM KRISHNA KUMAR

void preorderTraversal(struct Node* root) {

if (root != NULL) {

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

preorderTraversal(root->left);

preorderTraversal(root->right);

// Postorder Traversal: Left, Right, Root

void postorderTraversal(struct Node* root) {

if (root != NULL) {

postorderTraversal(root->left);

postorderTraversal(root->right);

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

int main() {

struct Node* root = NULL;

// Creating the binary tree

root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

root->right->left = createNode(6);

root->right->right = createNode(7);

int choice;
23BKT0031
GAUTAM KRISHNA KUMAR

// Display traversal options

printf("Select a traversal method:\n");

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

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

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

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Inorder Traversal: ");

inorderTraversal(root);

break;

case 2:

printf("Preorder Traversal: ");

preorderTraversal(root);

break;

case 3:

printf("Postorder Traversal: ");

postorderTraversal(root);

break;

default:

printf("Invalid choice.\n");

break;

printf("\n");

printf(“Gautam kk”)

return 0;

}
23BKT0031
GAUTAM KRISHNA KUMAR

3. Write a C program to implement the following Graph traversals a. BFS b. DFS

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

// Graph structure using adjacency matrix

int adjMatrix[MAX][MAX];

int visited[MAX];

int queue[MAX];

int front = -1, rear = -1;

int n; // Number of vertices

// Function to add an edge in the graph

void addEdge(int u, int v) {

adjMatrix[u][v] = 1;

adjMatrix[v][u] = 1; // For undirected graph

// Function for BFS Traversal

void bfs(int start) {

int i;

front = rear = 0;

queue[rear] = start;

visited[start] = 1;

printf("BFS Traversal: ");

while (front <= rear) {

int vertex = queue[front++];

printf("%d ", vertex);


23BKT0031
GAUTAM KRISHNA KUMAR

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

if (adjMatrix[vertex][i] == 1 && visited[i] == 0) {

queue[++rear] = i;

visited[i] = 1;

printf("\n");

// Function for DFS Traversal

void dfs(int vertex) {

printf("%d ", vertex);

visited[vertex] = 1;

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

if (adjMatrix[vertex][i] == 1 && visited[i] == 0) {

dfs(i);

int main() {

int i, j, start, choice, u, v, edges;

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

scanf("%d", &n);

// Initialize adjacency matrix to 0

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

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


23BKT0031
GAUTAM KRISHNA KUMAR

adjMatrix[i][j] = 0;

// Input edges

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

scanf("%d", &edges);

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

printf("Enter edge (u v): ");

scanf("%d %d", &u, &v);

addEdge(u, v);

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

scanf("%d", &start);

printf("Choose traversal method:\n");

printf("1. BFS\n");

printf("2. DFS\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

// Initialize visited array for BFS

for (i = 0; i < n; i++) visited[i] = 0;

bfs(start);

break;

case 2:

// Initialize visited array for DFS


23BKT0031
GAUTAM KRISHNA KUMAR

for (i = 0; i < n; i++) visited[i] = 0;

printf("DFS Traversal: ");

dfs(start);

printf("\n");

break;

default:

printf("Invalid choice!\n");

break;

printf(“Gautam”)

return 0;

You might also like