-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson-28.cpp
More file actions
72 lines (60 loc) · 1.09 KB
/
Lesson-28.cpp
File metadata and controls
72 lines (60 loc) · 1.09 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node* left;
node*right;
};
node * newnode(int data)
{
node * newnode= new node();
newnode->data=data;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
node * Insert(node* root, int data)
{
if(root==NULL)
{
root=newnode(data);
}
else if(data <=root->data)
{
root->left=Insert(root->left,data);
}
else{
root->right=Insert(root->right,data);
}
return root;
}
bool searchbst(node * root, int d)
{
if(root==NULL)return false;
else if(root->data==d)return true;
else if (root->data>=d)return searchbst(root->left,d);
else return searchbst(root->right,d);
}
int main()
{
node *root=NULL;
while(1)
{
cout<<"1.add data"<<endl<<"2. search tree"<<endl;
int h;
cin>>h;
if(h==1)
{
int d;
cin>>d;
root=Insert(root,d);
}
else if(h==2)
{
int d;
cin>>d;
bool state=searchbst(root,d);
cout<<state<<endl;
}
}
}