-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.c
91 lines (90 loc) · 1.6 KB
/
t.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
#include<stdio.h>
#include<stdlib.h>
typedef struct tree
{
int info;
struct tree *left;
struct tree *right;
}node;
node * create(int);
void insert(node *);
int search(int);
node *root=NULL;
void main()
{
int c,ele,n;
node *h,*m;
printf("Enter the size of the tree\n");
scanf("%d",&n);
h=create(n);
printf("created...\n");
do
{
printf("\t\t\tMAIN MENU\n");
printf("1.insert\n");
printf("2.search\n");
printf("3.delete\n");
printf("enter your choice\n");
scanf("%d",&c);
switch(c)
{
case 1:printf("enter the element to be inserted\n");
scanf("%d",&m);
insert(m);
break;
case 2:printf("enter element to be searched\n");
scanf("%d",&ele);
search(ele);
break;
}
}
while(c!=3);
}
node * create(int n)
{
int ele,i;
node *m;
for(i=1;i<=n;i++)
{
scanf("%d",&ele);
m=(node*)malloc(sizeof(node));
m->info=ele;
m->left=NULL;
m->right=NULL;
if(root==NULL)
root=m;
else
insert(m);
}
}
void insert(node *m)
{
node *p,*q;
int ele;
p=q=root;
while(q!=NULL)
{
p=q;
if(ele<q->info)
q=q->left;
else
q=q->right;
}
if(p->info>ele)
p->left=m;
else
p->right=m;
}
int search(int ele)
{
node *p;
p=root;
while(p!=NULL && p->info!=ele)
{
if(ele>p->info)
p=p->right;
else
p=p->left;
return p->info;
}
}