0% found this document useful (0 votes)
10 views4 pages

PR 4 B-6

The document contains C++ code for a Binary Search Tree (BST) implementation, including functions for insertion, deletion, searching, in-order traversal, and calculating the depth of the tree. It also includes a menu-driven interface for user interaction to perform various operations on the BST. The code is structured with appropriate function definitions and control flow for managing the BST operations.

Uploaded by

khodesnehal2503
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)
10 views4 pages

PR 4 B-6

The document contains C++ code for a Binary Search Tree (BST) implementation, including functions for insertion, deletion, searching, in-order traversal, and calculating the depth of the tree. It also includes a menu-driven interface for user interaction to perform various operations on the BST. The code is structured with appropriate function definitions and control flow for managing the BST operations.

Uploaded by

khodesnehal2503
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/ 4

#include<iostream> else

using namespace std; node->right = insert(node->right, key);

return node;

struct bstnode }

int data; struct bstnode* search(struct bstnode* root, int key)

struct bstnode *left, *right; {

}; if (root == NULL || root->data == key)

return root;

struct bstnode *newNode(char v)

{ // Key is greater than root's key

bstnode* temp = new bstnode(); if (root->data < key)

temp->data = v; return search(root->right, key);

temp->left = temp->right = NULL; else

return temp; return search(root->left, key);

} }

void inorder(struct bstnode *root) struct bstnode * minValueNode(struct bstnode* node)

{ {

if (root != NULL) struct bstnode* current = node;

{ while (current && current->left != NULL)

inorder(root->left); current = current->left;

cout<<root->data<<" "; return current;

inorder(root->right); }

} struct bstnode* deleteNode(struct bstnode* root, int


key)

{
struct bstnode* insert(struct bstnode* node, int key)
if (root == NULL)
{
return root;
if (node == NULL) return newNode(key);
if (key < root->data)
if (key < node->data)
root->left = deleteNode(root->left, key);
node->left = insert(node->left, key);
else if (key > root->data) return;

root->right = deleteNode(root->right, key); else

else {

{ struct bstnode* temp;

mirror(node->left);

if (root->left == NULL) mirror(node->right);

struct bstnode *temp = root->right; temp= node->left;

delete root; node->left = node->right;

return temp; node->right = temp;

} }

else if (root->right == NULL)

{ struct bstnode* copy(struct bstnode *root)

struct bstnode *temp = root->left; {

delete root; bstnode *root2;

return temp; if(root==NULL)

} return NULL;

else root2=new bstnode;

{ root2->left=copy(root->left);

struct bstnode* temp = minValueNode(root- root2->right=copy(root->right);


>right);
root2->data=root->data;
root->data = temp->data;

root->right = deleteNode(root->right, temp-


return root2;
>data);
}
}
int Maxdepth(struct bstnode *root)
}
{
return root;
if(root==NULL)
}
{

return 0;
void mirror(struct bstnode* node)
}
{
else
if (node == NULL)
{
cout<<"\n*************************************
int depth1=Maxdepth(root->left);
*******";
int depth2=Maxdepth(root->right);
cout<<"\nEnter your choice: ";
if(depth1>depth2)
cin>>ch;
return (depth1+1);
switch(ch)
else
{
return (depth2+1);
case 1:

cout<<"\nEnter number of values to insert: ";


}
cin>>n;
}
cout<<"\nEnter the values to create BST: ";

for(int i=0; i<n; i++)


int main()
{
{
cin>>d;
struct bstnode *root = NULL;
root = insert(root, d);
struct bstnode *root2=NULL;
}
struct bstnode *root1=NULL;
break;
struct bstnode *root3=NULL;
case 2:
int ch,n,d,depth;
cout<<"\nEnter the element to delete: ";
while(1)
cin>>d;
{
root3=deleteNode(root, d);

break;
cout<<"\n****************MENU****************
*********"; case 3:

cout<<endl<<"\t\t1.INSERT"; cout<<"\nEnter the element to search: ";

cout<<endl<<"\t\t2.DELETE"; cin>>d;

cout<<endl<<"\t\t3.SEARCH"; root1=search(root, d);

cout<<endl<<"\t\t4.TRAVERSAL"; if(root1!=NULL)

cout<<endl<<"\t\t5.DEPTH OF BST"; cout<<"\nFound in BST!!";

cout<<endl<<"\t\t6.COPY OF BST"; else

cout<<endl<<"\t\t7.MIRROR IMAGE"; cout<<"\nNot Found in BST!!";

cout<<endl<<"\t\t8.EXIT";
break;

case 4:

cout<<"\nTraversal of BST: ";

inorder(root);

break;

case 5:

depth=Maxdepth(root);

cout<<"The depth of BST is: "<<depth;

break;

case 6:

root2=copy(root);

cout<<"The copy of BST is: ";

inorder(root2);

break;

case 7:

mirror(root);

cout <<"\nInorder traversal of the mirror BST


is: ";

inorder(root);

mirror(root);

break;

case 8:

return 0;

default:

cout<<"\nWrong Option";

return 0;

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