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

Binary Search

This document contains a C++ implementation of a Binary Search Tree (BST) class, which includes methods for inserting, deleting, searching, and traversing the tree in preorder, inorder, and postorder. It defines a node structure and provides user interaction through a menu-driven interface for performing various operations on the BST. The program handles memory management and checks for duplicate entries during insertion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Binary Search

This document contains a C++ implementation of a Binary Search Tree (BST) class, which includes methods for inserting, deleting, searching, and traversing the tree in preorder, inorder, and postorder. It defines a node structure and provides user interaction through a menu-driven interface for performing various operations on the BST. The program handles memory management and checks for duplicate entries during insertion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<iostream>

using namespace std;


struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;
class BST
{
public :
NODE getnode();
void freenode(NODE x);
NODE insert(int item , NODE root);
void preorder(NODE root);
void inorder(NODE root);
void postorder(NODE root);
void display(NODE root, int level);
void search( NODE root);
NODE delete_item(int item, NODE root);
};
NODE BST:: getnode()
{
NODE x;
x= new(struct node);
if(x==NULL)
{
cout<<"Insufficient Memory\n";
exit(0);
}
return x;
}
void BST:: freenode(NODE x)
{
delete x;
}
void BST::preorder(NODE root)
{
if(root == NULL)
return;
cout<<root->info <<" ";
preorder(root->llink);
preorder(root->rlink);
}
void BST::inorder(NODE root)
{
if(root == NULL)
return;
inorder(root->llink);
cout<<root->info<<" ";
inorder(root->rlink);
}
void BST::postorder(NODE root)
{
if(root == NULL)
return;
postorder(root->llink);
postorder(root->rlink);
cout<<root->info<<" ";
}
void BST::display(NODE root, int level)
{
int i;
if(root == NULL) return;
display(root->rlink,level+1);
for(i = 0; i<level; i++)
cout<<" ";
cout<<root->info;
display(root->llink,level+1);
}
NODE BST::insert(int item,NODE root)
{
NODE temp, cur, prev;
temp = getnode();
temp->info = item;
temp->llink = NULL;
temp->rlink = NULL;
if(root == NULL)
return temp;
prev = NULL;
cur = root;
while(cur != NULL)
{
prev = cur;
if(item == cur->info)
{
cout<<"Duplicate items not allowd\n";
freenode(temp);
return root;
}
if(item<cur->info)
cur = cur->llink;
else
cur = cur->rlink;
}
if(item<prev->info)
prev->llink = temp;
else
prev->rlink = temp;
return root;
}
NODE BST::delete_item(int item, NODE root)
{
NODE parent,cur,suc,q;
if(root == NULL)
{
cout<<"Tree is empty, Item not found\n";
return root;
}
parent = NULL;
cur = root;
while(cur != NULL)
{

if(item == cur->info)
break;
parent = cur;
cur = (item < cur->info) ? cur->llink : cur->rlink;
}
if( cur == NULL)
{
cout<<"Item not found\n";
return root;
}
if(cur->llink == NULL)
q = cur->rlink;
else if(cur->rlink == NULL)
q = cur->llink;
else
{
suc = cur->rlink;
while(suc->llink != NULL)
suc = suc->llink;
suc->llink = cur->llink;
q = cur->rlink;
}
if(parent == NULL)
return q;
if(cur == parent->llink)
parent->llink = q;
else
parent->rlink = q;
return root;
}
void BST::search(NODE root)
{
int key,flag=0;
if(root == NULL)
{
cout<<"\n tree is empty";
return;
}
cout<<"\nenter the key::";
cin>>key;
while(root!=NULL)
{
if(key==root->info)
{
flag=1;
cout<<"\n element is found";
break;
}
else if(key<root->info)
root=root->llink;
else
root=root->rlink;
}
if(flag==0)
cout<<"\n element is not found";
}
int main()
{
NODE root = NULL;
int choice,item;
BST obj;
for(;;)
{
cout<<"\n\n1.Insert 2.Preorder 3.Inorder 4.Postorder 5.delete 6.Search
7.Exit \n";
cout<<"\nEnter your choice::";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter the item to be inserted::";
cin>>item;
root = obj.insert(item,root);
break;
case 2:
if(root == NULL)
cout<<"\nTree is empty\n";
else
{
cout<<"\nThe given Tree is \n";
obj.display(root,1);
cout<<"\nPreorder Treaversal is \n";
obj.preorder(root);
cout<<"\n";
}
break;
case 3:
if(root == NULL)
cout<<"\nTree is empty\n";
else
{
cout<<"\nThe given Tree is \n";
obj.display(root,1);
cout<<"\nInorder Treaversal is \n";
obj.inorder(root);
cout<<"\n";
}
break;
case 4:
if(root == NULL)
cout<<"\nTree is empty\n";
else
{
cout<<"\nThe given Tree is \n";
obj.display(root,1);
cout<<"\nPostorder Treaversal is \n";
obj.postorder(root);
cout<<"\n";
}
break;
case 5:
cout<<"\nEnter the item to be deleted\n";
cin>>item;
root =obj.delete_item(item,root);
break;
case 6:
cout<<"\nThe given Tree is \n";
obj.display(root,1);
obj.search(root);
break;
case 7: exit(0) ;
default : cout<<"\n invalid choice";
}
}
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