Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DAA LAB

Experiment # 2

Aim of the Experiment : Implement insertion, deletion and searching of a Binary Search Tree (BST).

Programming Language : C

A Binary Search Tree (BST) is a hierarchical data structure, where each node has at most two children.
It follows the property that,
• all nodes in the left subtree of a node have values less than the node's value
• all nodes in the right subtree have values greater than the node's value.
This ordering property allows for efficient searching, insertion, and deletion operations.

ALGORITHM 1 (insertion):
1. Start from the root of the tree.
2. If the tree is empty, create a new node and make it the root.
3. If the key to be inserted is less than the current node's key, move to the left subtree.
4. If the key to be inserted is greater than the current node's key, move to the right subtree.
5. Repeat the process recursively until an appropriate position is found.
6. Insert the new node at the appropriate position.

EXAMPLE :
50
Suppose we want to insert the value 45 into the BST.
/ \
30 70 1. Start from the root (50).
/ \ / \
2. Since 45 < 50, move to the left subtree.
20 40 60 80
3. Since 45 > 30, move to the right subtree of 30.
4. Since 45 > 40, move to the right subtree of 40.
50
/ \ 5. Now, there's no right child of 40, so we insert the new
30 70 node with value 45 here.
/ \ / \
20 40 60 80
\
45
SOURCE-CODE:

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

// Structure for a node in the BST


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 == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Function to insert a new node in the 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 perform inorder traversal of the BST


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

int main() {
// Predefined BST
Node* root = createNode(50);
root->left = createNode(30);
root->right = createNode(70);
root->left->left = createNode(20);
root->left->right = createNode(40);
root->right->left = createNode(60);
root->right->right = createNode(80);

// Print the BST before insertion


printf("Inorder traversal of the BST: ");
inorder(root);
printf("\n");

// Ask user to insert one additional node


int data;
printf("Enter the value of the node to insert: ");
scanf("%d", &data);

// Insert the node


root = insert(root, data);

// Print the BST after insertion


printf("Inorder traversal of the BST after insertion: ");
inorder(root);
printf("\n");

return 0;
}

OUTPUT

Inorder traversal of the BST: 20 30 40 50 60 70 80


Enter the value of the node to insert: 45
Inorder traversal of the BST after insertion: 20 30 40 45 50 60 70 80

---
ALGORITHM 2 (deletion):
1. Start from the root of the tree.
2. If the tree is empty, return NULL.
3. Search for the node containing the key to be deleted.
4. If the node is found:
• If the node has no children,
➢ simply delete the node.
• Else If the node has only one child,
➢ replace the node with its child.
• Else If the node has two children,
➢ find the minimum value node in the right subtree (or maximum value node in the
left subtree),
➢ copy its value to the node to be deleted,
➢ and then delete the minimum value node from the right subtree (or maximum
value node from the left subtree).
5. Return the root of the modified subtree.

EXAMPLE :
50
/ \ Suppose we want to delete the value 30 from the BST.
30 70 1. Start from the root (50).
/ \ / \
20 40 60 80 2. Since 30 < 50, move to the left subtree.
3. Since 30 > 30, move to the right subtree of 30.
4. Since 30 < 40, move to the left subtree of 40.
50
/ \ 5. We find the node containing the value 30.
40 70 6. Since node 30 has two children:
/ / \
20 60 80 ➢ Find the minimum value node in its right subtree, which is 40.
➢ Replace the value of node 30 with the value of node 40.
➢ Delete node 40 from the right subtree.

SOURCE-CODE:

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

// Structure for a node in the BST


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 == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

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


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

// Function to delete a node from the BST


Node* deleteNode(Node* root, int key) {
if (root == NULL) {
return root;
}
if (key < root->data) {
root->left = deleteNode(root->left, key);
} else if (key > root->data) {
root->right = deleteNode(root->right, key);
} else {
if (root->left == NULL) {
Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node* temp = root->left;
free(root);
return temp;
}
Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
// Function to perform inorder traversal of the BST
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

int main() {
Node* root = NULL;
root = createNode(50);
root->left = createNode(30);
root->right = createNode(70);
root->left->left = createNode(20);
root->left->right = createNode(40);
root->right->left = createNode(60);
root->right->right = createNode(80);

// Print the BST before deletion


printf("Inorder traversal of the BST: ");
inorder(root);
printf("\n");

// Ask user to delete one node


int key;
printf("Enter the value of the node to be deleted: ");
scanf("%d", &key);

// Delete the node


root = deleteNode(root, key);

// Print the BST after deletion


printf("Inorder traversal after deleting %d becomes: ", key);
inorder(root);
printf("\n");

return 0;
}

OUTPUT

Inorder traversal of the BST: 20 30 40 50 60 70 80


Enter the value of the node to be deleted: 30
Inorder traversal after deleting 30 becomes: 20 40 50 60 70 80

---
ALGORITHM 3 (searching):
1. Start from the root of the tree.
2. If the tree is empty or the key of the current node is equal to the target key, return the current
node.
3. If the target key is less than the current node's key, search in the left subtree.
4. If the target key is greater than the current node's key, search in the right subtree.
5. Repeat the process recursively until the node containing the target key is found or the subtree
becomes empty.
6. If the target key is not found, return NULL.

EXAMPLE : Suppose we want to search for the value 60 in the BST.


50
1. Start from the root (50).
/ \
30 70 2. Since 60 > 50, move to the right subtree.
/ \ / \ 3. Since 60 < 70, move to the left subtree of 70.
20 40 60 80
4. We find the value 60 in the left subtree of 70.

Result: 60 found in the BST.

SOURCE-CODE:

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

// Structure for a node in the BST


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 == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to search for a node in the 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);
}

int main() {
Node* root = NULL;
root = createNode(50);
root->left = createNode(30);
root->right = createNode(70);
root->left->left = createNode(20);
root->left->right = createNode(40);
root->right->left = createNode(60);
root->right->right = createNode(80);

// Ask user to search one node


int key;
printf("Enter the value of the node to search: ");
scanf("%d", &key);

// Search for the node


Node* result = search(root, key);
if (result != NULL) {
printf("%d found in the BST\n", key);
} else {
printf("%d not found in the BST\n", key);
}

return 0;
}

OUTPUT

Enter the value of the node to search: 60


60 found in the BST

---

You might also like