-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.C
175 lines (172 loc) · 3.21 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
#include<stdio.h>
#include<conio.h>
#define DMA (struct BST*)malloc(sizeof(struct BST))
struct BST *insert_bst(struct BST *root,int data);
void preorder(struct BST *root); /* Function Prototype */
void inorder(struct BST *root); /* Function Prototype */
void postorder(struct BST *root);/* Function Prototype */
void height_of_tree(struct BST *root);/* Function Prototype */
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
void main() /* Main function definition */
{
int data,ch;
struct BST *root;
root=NULL;
while(1)
{
clrscr();
printf("Menu of Binary Search Tree\n");
printf("1.Create Tree\n");
printf("2.Inorder\n");
printf("3.postorder\n");
printf("4.preorder\n");
printf("5.Height of tree\n");
printf("0.Quit\n");
printf("Enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter data=");
scanf("%d",&data);
root=insert_bst(root,data);break;
case 2:
inorder(root);break;
case 3:
postorder(root);break;
case 4:
preorder(root);break;
case 5:
height_of_tree(root);break;
case 0:
exit(0);
}
}
}
/* Insert_Binary_Search_Tree */
struct BST *insert_bst(struct BST *root,int data)
{
struct BST *newnode,*prevnode,*temp;
newnode=DMA;
newnode->data=data;
newnode->left=NULL;
newnode->right=NULL;
if(root==NULL)
root=newnode;
else
{
temp=root;
while(temp)
{
if(data<=temp->data)
{
prevnode=temp;
temp=temp->left;
}
else
{
prevnode=temp;
temp=temp->right;
}
}
if(data<=prevnode->data)
prevnode->left=newnode;
else
prevnode->right=newnode;
}
return root;
}
/* Inoder */
void inorder(struct BST *root)
{
if(root==NULL)
return;
else
{
inorder(root->left);
printf("%3d",root->data);
inorder(root->right);
}
sleep(2);
}
/* preorder */
void preorder(struct BST *root)
{
if(root==NULL)
return;
else
{
printf("%3d",root->data);
preorder(root->left);
preorder(root->right);
}
sleep(2);
}
/* Postorder */
void postorder(struct BST *root)
{
if(root==NULL)
return;
else
{
postorder(root->left);
postorder(root->right);
printf("%3d",root->data);
}
sleep(2);
}
/* Height_of_Tree */
void height_of_tree(struct BST *root)
{
struct BST *temp1,*temp2,*temp;
int h1,h2,h3;
h1=h2=h3=0;
temp=root;
if(root==NULL)
printf("tree Empty");
else if(root->left==NULL && root->right==NULL)
printf("Height of tree=1");
else
{ temp1=temp->left;
while(temp1)
{
if(temp1->left)
{
h1++;
temp1=temp1->left;
}
else
{
h1++;
temp1=temp1->right;
}
}
temp2=temp->right;
while(temp2)
{
if(temp2->left)
{
h2++;
temp2=temp2->left;
}
else
{
h2++;
temp2=temp2->right;
}
}
if(h1>h2)
h3=h1;
else
h3=h2;
printf("\nHeight of left tree=%d",h1);
printf("\nHeight of right tree=%d",h2);
printf("\nMaximum hight of tree=%d",h3);
}
getch();
}