-
Notifications
You must be signed in to change notification settings - Fork 1
/
bst.c
104 lines (98 loc) · 2.04 KB
/
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
93
94
95
96
97
98
99
100
101
102
103
104
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *l,*r;
};
struct node *root=NULL;
struct node *create(int k)
{
struct node *n=malloc(sizeof(struct node));
n->data=k;
n->l=NULL;
n->r=NULL;
return n;
}
struct node *insert(struct node *root,int k){
if(root==NULL)
return create(k);
else if(k<root->data)
root->l=insert(root->l,k);
else
root->r=insert(root->r,k);
return root;
}
void preorder(struct node*t)
{
if(t!=NULL){
printf("%d->",t->data);
preorder(t->l);
preorder(t->r);
}
}
void inorder(struct node*t){
if(t!=NULL)
{
inorder(t->l);
printf("%d->",t->data);
inorder(t->r);
}
}
int fdmix(struct node *root){
struct node *t=root;
while(t->l!=NULL)
{
t=t->l;
}
return t->data;
}
int fdmax(struct node *root){
struct node *t=root;
while(t->r!=NULL)
{
t=t->r;
}
return t->data;
}
int search(struct node *root,int k){
if(root==NULL)
return 0;
else{
if(root->data==k)
return 1;
else if(k<root->data)
search(root->l,k);
else if(k>root->data)
search(root->l,k);
}
}
void main(){
int data;
while(1){
scanf("%d",&data);
if(data==-1)
break;
if(root==NULL)
root=insert(root,data);
else
insert(root,data);
}
printf("pre-order : ");
preorder(root);
printf("NULL");
printf("\n");
printf(" In-order : ");
inorder(root);
printf("NULL");
int mix=fdmix(root);
printf("\nMIN-ELEMENT : %d",mix);
int max=fdmax(root);
printf("\nMIN-ELEMENT : %d",max);
printf("\n Enter node to search");
int k;
scanf("%d",&k);
if(search(root,k))
printf("\n SEARCH SUCCESSFULL ELEMENT FOUND");
else
printf("\n SEARCH SUCCESSFULL ELEMENT NOT FOUND");
}