File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class Node {
5
+ public:
6
+ int data;
7
+ Node* left;
8
+ Node* right;
9
+ };
10
+
11
+ Node* root=NULL ;
12
+
13
+ // return pointer to newly created node
14
+ Node* MakeNode (int x){
15
+ Node* newNode=new Node ();
16
+ newNode->data =x;
17
+ newNode->left =newNode->right =NULL ;
18
+ return newNode;
19
+ }
20
+
21
+ // for inserting value in BST and will return address of root note
22
+ Node* Insert (Node* root,int data){
23
+ if (root==NULL )
24
+ root=MakeNode (data);
25
+ // if value is lesser than root insert at left.
26
+ else if (data <= root->data )
27
+ root->left = Insert (root->left ,data);
28
+ // else insert at right
29
+ else
30
+ root->right = Insert (root->right ,data);
31
+
32
+ return root;
33
+ }
34
+ // For searching the element return the T/F value
35
+ bool Search (Node* root,int data){
36
+ if (root==NULL )
37
+ return false ;
38
+ else if (root->data ==data)
39
+ return true ;
40
+ else if (data<=root->data )
41
+ return Search (root->left ,data);
42
+ else
43
+ return Search (root->right ,data);
44
+ }
45
+ int main (){
46
+ root=Insert (root,63 );
47
+ root=Insert (root,11 );
48
+ root=Insert (root,43 );
49
+ root=Insert (root,52 );
50
+ root=Insert (root,19 );
51
+ root=Insert (root,17 );
52
+
53
+ int key;
54
+ cout<<" Enter value to be searched:" <<endl;
55
+ cin>>key;
56
+
57
+ if (Search (root,key))
58
+ cout<<" Found!" <<endl;
59
+
60
+ else cout<<" Not Found!" <<endl;
61
+ }
You can’t perform that action at this time.
0 commit comments