-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst2.cpp
89 lines (82 loc) · 1.37 KB
/
bst2.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
struct bstNode
{
int data;
bstNode *left;
bstNode *right;
};
bstNode *root=NULL;
bstNode *createNode(int data){
bstNode *temp=new bstNode();
temp->data=data;
temp->left=NULL;
temp->right=NULL;
return temp;
}
bstNode *Insert(bstNode* root,int data){
bstNode* temp;
temp=createNode(data);
if(root==NULL){
cout<<"1-->";
root=temp;
return root;
}
else if(root->data>=data){
cout<<"2--->";
root->left=temp;
root->left=Insert(root->left,data);
}
else{
cout<<"3---->";
root->right=temp;
root->right=Insert(root->right,data);
}
return root;
}
bool Search(bstNode* root,int data){
if(root==NULL){
//cout<<" 11";
return false;
}
else if(root->data==data){
//cout<<" 12";
return true;
}
else if(root->data>=data){
//cout<<" 13";
return Search(root->left,data);
}
else{
//cout<<" 14";
return Search(root->right,data);
}
}
int main(){
int n;
while(1){
cout<<"1.for enter data::\n2.for search data::\n";
cin>>n;
switch(n){
case 1:
int data;
cout<<"Enter data\n";
cin>>data;
root=Insert(data);
break;
case 2:
int find;
cout<<"Enter data to find::";
cin>>find;
if(Search(root,find)){
cout<<"data found::\n";
}
else if(!Search(root,find)){
cout<<"sorry data is not available::\n";
}
break;
default :
cout<<"sorry invalid choice\n" ;
}
}
}