-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy path08 leaf,D1,D2 node count.c
156 lines (152 loc) · 3.64 KB
/
08 leaf,D1,D2 node count.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
#include <stdio.h>
#include <stdlib.h>
struct Node // this is tree node
{
struct Node* lchild; // lefe child as node in tree
int data; // data element
struct Node* rchild; // right child as node in tree
};
// we are implementing tree with the help of queue
struct Queue
{
int size; // size of queue
int front; // front of queue // using for del
int rear; // last of queue used for insertion
struct Node** Q; // this should be an aray of nodes
};
void create(struct Queue* q, int size) // creating a queue // passing pointer and the size of queue
{
q->size = size; // size of queue
q->front = 0; // make front and rear as null intially
q->rear = 0;
q->Q = (struct Node**)malloc(q->size * sizeof(struct Node*)); // allocating space in heap
}
void enqueue(struct Queue* q, struct Node* x) // passing queue as pointer
{
if ((q->rear + 1) % q->size == q->front) // checking wheather queue is full
printf("Queue if FULL");
else
{
q->rear = (q->rear + 1) % q->size;
q->Q[q->rear] = x; // push the data
}
}
struct Node* dequeue(struct Queue* q)
{
struct Node* x = NULL;
if (q->front == q->rear)
return NULL;
else
{
q->front = (q->front + 1) % q->size;
x = q->Q[q->front];
return x;
}
}
int isEmpty(struct Queue q)
{
return q.front == q.rear;
}
struct Node* root = NULL;
void TreeCreate()
{
struct Node* t, * p;
int x;// val of root
struct Queue q;
create(&q, 20); // size of a queue should be bigger
root = (struct Node*)malloc(sizeof(struct Node));
printf("Enter a Root Value :\n");
scanf_s("%d", &x);
root->data = x;
root->lchild = NULL;
root->rchild = NULL;
enqueue(&q, root);
while (!isEmpty(q))
{
p = dequeue(&q);
printf("Enter Left Child of %d :", p->data);
scanf_s("%d", &x);
if (x != -1)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = x;
t->lchild = t->rchild = NULL;
p->lchild = t;
enqueue(&q, t);
}
printf("Enter Right Child of %d:", p->data);
scanf_s("%d", &x);
if (x != -1)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = x;
t->lchild = t->rchild = NULL;
p->rchild = t;
enqueue(&q, t);
}
}
}
int leafcount(struct Node * root)
{
int x, y;
if (root != NULL)
{
x = leafcount(root->lchild);
y = leafcount(root->rchild);
if (root->lchild == NULL && root->rchild == NULL) // condition for leaf node
return x + y + 1;
else
return x + y;
}
return 0;
}
int deg2count(struct Node* root)
{
int x, y;
if (root != NULL)
{
x = deg2count(root->lchild);
y = deg2count(root->rchild);
if (root->lchild != NULL && root->rchild != NULL) // condition for node with deg 1
return x + y + 1;
else
return x + y;
}
return 0;
}
int deg_1_2count(struct Node* root)
{
int x, y;
if (root != NULL)
{
x = deg_1_2count(root->lchild);
y = deg_1_2count(root->rchild);
if (root->lchild != NULL || root->rchild != NULL) // condition for node having deg 1 as well as 2
return x + y + 1;
else
return x + y;
}
return 0;
}
int deg1count(struct Node* root)
{
int x, y;
if (root != NULL)
{
x = deg1count(root->lchild);
y = deg1count(root->rchild);
if (root->lchild != NULL ^ root->rchild != NULL) // condition for node having deg 1
return x + y + 1;
else
return x + y;
}
return 0;
}
int main()
{
TreeCreate();
printf("total no of leaf node is %d \n",leafcount(root));
printf("total no of node with deg 2 is %d \n", deg2count(root));
printf("total no of node with deg 2 as well as 1 is %d \n", deg_1_2count(root));
printf("total no of node with deg 1 is %d \n", deg1count(root));
}