Binary Search Tree

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

//Program for Binary Search Tree

#include<iostream>
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
};
class BST
{
public:
node* getnewnode(int);
node* insert(node*,int) ;
node* del(node*,int);
bool search(node* root, int data);
node* min(node* root);
void inorder(node* root);
void preorder(node* root);
void postorder(node* root);
};
node* BST::getnewnode (int data)
{
node* ptr= new node;
ptr->data= data;
ptr->left=NULL;
ptr->right=NULL;
return ptr;
}
node* BST::insert(node* root, int data)
{
if(root==NULL)
{
root = getnewnode(data);
return root;
}
else if(root->data < data)
root->right= insert(root->right,data);
else
root->left= insert(root->left,data);
return root;
}
node* BST::del (node* root, int data)
{
if(root==NULL)
return root;
else if(data<root->data)
root->left= del(root->left,data);
else if(data> root->data)
root->right= del(root->right,data);
else
{
if(root->left==NULL && root->right==NULL)
{
delete root;
root= NULL;
return root;
}
else if(root->left==NULL)
{
node* temp=root;
root=root->right;
delete temp;
return root;
}
else if(root->right==NULL)
{
node* temp=root;
root= root->left;
delete temp;
return root;
}
else
{
node* temp=NULL;
temp=min(root->right);
root->data=temp->data;
root->right=del(root->right, temp->data);
return root;
}
}
}
bool BST::search(node* root, int data)
{
if(root==NULL)
return 0;
else if(data==root->data)
return 1;
else if(root->data > data)
return search(root->left,data);
else
return search(root->right,data);
}
node* BST::min(node* root)
{
if(root==NULL)
return NULL;
else if(root->left==NULL)
return root;
else
return min(root->left);
}
void BST::inorder(node* root)
{
if(root==NULL)
return;
else
{
inorder(root->left);
cout<<root->data<<"\t";
inorder(root->right);
}
}
void BST::preorder(node* root)
{
if(root==NULL)
return;
else
{
cout<<root->data<<"\t";
preorder(root->left);
preorder(root->right);
}
}
void BST::postorder(node* root)
{
if(root==NULL)
return;
else
{
postorder(root->left);
postorder(root->right);
cout<<root->data<<"\t";
}
}
int main()
{
BST tree;
node* root=NULL;
int choice, data, find;
cout<<"BINARY SEARCH TREE IMPLEMENTATIONS"<<endl;
do
{
cout<<"\n\nMenu\n1. Insert\n2. Delete\n3. Traversal\n4. Search\n5. Exit\n";
cout<<"\nEnter ur choice:";
cin>>choice;
switch(choice)
{
case 1 : cout<<"\nEnter the element to be inserted : ";
cin>>data;
root=tree.insert(root,data);
cout<<"\nThe element "<<data<<" inserted successfully ";
break;
case 2 : cout<<"Enter the element to be deleted : ";
cin>>data;
root=tree.del(root,data);
cout<<"\nThe element "<<data<<" deleted successfully ";
break;
case 3 : cout<<"\nInorder Traversal:\n ";
tree.inorder(root);
cout<<"\nPreorder Traversal: \n";
tree.preorder(root);
cout<<"\nPostorder Traversal: \n";
tree.postorder(root);
break;
case 4 : cout<<"Enter the element to be searched : ";
cin>>data;
find= tree.search(root,data);
if(find==1)
cout<<"The element "<<data<<" is present\n";
else
cout<<"The element "<<data<<" is not present\n";
break;
}
}while(choice<5);
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