Professional Documents
Culture Documents
DAA Lab Expt 2
DAA Lab Expt 2
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>
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);
return 0;
}
OUTPUT
---
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>
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);
return 0;
}
OUTPUT
---
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.
SOURCE-CODE:
#include <stdio.h>
#include <stdlib.h>
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);
return 0;
}
OUTPUT
---