-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSearchInBST.cpp
More file actions
52 lines (46 loc) · 1013 Bytes
/
SearchInBST.cpp
File metadata and controls
52 lines (46 loc) · 1013 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
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
node *prev, *next;
};
node *nodeinsert(int value) {
node *temp = new node();
temp->data = value;
temp->prev = temp->next = NULL;
return temp;
}
node *addnode(node *node, int data) {
if (node == NULL)
return nodeinsert(data);
if (data < node->data)
node->prev = addnode(node->prev, data);
else
node->next = addnode(node->next, data);
return node;
}
// Search In BST
node* search(node *node,int key){
if(node== NULL)return NULL;
if(node->data == key)return node;
if(node->data > key)return search(node->prev,key);
return search(node->next,key);
}
int main() {
node *s = NULL;
cout<<"Enter the size ";
int size;
cin>>size;
cout<<"Enter the values";
for (int i = 0; i < size; i++)
{
int n;
cin>>n;
s=addnode(s,n);
}
int num;
cout<<"Input the value to be Searched for "<<endl;
cin>>num;
if(search(s,num)==NULL)cout<<"No Such Node Found";
else cout<<"Node Found";
}