0% found this document useful (0 votes)
39 views10 pages

Lab Report 5

This document discusses implementing binary search trees in C including insertion, deletion, and traversal algorithms. It also covers implementing recursive functions for pre-order, in-order, and post-order tree traversals.

Uploaded by

abinashreddy792
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)
39 views10 pages

Lab Report 5

This document discusses implementing binary search trees in C including insertion, deletion, and traversal algorithms. It also covers implementing recursive functions for pre-order, in-order, and post-order tree traversals.

Uploaded by

abinashreddy792
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/ 10

AURORA DEEMED TO BE UNIVERSITY

DATA STRUCTURES

NAME: K. ABINASH
CLASS: CSE-D
REG NO: AUU23EGCSE118

LAB REPORT-5

Programs:
1. Write a C program to implement Binary Search Tree
(i) Insertion

(ii) Deletion

(iii) Traversals

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

// Structure for a BST node


struct Node {
int key;
struct Node *left, *right;
};

// Function to create a new BST node


struct Node* createNode(int item) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// Function to insert a new key in BST


struct Node* insert(struct Node* node, int key) {
// If the tree is empty, return a new node
if (node == NULL) return createNode(key);

// Otherwise, recur down the tree


if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);

// Return the unchanged node pointer


return node;
}

// Function to find the minimum value node in a given BST


struct Node* minValueNode(struct Node* node) {
struct Node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}

// Function to delete a key from BST


struct Node* deleteNode(struct Node* root, int key) {
if (root == NULL) return root;

// Recur down the tree


if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// Node with only one child or no child
if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;
}
// Node with two children: Get the inorder successor (smallest in the
right subtree)
struct Node* temp = minValueNode(root->right);

// Copy the inorder successor's content to this node


root->key = temp->key;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->key);
}
return root;
}

// Function to do inorder traversal of BST


void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}

int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

printf("Inorder traversal of the BST is: ");


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

printf("Deleting 20 from the BST...\n");


root = deleteNode(root, 20);
printf("Inorder traversal of the modified BST is: ");
inorder(root);
printf("\n");

return 0;
}
2. Write a C program to implement the traversals of Binary Search Tree
using Recursive Functions
(i) Pre-Order

(ii) In-Order

(iii) Post-Order introduction and explanation

PROGRAM:

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

// Structure for a BST node


struct Node {
int key;
struct Node *left, *right;
};

// Function to create a new BST node


struct Node* createNode(int item) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Function to insert a new key in BST
struct Node* insert(struct Node* node, int key) {
// If the tree is empty, return a new node
if (node == NULL) return createNode(key);

// Otherwise, recur down the tree


if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);

// Return the unchanged node pointer


return node;
}

// Function for pre-order traversal of BST


void preorder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->key); // Visit the root
preorder(root->left); // Traverse the left subtree
preorder(root->right); // Traverse the right subtree
}
}

// Function for in-order traversal of BST


void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left); // Traverse the left subtree
printf("%d ", root->key); // Visit the root
inorder(root->right); // Traverse the right subtree
}
}

// Function for post-order traversal of BST


void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left); // Traverse the left subtree
postorder(root->right); // Traverse the right subtree
printf("%d ", root->key); // Visit the root
}
}

int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

printf("Pre-order traversal of the BST is: ");


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

printf("In-order traversal of the BST is: ");


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

printf("Post-order traversal of the BST is: ");


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

return 0;
}

Conclusion:
The laboratory experiment demonstrated the implementation of the Queue ADT
and its application in DFS and BFS graph traversal algorithms. These
algorithms are fundamental in graph theory and are used in various applications
such as network routing, social network analysis, and pathfinding in games and
robotics.

You might also like