-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_count_tree.c
More file actions
78 lines (76 loc) · 1.41 KB
/
Copy pathnode_count_tree.c
File metadata and controls
78 lines (76 loc) · 1.41 KB
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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *l;
struct node *r;
};
struct node *p = NULL;
struct node *getnode(int data)
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->data = data;
p->l = NULL;
p->r = NULL;
};
void preorder(struct node *p)
{
if (p != NULL)
{
printf("%d\n", p->data);
preorder(p->l);
preorder(p->r);
}
}
void postorder(struct node *n1)
{
if (n1 != NULL)
{
postorder(n1->l);
postorder(n1->r);
printf("%d\n", n1->data);
}
}
void inorder(struct node *n1)
{
if (n1 != NULL)
{
inorder(n1->l);
printf("%d\n", n1->data);
inorder(n1->r);
}
}
int countnode(struct node *p)
{
int c = 1;
if (p == NULL)
return 0;
else
{
c += countnode(p->l);
c += countnode(p->r);
return c;
}
}
int main()
{
struct node *n1, *n2, *n3, *n4;
n1 = getnode(2);
n2 = getnode(4);
n3 = getnode(5);
n4 = getnode(6);
n1->l = n2;
n1->r = n3;
n3->l = n4;
printf("Preorder Traversal\n");
preorder(n1);
printf("Postorder Traversal\n");
postorder(n1);
printf("Inorder Traversal\n");
inorder(n1);
int c = countnode(n1);
printf("Number of Nodes in Tree are %d", c);
return 0;
}