-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
executable file
·54 lines (54 loc) · 828 Bytes
/
bst.cpp
File metadata and controls
executable file
·54 lines (54 loc) · 828 Bytes
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
51
52
53
54
#include <stdio.h>
#include <iostream>
struct bst
{
int data;
bst *left;
bst *right;
};
bst *root=NULL;
bst *new_node(int data)
{
bst *newnode=new bst();
newnode->data=data;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
void insert(int data)
{
if(root==NULL)
{
root=new_node(data);
}
else if(data<=(root->data))
{
root->left=insert(root->left,data);
}
else
root->right=insert(root->right,data);
}
bool search(bst* root,int data)
{
if(root==NULL)
return false;
else if(root->data==data)
return true;
else if(data<=root->data)
return search(root->left,data);
else
return search(root->right,data);
}
int main()
{
int n;
insert(15);
insert(10);
insert(20);
std::cin>>n;
if(search(root,n)==true)
std::cout<<"Number Found\n";
else
std::cout<<"Number not found\n";
return 0;
}