0% found this document useful (0 votes)
5 views5 pages

Sa3 2101053 Dsa6

The document contains a C++ program that implements a Binary Search Tree (BST) with functionalities to create a tree, insert nodes, search for values, and delete nodes. It includes a user interface for interacting with the tree, allowing users to add, search, and delete nodes based on user input. The program demonstrates the creation of a BST with sample data and provides an interactive menu for further operations.

Uploaded by

rudrajamdarsits
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)
5 views5 pages

Sa3 2101053 Dsa6

The document contains a C++ program that implements a Binary Search Tree (BST) with functionalities to create a tree, insert nodes, search for values, and delete nodes. It includes a user interface for interacting with the tree, allowing users to add, search, and delete nodes based on user input. The program demonstrates the creation of a BST with sample data and provides an interactive menu for further operations.

Uploaded by

rudrajamdarsits
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/ 5

ASSIGNMENT NO.

- 3

NAME-JAMDAR RUDRA RAHUL


STD-SE DIV-A
ROLL NO.-53

INPUT

#include<iostream>
#include<string>
using namespace std;

class
node{ public:
string key;
string value;
node*left;
node*right;

node(string k, string val)


{ key = k;
value = val;
left = NULL;
right = NULL;
}
};

class
BST{ public:
int n;
node*root;

BST(){
root = NULL;
}

node* createNode(string key, string value)


{ return new node(key, value);
}

void createTree(){
node*temp;
cout<<"Enter the number of elements:";
cin>>n;
for(int i=0; i<n; i++)
{ string key, value;
cout<<"Enter the key and value (separated by space): ";
cin>>key>>value;
temp = createNode(key, value);
if(root == NULL){
root = temp;
}
else{
insertNode(root, temp);
}
}
}

void insertNode(node*root, node*temp){


if(temp->key < root->key){
if(root->left == NULL){
root->left = temp;
}
else{
insertNode(root->left, temp);
}
}
else{
if(root->right == NULL){
root->right = temp;
}
else{
insertNode(root->right, temp);
}
}
}

void inorder(node*root){
if(root == NULL){
return;
}
inorder(root->left);
cout<< "(" << root->key << ", " << root->value << ") ";
inorder(root->right);
}

void search(node*root, string key)


{ if(root == NULL){
cout<<key<<" is not present."<<endl;
return;
}
if(root->key == key){
cout<<key<<" is present with value: "<<root->value<<endl;
}
else if(root->key > key){
return search(root->left, key);
}
else{
return search(root->right, key);
}
}

void addnode(string key, string value){


node* temp = createNode(key, value);
insertNode(root, temp);
inorder(root);
cout<<endl;
}

node* findMin(node* root)


{ while(root->left != NULL)
{
root = root->left;
}
return root;
}

node* deleteNode(node* root, string key)


{ if(root == NULL) {
return root;
}

// Find the node to be deleted


if(key < root->key) {
root->left = deleteNode(root->left, key);
}
else if(key > root->key) {
root->right = deleteNode(root->right, key);
}
else { // Node to be deleted is found
// Case 1: Node has no children (leaf node)
if(root->left == NULL && root->right == NULL)
{ delete root;
root = NULL;
}
// Case 2: Node has one child
else if(root->left == NULL) {
node* temp = root;
root = root->right;
delete temp;
}
else if(root->right == NULL)
{ node* temp = root;
root = root->left;
delete temp;
}
// Case 3: Node has two children
else {
node* temp = findMin(root->right); // Find in-order successor
root->key = temp->key;
root->value = temp->value;
root->right = deleteNode(root->right, temp->key); // Delete the in-order successor
}
}

return root;
}

};

int main(){
BST b;
string key1, key2, value1;

b.createTree();
cout << endl << "The tree generated is:";
b.inorder(b.root);
cout << endl;

cout<<"-------------------Menu -------------------" << endl;


cout<< "1. Add new node." << endl;
cout<< "2. Search an element in the tree." << endl;
cout<< "3. Delete an element from the tree." << endl;
cout<< "4. Exit program." << endl;
while(true)

{ int

choice;

cout<< "------------------------------------------" << endl;


cout<< "Enter your choice: ";
cin>> choice;

switch(choice)
{ case 1:
cout<<"Enter key and value to insert: ";
cin>>key1>>value1;
b.addnode(key1, value1);
break;

case 2:
cout<<"Enter the key to find: ";
cin>>key1;
b.search(b.root, key1);
break;
case 3:
cout<<"Enter the key to delete: ";
cin>>key2;
b.deleteNode(b.root, key2);
b.inorder(b.root);
cout<<endl;
break;

case 4:
cout<<"Program closed."<<endl;
return 0;

default:
break;
}
}
}

Output:

Enter the number of elements:5


Enter the key and value (separated by space): Nexon Shriram
Enter the key and value (separated by space): Baleno Mahesh
Enter the key and value (separated by space): Swift Pawan
Enter the key and value (separated by space): Compass Rushi
Enter the key and value (separated by space): Nano Tushar

The tree generated is:(Baleno, Mahesh) (Compass, Rushi) (Nano, Tushar) (Nexon, Shriram) (Swift,
Pawan)
-------------------Menu-------------------
1. Add new node.
2. Search an element in the tree.
3. Delete an element from the tree.
4. Exit program.

Enter your choice: 2


Enter the key to find: Nexon
Nexon is present with value: Shriram

Enter your choice: 1


Enter key and value to insert: Verna Rohit
(Baleno, Mahesh) (Compass, Rushi) (Nano, Tushar) (Nexon, Shriram) (Swift, Pawan) (Verna,
Rohit)

Enter your choice: 3


Enter the key to delete: Compass
(Baleno, Mahesh) (Nano, Tushar) (Nexon, Shriram) (Swift, Pawan) (Verna, Rohit)

Enter your choice: 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy