forked from harikrishnan669/DS_Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarysearchtree_search.c
62 lines (62 loc) · 993 Bytes
/
binarysearchtree_search.c
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node1
{
int data;
struct node *left;
struct node *right;
}node;
int key;
int flag=0;
node *create()
{
node *ptr;
int num;
printf("Enter the value(Input -1 if there is no data):");
scanf("%d",&num);
if(num==-1)
{
return NULL;
}
ptr=(node*)malloc(sizeof(node));
ptr->data=num;
if(num<ptr->data)
{
ptr->left=create();
}
else
{
ptr->right=create();
}
return ptr;
}
void search(node *ptr)
{
if(ptr!=NULL)
{
search(ptr->left);
if(ptr->data==key)
{
flag=1;
return flag;
}
search(ptr->right);
}
}
int main()
{
int choice;
node *root;
root=create();
printf("Enter the element to be searched:");
scanf("%d",&key);
search(root);
if(flag==1)
{
printf("The element is present in the tree");
}
else
{
printf("The element is not present in the tree");
}
}