-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.c
171 lines (154 loc) · 3.02 KB
/
BinarySearchTree.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
$$$$$$$\ $$\ $$\ $$\ $$$$$$$$\
$$ __$$\ $$$\ $$ | $$ | \__$$ __|
$$ | $$ |$$\ $$\ $$$$\ $$ | $$$$$$\ $$$$$$\ $$ | $$ | $$$$$$\ $$\ $$\
$$$$$$$\ |$$ | $$ | $$ $$\$$ |$$ __$$\ $$ __$$\ $$ | $$ |$$ __$$\ $$ | $$ |
$$ __$$\ $$ | $$ | $$ \$$$$ |$$ / $$ |$$$$$$$$ |$$ | $$ |$$ / $$ |$$ | $$ |
$$ | $$ |$$ | $$ | $$ |\$$$ |$$ | $$ |$$ ____|$$ | $$ |$$ | $$ |$$ | $$ |
$$$$$$$ |\$$$$$$$ |$$\ $$ | \$$ |\$$$$$$ |\$$$$$$$\ $$ | $$ |\$$$$$$ |\$$$$$$$ |
\_______/ \____$$ |$ | \__| \__| \______/ \_______|\__| \__| \______/ \____$$ |
$$\ $$ |\_/ $$\ $$ |
\$$$$$$ | \$$$$$$ |
\______/ \______/
@author Noel Toy
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}node;
node *create(int x)
{
node *p;
p=(node *)malloc(sizeof(struct node));
p->data=x;
p->left=NULL;
p->right=NULL;
return p;
}
void insert(node *t)
{
node *temp;
int x;
printf("\nEnter the key value:");
scanf("%d",&x);
while(t!=NULL)
{
if(x<=t->data)
{
temp=t;
t=t->left;
}
else
{
temp=t;
t=t->right;
}
}
if(x<=temp->data)
{
temp->left=create(x);
}
else
{
temp->right=create(x);
}
}
void inorder(node *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("\nThe Data is:%d",t->data);
inorder(t->right);
}
}
void preorder(node *t)
{
if(t!=NULL)
{
printf("\nThe Data is:%d",t->data);
preorder(t->left);
preorder(t->right);
}
}
/*void search(int x,node *t) Search Without using Recursion
{
while(t!=NULL)
{
if(t->data==x)
{
printf("\nThe Element Found!!");
return;
}
else if(x<=t->data)
{
t=t->left;
}
else
{
t=t->right;
}
}
printf("\nThe Element Not Found");
}*/
void search(int x,node *t) //Search Using Recursion
{
if(t==NULL)
{
printf("\nThe Element Not Found");
return;
}
if(t->data==x)
{
printf("\nThe Element Found!!");
return;
}
else if(x<=t->data)
{
search(x,t->left);
}
else
{
search(x,t->right);
}
}
void main()
{
int ch,x;
node *root;
printf("\nEnter the key value:");
scanf("%d",&x);
root=create(x);
do
{
printf("\n1.Insert New Node\n2.Inorder Traversal\n3.Preorder Traversal\n4.Search\n5.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert(root);
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
printf("\nEnter the key value:");
scanf("%d",&x);
search(x,root);
break;
case 5:
printf("\nExiting....");
break;
default:
printf("\nInvalid Choice!!");
}
}while(ch!=5);
}