-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.c
190 lines (179 loc) · 3.59 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<stdio.h>
#include<conio.h>
struct Node
{
int value;
struct Node *left;
struct Node *right;
} *root = NULL;
struct Node* insert(struct Node *r,int data)
{
if (r==NULL)
{
r=(struct Node*)malloc(sizeof(struct Node));
r->value=data;
r->left=NULL;
r->right=NULL;
}
else if(data<r->value)
r->left=insert(r->left,data);
else
r->right=insert(r->right,data);
return r;
}
void inOrder(struct Node *r)
{
if(r!=NULL)
{
inOrder(r->left);
printf("%d ",r->value);
inOrder(r->right);
}
}
void preOrder(struct Node *r)
{
if(r!=NULL)
{
printf("%d ",r->value);
preOrder(r->left);
preOrder(r->right);
}
}
void postOrder(struct Node *r)
{
if(r!=NULL)
{
postOrder(r->left);
postOrder(r->right);
printf("%d ",r->value);
}
}
void search(struct Node *r,int data)
{
if(r==NULL)
printf("\nElement does not exist.");
else if(data==r->value)
printf("\nElement is present in the tree.");
else if(data < r->value)
search(r->left,data);
else
search(r->right,data);
}
struct Node *findMin(struct Node *r)
{
if(r==NULL)
return 0;
else if(r->left==NULL)
return r;
else
return(findMin(r->left));
}
struct Node *delete(struct Node *r,int data)
{
struct Node *temp;
if(r==NULL)
{
printf("\nTree is empty");
return 0;
}
else if(data < r->value)
r->left=delete(r->left,data);
else if(data > r->value)
r->right=delete(r->right,data);
else if(r->left!=NULL && r->right!=NULL)
{
temp=findMin(r->right);
r->value=temp->value;
r->right=delete(r->right,r->value);
}
else
{
temp=r;
if(r->left==NULL)
r=r->right;
else if (r->right==NULL)
r=r->left;
free(temp);
}
return(r);
}
int count(struct Node *r)
{
if(r==NULL)
return 0;
return (1 + count(r->left) + count(r->right));
}
int height(struct Node* r)
{
int hgt_left,hgt_right;
if(r==NULL)
return 0;
hgt_left = height(r->left);
hgt_right = height(r->right);
if(hgt_left>hgt_right)
return (1+hgt_left);
return (1+hgt_right);
}
void mirrorImg(struct Node* r)
{
struct Node* temp;
if(r!=NULL)
{
mirrorImg(r->left);
mirrorImg(r->right);
temp = r->left;
r->left =r->right;
r->right = temp;
}
}
void main()
{
int i,n,data,ch;
root=NULL;
//clrscr();
do {
printf("\nEnter a choice\n");
printf("\nBinary Tree Traversal\n");
printf("1] Enter a node\n2] Preorder Traversal\n3] Inorder Traversal\n4] Postorder Traversal\n5] Search\n6] Delete Node\n7] Count\n8] Height\n9]Mirror image\n10] Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data: ");
scanf("%d",&data);
root=insert(root,data);
break;
case 2: printf("\nPreorder Traversal\n");
preOrder(root);
printf("\n");
break;
case 3: printf("\nInorder Traversal\n");
inOrder(root);
printf("\n");
break;
case 4: printf("\nPostorder Traversal\n");
postOrder(root);
printf("\n");
break;
case 5: printf("Enter the element you want to search: ");
scanf("%d",&data);
search(root,data);
break;
case 6: printf("Enter the element you want to delete: ");
scanf("%d",&data);
delete(root,data);
break;
case 7: printf("Count of the nodes is: %d",count(root));
break;
case 8: printf("Height of the tree is: %d",height(root));
break;
case 9: printf("\n Mirror image of Binary tree is:");
mirrorImg(root);
inOrder(root);
break;
case 10: exit(0);
break;
default: printf("\nWrong choice entered.");
}
} while(ch!=10);
getch();
}