Practical_5_DATA STRUCTURE 1 - Copy
Practical_5_DATA STRUCTURE 1 - Copy
Program:
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
else
return root;
root = root->left;
return root;
if (!root->left) {
delete root;
return temp;
} else if (!root->right) {
delete root;
return temp;
return root;
if (root) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
int main() {
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
inorder(root);
inorder(root);
return 0;
}
Output:
Explanation:
1. Node Structure for BST
The Node structure represents each node in the binary search tree.
struct Node {
int data;
Node* left;
Node* right;
};
• Node* left: Points to the left child of the current node (values smaller than data).
• Node* right: Points to the right child of the current node (values greater than data).
• The constructor Node(int value) initializes the node's data and sets its left and right pointers
to nullptr.
2. Insert an Element into BST
else
return root;
• Goal: To insert a new value into the tree while maintaining the BST property (left subtree has
smaller values, right subtree has larger values).
• How it works:
1. Base case: If root is nullptr, we create a new node with the given value and return it.
This happens when we've found the appropriate empty spot to insert the value.
2. Recursive calls: If the value is smaller than the current node’s data, we recursively
insert it into the left subtree. If the value is larger, we insert it into the right subtree.
3. Return the root: After the recursive calls, we return the root to maintain the tree's
structure.
This function helps to find the minimum value in a subtree. It's particularly useful when deleting a
node with two children, where we need to replace the node with its inorder successor (smallest
node in the right subtree).
root = root->left;
return root;
• How it works: Starting from the given root, the function keeps moving left until it finds the
leftmost (smallest) node and returns it.
else {
if (!root->left) {
delete root;
return temp;
} else if (!root->right) {
delete root;
return temp;
root->data = temp->data;
return root;
• Goal: To delete a node with the given value while maintaining the BST structure.
• Steps:
1. Find the node to delete: If value is smaller than the current node's data, we
recursively search in the left subtree. If it's larger, we search in the right subtree.
▪ Case 1: No left child: If the node to be deleted has no left child, replace the
node with its right child and delete the node.
▪ Case 2: No right child: If the node has no right child, replace it with its left
child and delete the node.
▪ Case 3: Two children: Find the node’s inorder successor (the smallest node
in its right subtree) using findMin(), copy the successor’s value to the current
node, and then delete the successor.
3. Return the updated root: This ensures the correct structure of the tree after the
deletion.
5. Inorder Traversal
if (root) {
• How it works:
o This ensures that the values are printed in ascending order (since in a BST, left < root
< right).
6. Main Function
int main() {
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
cout << "Inorder after insertion: ";
inorder(root);
inorder(root);
return 0;
• Tree construction: The code first constructs the BST by inserting values into it using the
insert function. The values inserted are: 50, 30, 70, 20, 40, 60, and 80.
• Display the tree (inorder traversal): The inorder traversal after insertion prints the tree in
ascending order.
• Delete a node: The code deletes the node with value 50. Since node 50 has two children, the
deletion function finds the inorder successor (which is 60), replaces 50 with 60, and deletes
60.
• Display the updated tree (inorder traversal): After deletion, the new inorder traversal prints: