C Program for Binary Search Tree of Characters and Recursively in Postorder

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