forked from MayukhC99/Algo_Ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Height.c
executable file
·65 lines (58 loc) · 925 Bytes
/
Height.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
#include<stdio.h>
#include<stdlib.h>
struct tree{
int data;
struct tree* left;
struct tree* right;
};
struct tree* create(int dat)
{
struct tree* n=(struct tree*)malloc(sizeof(struct tree));
n->data=dat;
n->left=NULL;
n->right=NULL;
return n;
}
int height(struct tree* n)
{
int l,r;
if(n==NULL)
return 0;
else
{
l=height(n->left);
r=height(n->right);
if(l>r)
return(l+1);
else
return (r+1);
}
}
int count=0;
int node(struct tree* n)
{
if(n==NULL)
return count;
else
{
node(n->left);
node(n->right);
count++;
}
}
void main()
{
struct tree* root;
int ans, exit;
root=create(30);
ans=height(root);
printf("Height of tree after first insertion: %d\n",ans);
root->left=create(20);
root->right=create(40);
root->left->left=create(60);
ans=height(root);
printf("Height of given tree: %d\n",ans);
ans=node(root);
printf("No. of nodes: %d\n",ans);
scanf("%d", exit);
}