0% found this document useful (0 votes)
29 views7 pages

AIM:Write A C++ Program To

The document describes a C++ program to create a binary search tree, traverse it in inorder non-recursively, and search for a given element. It defines Node and Stack classes to build the binary search tree. The BST class implements functions to create the tree by inserting elements, traverse it inorder using a stack, and search for an element. The main function creates a BST, inserts elements, traverses it inorder, and searches for an element, outputting the results.

Uploaded by

Rabacca Grace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views7 pages

AIM:Write A C++ Program To

The document describes a C++ program to create a binary search tree, traverse it in inorder non-recursively, and search for a given element. It defines Node and Stack classes to build the binary search tree. The BST class implements functions to create the tree by inserting elements, traverse it inorder using a stack, and search for an element. The main function creates a BST, inserts elements, traverses it inorder, and searches for an element, outputting the results.

Uploaded by

Rabacca Grace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

 AIM:Write a c++ program to

a)create a Binary search tree


b)Traverse binary search tree in inorder non recursively.
c)To search for a given element.
 DESCRIPTION:
o Binary search tree is a tree which holds the properties of binaty tree.
 PROGRAM:
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *lchild,*rchild;
};
class snode
{
public:
node *data;
snode *next;
};
class linkedstack
{
public:
snode *stacktop;
linkedstack()
{
stacktop=NULL;
}
int isEmpty()
{
II B.Tech CSE-A I-SEM 19255A0503
if(stacktop==NULL)
return 1;
else
return 0;
}

void push(node *x)


{
snode *temp=new snode;
temp->data=x;
temp->next=stacktop;
stacktop=temp;

}
node* pop()
{
node *x;
if(stacktop==NULL)
{
cout<<"Stack underflown\n";
}
else
{
x=stacktop->data;
snode *temp=stacktop;
stacktop=stacktop->next;
delete temp;
return x;
}
II B.Tech CSE-A I-SEM 19255A0503
}
};
class Tree
{
public:
virtual void create()=0;
virtual void inorder(node *root)=0;
virtual void search(node *root,int x)=0;
};
class BST :public Tree
{ public:
node *root;
BST()
{
root=new node;
root->lchild=NULL;
root->rchild=NULL;
}

void create();
void inorder(node *root)
{
linkedstack s;
node *temp=root;
while(1)
{
while(temp!=NULL)
{
s.push(temp);
II B.Tech CSE-A I-SEM 19255A0503
temp=temp->lchild;
}
if(s.isEmpty())
return;
temp=s.pop();
cout<<temp->data<<" ";

temp=temp->rchild;
}
}
void search(node *root,int x)
{
if(root!=NULL)
{
if(root->data==x)
{
cout<<"Found\n";
return;
}
else if(x<root->data)
{
search(root->lchild,x);
}
else
search(root->rchild,x);
}

if(root==NULL)
{
II B.Tech CSE-A I-SEM 19255A0503
cout<<"Not found\n";
return;
}
}
};
void BST::create()
{
int x;
char ch;
cout<<"Enter element:";
cin>>x;
root->data=x;
do{
cout<<"Enter element:";
cin>>x;
node *ptr=root;
node *ptr1=root;
while(ptr!=NULL)
{
if(x<ptr->data)
{
ptr1=ptr;
ptr=ptr->lchild;
}

else if(x>ptr->data)
{
ptr1=ptr;
ptr=ptr->rchild;
II B.Tech CSE-A I-SEM 19255A0503
}
else
{
cout<<"Element exist\n";
break;
}
}

if(ptr==NULL)
{
node *temp=new node;
temp->data=x;
temp->lchild=NULL;
temp->rchild=NULL;
if(x<ptr1->data)
ptr1->lchild=temp;
else
ptr1->rchild=temp;
}
cout<<"Do u want to add one more(y,n):";
cin>>ch;
}while(ch=='y'||ch=='Y');
}
int main()
{
BST b;
int n,a,x;
b.create();
b.inorder(b.root);
II B.Tech CSE-A I-SEM 19255A0503
cout<<"\nEnter the element to be searched:";
cin>>x;
b.search(b.root,x);
return 0;
}
OUTPUT:
Enter element:6
Do u want to add one more(y,n):y
Enter element:3
Do u want to add one more(y,n):y
Enter element:8
Do u want to add one more(y,n):y
Enter element:4
Do u want to add one more(y,n):n
3468
Enter the element to be searched:8
Found

II B.Tech CSE-A I-SEM 19255A0503

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