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

BST Code Implementation

Uploaded by

Wishaq Akbar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

BST Code Implementation

Uploaded by

Wishaq Akbar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Wishaq Akbar Reg #: 2023-BS-AI-041

BST Code Implementation

Code:
#include <iostream>
using namespace std;

// Node ka structure define karte hain


struct Node {
int data; // Node ka data
Node* left; // Left child pointer
Node* right; // Right child pointer
};

// Nayi Node create karne ka function


void create(Node*& root, int val) {
root = new Node(); // Memory allocate karte hain
root->data = val; // Data set karte hain
root->left = nullptr; // Left ko NULL set karte hain
root->right = nullptr; // Right ko NULL set karte hain
}

// Tree me value insert karne ka function


void insert(Node*& root, int val) {
if (root == nullptr) {
create(root, val); // Agar tree khali ho to nayi node create karte
hain
return;
}
if (val < root->data) {
insert(root->left, val); // Choti value left subtree me dalte hain
} else {
insert(root->right, val); // Bari value right subtree me dalte
hain
}
}

// Tree me value search karne ka function


void search(Node* root, int val) {
if (root == nullptr) {
cout << "Value not found" << endl; // Agar value na mile to ye
print karte hain
return;
}
if (val == root->data) {
cout << "Found " << val << endl; // Agar value mil jaye to ye
print karte hain
return;
}
if (val < root->data) {
Name: Wishaq Akbar Reg #: 2023-BS-AI-041

search(root->left, val); // Agar value choti ho to left subtree me


search karte hain
} else {
search(root->right, val); // Agar value badi ho to right subtree
me search karte hain
}
}

// Inorder traversal function (Left, Root, Right)


void inorderTraversal(Node* root) {
if (root == nullptr) return; // Agar tree khali ho to kuch na karein
inorderTraversal(root->left); // Left subtree traverse karein
cout << root->data << " "; // Current node ka data print karein
inorderTraversal(root->right); // Right subtree traverse karein
}

// Preorder traversal function (Root, Left, Right)


void preorderTraversal(Node* root) {
if (root == nullptr) return; // Agar tree khali ho to kuch na karein
cout << root->data << " "; // Current node ka data print karein
preorderTraversal(root->left); // Left subtree traverse karein
preorderTraversal(root->right); // Right subtree traverse karein
}

// Postorder traversal function (Left, Right, Root)


void postorderTraversal(Node* root) {
if (root == nullptr) return; // Agar tree khali ho to kuch na karein
postorderTraversal(root->left); // Left subtree traverse karein
postorderTraversal(root->right); // Right subtree traverse karein
cout << root->data << " "; // Current node ka data print karein
}

// Tree me kisi node ko delete karne ka function


void delNode(Node*& root, int val) {
if (root == nullptr) {
cout << "Value not found!" << endl; // Agar node na mile to ye
print karein
return;
}

if (val < root->data) {


delNode(root->left, val); // Agar value choti ho to left subtree
me delete karein
} else if (val > root->data) {
delNode(root->right, val); // Agar value badi ho to right subtree
me delete karein
} else {
// Jab node mil jaye
if (root->left == nullptr && root->right == nullptr) {
// Agar leaf node ho to delete karein
Name: Wishaq Akbar Reg #: 2023-BS-AI-041

delete root;
root = nullptr;
} else if (root->left == nullptr) {
// Agar sirf right child ho to root ko update karein
Node* temp = root;
root = root->right;
delete temp;
} else if (root->right == nullptr) {
// Agar sirf left child ho to root ko update karein
Node* temp = root;
root = root->left;
delete temp;
} else {
// Agar dono child ho to in-order successor dhundhein
Node* successor = root->right;
while (successor->left != nullptr) {
successor = successor->left;
}
root->data = successor->data; // Root ki value successor se
replace karein
delNode(root->right, successor->data); // Successor ko delete
karein
}
}
}

int main() {
Node* root = nullptr; // Tree ka root node

// Tree me values insert karte hain


insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);

// Different traversals
cout << "Inorder Traversal: ";
inorderTraversal(root);
cout << endl;

cout << "Preorder Traversal: ";


preorderTraversal(root);
cout << endl;

cout << "Postorder Traversal: ";


postorderTraversal(root);
cout << endl;
Name: Wishaq Akbar Reg #: 2023-BS-AI-041

// Tree me value search karte hain


int searchval = 40;
search(root, searchval);

// Tree me node delete karte hain


delNode(root, 50);

// Traversal after deletion


cout << "Inorder Traversal after deletion: ";
inorderTraversal(root);
cout << endl;

return 0;
}
Output:

Algorithm:
1. Structure Definition
• Define a Node structure with:
o data: stores the value.
o left: pointer to the left child.
o right: pointer to the right child.
2. Create Node
• Input: Reference to a Node pointer and a value val.
• Process:
o Allocate memory for a new Node.
o Initialize data with val and left/right as nullptr.
• Output: A new node is created.
3. Insert Node
• Input: Reference to the root node and a value val.
• Process:
o If the tree is empty, create a new node at the root.
o Otherwise:
▪ If val is less than root->data, insert into the left subtree.
▪ Otherwise, insert into the right subtree.
• Output: The tree is updated with the new node.
4. Search Node
• Input: Root node and a value val.
• Process:
Name: Wishaq Akbar Reg #: 2023-BS-AI-041

o If the current node is nullptr, print "Value not found".


o If val matches root->data, print "Found val".
o If val is less, search in the left subtree.
o If val is greater, search in the right subtree.
• Output: Message indicating whether the value is found.
5. Traversal
• Input: Root node.
• Process:
o If the current node is nullptr, return.
o Print the current node's data.
o Visit the left subtree.
o Visit the right subtree.
• Output: Print all nodes in generalized order.
6. Delete Node
• Input: Reference to the root node and a value val.
• Process:
o If the tree is empty, print "Value not found".
o Otherwise:
▪ If val is less, delete from the left subtree.
▪ If val is greater, delete from the right subtree.
▪ If the current node matches:
▪ Case 1: No children - delete the node and set it to nullptr.
▪ Case 2: One child - replace the node with its child and delete it.
▪ Case 3: Two children - find the in-order successor, replace the
node's value with the successor's value, and delete the
successor.
• Output: The tree is updated with the node deleted.
7. Main Function
• Process:
o Initialize the root as nullptr.
o Insert values into the tree.
o Perform traversal and print the nodes.
o Search for a specific value in the tree.
o Delete a node with a specified value.
o Perform traversal again to reflect changes.
• Output: Results of traversal, search, and deletion operations.

Diagram:

You might also like