forked from Harsh-jot/HacktoberFest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarysearchtree
50 lines (42 loc) · 1.01 KB
/
binarysearchtree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//author : raj patil
//जय हिंद, जय महाराष्ट्र !
//BST-Implementaion
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* right;
Node* left;
};
Node* newNode(int x){
Node* temp=new Node();
temp->data=x;
temp->right=NULL;
temp->left=NULL;
return temp;
}
Node* Insert(Node* root, int x){
if(root==NULL) root=newNode(x);
else if(x<=root->data) root->left=Insert(root->left,x);
else root->right=Insert(root->right,x);
return root;
}
bool search(Node* root, int x){
if(root=NULL) return false;
else if(root->data==x) return true;
else if(x<=root->data) return search(root->left,x);
else return search(root->right,x);
}
int main(){
Node* root=NULL;
root = Insert(root,15);
root = Insert(root,10);
root = Insert(root,20);
root = Insert(root,25);
root = Insert(root,8);
root = Insert(root,12);
int n;
cin>>n;
if(search(root,n)==true) cout<<"Number Present";
else cout<<"Not Present";
}