C Program for Binary Search Tree of integers and Non-Recursively in Inorder

Estudies4you

1 - - #include<stdio.h>
2 #include<stdlib.h>
3 struct node
4 {
5 int data;
6 struct node *left;
7 struct node *right;
8 }*ptr;
9 struct node *searched,*max,*min;
10 struct node *insert(struct node *ptr,int ele);
11 struct node *deleted(struct node *ptr,int ele);
12 struct node *findmin(struct node *ptr);
13 struct node *findmax(struct node *ptr);
14 struct node *search(struct node *ptr,int ele);
15 void inorder(struct node *ptr);
16 void preorder(struct node *ptr);
17 void postorder(struct node *ptr);
18 void main()
19 {
20 int ch,ele;
21 ptr=NULL;
22 printf("\n ----Main Menu---");
23 printf("\n1.Insert \n2.Delete\n3.Search\n4.Inoredr\n5.Preorder\n6.Postorder\n7.Findmin\n8.Findmax\n9.Exit");
24 while(1)
25 {
26 printf("\nEnter ur choice : ");
27 scanf("%d",&ch);
28 switch(ch)
29 {
30 case 1:
31 printf("\nEnter an elemnet to insert : ");
32 scanf("%d",&ele);
33 ptr=insert(ptr,ele);
34 break;
35 case 2: printf("\nEnter element to delete : ");
36 scanf("%d",&ele);
37 ptr=deleted(ptr,ele);
38 printf("\n Deleted Successfully");
39 break;
40 case 3: printf("\nEnter a element to search : ");
41 scanf("%d",&ele);
42 searched=search(ptr,ele);
43 if(searched==NULL)
44 printf("\nElement is not found");
45 else
46 printf("\nElement is found");
47 break;
48 case 4:
49 inorder(ptr);
50 break;
51 case 5: preorder(ptr);
52 break;
53 case 6: postorder(ptr);
54 break;
55 case 7: min=findmin(ptr);
56 if(min!=NULL)
57 printf("\n Minimum value is %d",min->data);
58 else
59 printf("\nTree is empty");
60 break;
61 case 8: max=findmax(ptr);
62 if(max!=NULL)
63 printf("\n Maximum value is %d",max->data);
64 else
65 printf("\nTree is empty");
66 break;
67 case 9: exit(0);
68 default: printf("\n Wrong choice");
69 }
70 }
71 }
72 struct node *insert(struct node *ptr,int ele)
73 {
74 if(ptr==NULL)
75 {
76 ptr=(struct node *)malloc(sizeof(struct node));
77 ptr->data=ele;
78 ptr->left=ptr->right=NULL;
79 }
80 else if(ele<ptr->data)
81 ptr->left=insert(ptr->left,ele);
82 else if(ele>ptr->data)
83 ptr->right=insert(ptr->right,ele);
84 else
85 printf("\nElement exists");
86 return(ptr);
87 }
88 struct node *deleted(struct node *ptr,int ele)
89 {
90 struct node *tempmin,*temp;
91 if(ptr==NULL)
92 {
93 printf("\nTree is Empty");
94 return NULL;
95 }
96 else if(ele<ptr->data)
97 ptr->left=deleted(ptr->left,ele);
98 else if(ele>ptr->data)
99 ptr->right=deleted(ptr->right,ele);
100 else
101 {
102 if(ptr->left!=NULL && ptr->right!=NULL)
103 {
104 tempmin=findmin(ptr->right);
105 ptr->data=tempmin->data;
106 ptr->right=deleted(ptr->right,tempmin->data);
107 free(tempmin);
108 }
109 else
110 {
111 temp=ptr;
112 if(ptr->left==NULL)
113 ptr=ptr->right;
114 else if(ptr->right==NULL)
115 {
116 ptr=ptr->left;
117 free(temp);
118 }
119 }
120 }
121 return(ptr);
122 }
123 struct node *findmin(struct node *ptr)
124 {
125 if(ptr!=NULL)
126 {
127 while(ptr->left!=NULL)
128 ptr=ptr->left;
129 return(ptr);
130 }
131 else
132 return(NULL);
133 }
134 struct node *findmax(struct node *ptr)
135 {
136 if(ptr!=NULL)
137 {
138 while(ptr->right!=NULL)
139 ptr=ptr->right;
140 return(ptr);
141 }
142 else
143 return(NULL);
144 }
145 struct node *search(struct node *ptr,int ele)
146 {
147 if(ptr==NULL)
148 return NULL;
149 else
150 {
151 if(ele<ptr->data)
152 return(search(ptr->left,ele));
153 else if(ele>ptr->data)
154 return(search(ptr->right,ele));
155 else if(ele==ptr->data)
156 return(ptr);
157 else
158 return(NULL);
159 }
160 }
161 void inorder(struct node *ptr)
162 {
163 struct node *stack[50];
164 int top=-1;
165 if(ptr==NULL)
166 return;
167 else
168 {
169 while(ptr!=NULL||top>=0)
170 {
171 if(ptr!=NULL)
172 {
173 top++;
174 stack[top]=ptr;
175 ptr=ptr->left;
176 }
177 else
178 {
179 ptr=stack[top];
180 printf("%d ",ptr->data);
181 top--;
182 ptr=ptr->right;
183 }
184 }
185 }
186 }
187 void preorder(struct node *ptr)
188 {
189 struct node *stack[50];
190 int top=-1;
191 if(ptr==NULL)
192 return;
193 else
194 {
195 while(ptr!=NULL||top>=0)
196 {
197 if(ptr!=NULL)
198 {
199 printf("%d ",ptr->data);
200 top++;
201 stack[top]=ptr;
202 ptr=ptr->left;
203 }
204 else
205 {
206 ptr=stack[top];
207 top--;
208 ptr=ptr->right;
209 }
210 }
211 }
212 }
213 void postorder(struct node *ptr)
214 {
215 int top=-1,v1=-1;
216 struct node *stack[25],*s1[25],*ptr1;
217 if(ptr==NULL)
218 return;
219 else
220 {
221 stack[++top]=ptr;
222 while(top>=0)
223 {
224 ptr=stack[top--];
225 s1[++v1]=ptr;
226 if(ptr->left!=NULL)
227 stack[++top]=ptr->left;
228 if(ptr->right!=NULL)
229 stack[++top]=ptr->right;
230 }
231 while(v1!=-1)
232 {
233 ptr=s1[v1--];
234 printf("%d ",ptr->data);
235 }
236 }
237 }
OutPut:
1.Insert
2.Delete
3.Search
4.Inoredr
5.Preorder
6.Postorder
7.Findmin
8.Findmax
9.Exit
Enter ur choice : 1
Enter an elemnet to insert : 5
Enter ur choice : 1
Enter an elemnet to insert : 3
Enter ur choice : 1
Enter an elemnet to insert : 2
Enter ur choice : 1
Enter an elemnet to insert : 4
Enter ur choice : 1
Enter an elemnet to insert : 7
Enter ur choice : 1
Enter an elemnet to insert : 6
Enter ur choice : 1
Enter an elemnet to insert : 8
Enter ur choice : 4
2 3 4 5 6 7 8
Enter ur choice : 5
5 3 2 4 7 6 8
Enter ur choice : 6
2 4 3 6 8 7 5
Enter ur choice : 2
Enter element to delete : 5
Deleted Successfully
Enter ur choice : 4
2 3 4 6 7 8
Enter ur choice : 5
6 3 2 4 7 8
Enter ur choice : 6
2 4 3 8 7 6
Enter ur choice : 7
Minimum value is 2
Enter ur choice : 8
Maximum value is 8
Enter ur choice : 3
Enter a element to search : 6
Element is found
Enter ur choice : 9

To Top