-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinaryTree2.c
executable file
·215 lines (211 loc) · 5.17 KB
/
binaryTree2.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// binaryTree2.c
// AnishC++
//
// Created by Anish Mookherjee on 04/11/19.
// Copyright © 2019 Anish Mookherjee. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
}*tree;
void insert(int d){
struct node *p,*t,*curr;
t=(struct node*)malloc(sizeof(struct node));
t->data=d;
t->right=t->left=NULL;
if(tree==NULL){
tree=t;
}
else{
curr=tree;
while(curr){
p=curr;
if(t->data<curr->data){
curr=curr->left;
}
else{
curr=curr->right;
}
}
if(t->data<p->data){
p->left=t;
}
else{
p->right=t;
}
}
}
struct node *search(struct node *tree,int value){
if(tree->data==value || tree==NULL)
return tree;
else{
if(value<tree->data){
return search(tree->left,value);
}
else
return search(tree->right,value);
}
}
void preordert(struct node *tree)
{
if(tree!=NULL)
{
printf("%d",tree->data);
preordert(tree->left);
preordert(tree->right);
}
}
void inorder(struct node *tree)
{
if(tree!=NULL){
inorder(tree->left);
printf("%d",tree->data);
inorder(tree->right);
}
}
void postordert(struct node *tree){
if(tree!=NULL){
postordert(tree->left);
postordert(tree->right);
printf("%d",tree->data);
}
}
int height(struct node *tree){
int leftheight,rightheight;
if(tree==NULL){
return 0;
}
else{
leftheight=height(tree->left);
rightheight=height(tree->right);
if(leftheight>rightheight)
return leftheight+1;
else
return rightheight+1;
}
}
int totalnodes(struct node *tree){
if (tree==NULL)
return 0;
else{
return (totalnodes(tree->left)+totalnodes(tree->right)+1);
}
}
int externalnodes(struct node *tree){
if(tree==NULL)
return 0;
else if(tree->left==NULL && tree->right==NULL){
return 1;
}
else
return (externalnodes(tree->left)+externalnodes(tree->right));
}
int internalnodes(struct node *tree){
if(tree==NULL || (tree->left==NULL && tree->right==NULL))
{
return 0;
}
else{
return internalnodes(tree->left)+internalnodes(tree->right)+1;
}
}
void deletetree(struct node *tree){
if(tree!=NULL){
deletetree(tree->left);
deletetree(tree->right);
free(tree);
}
}
struct node *largest(struct node *tree){
if(tree==NULL || tree->right==NULL)
return tree;
else
return largest(tree->right);
}
struct node *smallest(struct node *tree){
if(tree==NULL || tree->left==NULL)
return tree;
else
return smallest(tree->left);
}
int main(){
int option,value;
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
do{
printf("1 Insert element\n");
printf("2 Search element\n");
printf("3 Height\n");
printf("4 Total nodes\n");
printf("5 External nodes\n");
printf("6 Internal nodes\n");
printf("7 Delete tree\n");
printf("8 preorder \n");
printf("9 inorder\n");
printf("10 postorder\n");
printf("11 largest\n");
printf("12 smallest\n");
printf("13 exit\n");
scanf("%d",&option);
switch (option) {
case 1:
printf("enter the element");
scanf("%d",&value);
insert(value);
break;
case 2:
printf("enter the element you wnt to search");
scanf("%d",&value);
if(search(tree,value)==NULL)
printf("NOT FOUND\n");
else
printf("FOUND\n");
break;
case 3:
printf("height of the tree:%d\n",height(tree));
break;
case 4:
printf("total nodes: %d\n",totalnodes(tree));
break;
case 5:
printf("external nodes: %d\n",externalnodes(tree));
break;
case 6:
printf("internal nodes: %d\n",internalnodes(tree));
break;
case 7:
printf("delete tree:\n");
deletetree(tree);
break;
case 8:
printf("preorder:\n");
preordert(tree);
break;
case 9:
printf("inorder:\n");
inorder(tree);
break;
case 10:
printf("postorder:\n");
postordert(tree);
break;
case 11:
p=largest(tree);
printf("largest element is:%d\n",p->data);
break;
case 12:
p=smallest(tree);
printf("smallest element is:%d\n",p->data);
break;
default:
printf("EXIT");
break;
}
}while (option!=13);
return 0;
}