Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create CREATED BINARY SEARCH ITERATIVE Cpp #322

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions CREATED BINARY SEARCH ITERATIVE Cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node*left;
node*right;
node(int k){
this->data=k;
this->left=NULL;
this->right=NULL;
}
};
node* insertinBST(node*&root,int d){
if(root==NULL){
root=new node(d);
return root;
}
if(d>root->data){
root->right=insertinBST(root->right,d);

}
else{
root->left=insertinBST(root->left,d);
}
return root;
}
void takeinput(node*&root){
int data;
cin>>data;
while(data!=-1){
insertinBST(root,data);
cin>>data;
}
}
void levelorder(node*root){
queue<node*>q;
q.push(root);
q.push(NULL);
while(!q.empty( )){
node*temp=q.front();
q.pop();
if(temp==NULL){
cout<<endl;
if(!q.empty()){
q.push(NULL);
}
}
else{
cout<<temp->data<<" ";
if(temp->left){
q.push(temp->left);
}
if(temp->right){
q.push(temp->right);

}
}
}
}
bool search(node*&root,int key){

node*temp=root;

while(temp!=NULL){
if(temp->data==key){
return true;
}
if(temp->data>key){
temp=temp->left;

}
else{
temp=temp->right;
}
}
return false;
}
int main(){
node*root=NULL;
takeinput(root);
cout<<"print bst"<<endl;
levelorder(root);
cout<<"search"<<endl;
int key;
cin>>key;
int a=search(root,key);
cout<<a<<endl;
return 0;
}