-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.c
152 lines (129 loc) · 2.85 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
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node{
int info;
struct Node *lchild, *rchild;
};
typedef struct Node* NODE;
NODE root=NULL;
void insert(int ele){
NODE newn=(NODE)malloc(sizeof(struct Node));
newn->info=ele;
if(root==NULL){
root=newn;
return;
}
NODE cur=root,prev;
while(cur!=NULL){
prev=cur;
if(ele < cur->info){
cur=cur->lchild;
}
else
cur=cur->rchild;
}
if(ele < prev->info){
prev->lchild=newn;
}
else
prev->rchild=newn;
}
void preorder(NODE root){
if(root==NULL)
{
printf("Empty tree\n");
return;
}
printf("%d ",root->info);
preorder(root->lchild);
preorder(root->rchild);
}
void inorder(NODE root){
if(root==NULL)
{
printf("Empty tree\n");
return;
}
inorder(root->lchild);
printf("%d ",root->info);
inorder(root->rchild);
}
void postorder(NODE root){
if(root==NULL)
{
printf("Empty tree\n");
return;
}
postorder(root->lchild);
postorder(root->rchild);
printf("%d ",root->info);
}
void search(int ele){
NODE cur=root;
while(cur!=NULL){
if(cur->info == ele)
break;
if(ele < cur->info)
cur=cur->lchild;
else
cur=cur->rchild;
}
if(cur!=NULL)
printf("search successful\n");
else
printf("Element not found\n");
}
void min(NODE root){
if(root==NULL){
printf("Empty tree\n");
return;
}
NODE cur=root;
while(cur->lchild != NULL){
cur=cur->lchild;
}
printf("MIN:%d\n",cur->info);
}
void man(NODE root){
if(root==NULL){
printf("Empty tree\n");
return;
}
NODE cur=root;
while(cur->rchild != NULL){
cur=cur->rchild;
}
printf("MAX:%d\n",cur->info);
}
void display(NODE root,int level){
int i;
if(root){
display(root->rchild,level+1);
printf("\n");
for(i=0;i<level;i++){
printf("\t");
}
printf("%d",root->info);
dispaly(root->lchild,level+1);
}
return;
}
void main(){
int ch,ele,n;
while(1){
printf("1.create\n2.preorder\n3.inorder\n4.postorder\n5.search\n6.min\n7.max\n8.display\n9.display\nenter your choice:");
switch(ch){
case 1:
printf("enter the no. of nodes:");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("enter the element:");
scanf("%d",&ele);
insert(ele);
}
break;
case 2:
}
}
}