-
Notifications
You must be signed in to change notification settings - Fork 86
/
Insertion&Searching_BST.c
92 lines (88 loc) · 1.89 KB
/
Insertion&Searching_BST.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
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
90
91
92
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
struct node * createNode(int value)
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
n->data=value;
n->left=NULL;
n->right=NULL;
return n;
}
struct node * search(struct node *root,int key)
{
while(root!=NULL)
{
if(root->data ==key)
{
return root;
}
else if(root->data>key)
{
root=root->left;
}
else{
root=root->right;
}
}
return NULL;
}
void insert(struct node *root, int key)
{
struct node *prev=NULL;
while(root!=NULL)
{
prev =root;
if(key==root->data)
{
printf("The element %d cannot be inserted in the Binary Search Tree\n",key);
return ;
}
else if(key<root->data)
{
root=root->left;
}
else{
root=root->right;
}
}
struct node *new=createNode(key);
if(key<prev->data)
{
prev->left=new;
}else{
prev->right=new;
}
}
int main()
{
struct node *p1=createNode(5);
struct node *p2=createNode(3);
struct node *p3=createNode(6);
struct node *p4=createNode(1);
struct node *p5=createNode(4);
p1->left=p2;
p1->right=p3;
p2->left=p4;
p2->right=p5;
printf("Enter the element to be inserted:\n");
int num;
scanf("%d",&num);
insert(p1,num);
printf("Enter the element to be searched:\n");
int e;
scanf("%d",&e);
struct node *x= search(p1,e);
if(x!=NULL)
{
printf("The element %d has been found in the Binary Search Tree\n",x->data);
}else{
printf("The element is not found in the Binary Search Tree\n");
}
return 0;
}