Skip to content

Commit 20ed2a8

Browse files
authored
Add files via upload
1 parent 508486f commit 20ed2a8

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

Tree/Basic/08 leaf,D1,D2 node count.c

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct Node // this is tree node
4+
{
5+
struct Node* lchild; // lefe child as node in tree
6+
int data; // data element
7+
struct Node* rchild; // right child as node in tree
8+
};
9+
// we are implementing tree with the help of queue
10+
struct Queue
11+
{
12+
int size; // size of queue
13+
int front; // front of queue // using for del
14+
int rear; // last of queue used for insertion
15+
struct Node** Q; // this should be an aray of nodes
16+
};
17+
void create(struct Queue* q, int size) // creating a queue // passing pointer and the size of queue
18+
{
19+
q->size = size; // size of queue
20+
q->front = 0; // make front and rear as null intially
21+
q->rear = 0;
22+
q->Q = (struct Node**)malloc(q->size * sizeof(struct Node*)); // allocating space in heap
23+
}
24+
void enqueue(struct Queue* q, struct Node* x) // passing queue as pointer
25+
{
26+
if ((q->rear + 1) % q->size == q->front) // checking wheather queue is full
27+
printf("Queue if FULL");
28+
else
29+
{
30+
q->rear = (q->rear + 1) % q->size;
31+
q->Q[q->rear] = x; // push the data
32+
}
33+
}
34+
struct Node* dequeue(struct Queue* q)
35+
{
36+
struct Node* x = NULL;
37+
if (q->front == q->rear)
38+
return NULL;
39+
else
40+
{
41+
q->front = (q->front + 1) % q->size;
42+
x = q->Q[q->front];
43+
return x;
44+
}
45+
}
46+
int isEmpty(struct Queue q)
47+
{
48+
return q.front == q.rear;
49+
}
50+
struct Node* root = NULL;
51+
void TreeCreate()
52+
{
53+
struct Node* t, * p;
54+
int x;// val of root
55+
struct Queue q;
56+
create(&q, 20); // size of a queue should be bigger
57+
root = (struct Node*)malloc(sizeof(struct Node));
58+
printf("Enter a Root Value :\n");
59+
scanf_s("%d", &x);
60+
root->data = x;
61+
root->lchild = NULL;
62+
root->rchild = NULL;
63+
enqueue(&q, root);
64+
while (!isEmpty(q))
65+
{
66+
p = dequeue(&q);
67+
printf("Enter Left Child of %d :", p->data);
68+
scanf_s("%d", &x);
69+
if (x != -1)
70+
71+
{
72+
t = (struct Node*)malloc(sizeof(struct Node));
73+
t->data = x;
74+
t->lchild = t->rchild = NULL;
75+
p->lchild = t;
76+
enqueue(&q, t);
77+
}
78+
printf("Enter Right Child of %d:", p->data);
79+
scanf_s("%d", &x);
80+
if (x != -1)
81+
{
82+
t = (struct Node*)malloc(sizeof(struct Node));
83+
t->data = x;
84+
t->lchild = t->rchild = NULL;
85+
p->rchild = t;
86+
enqueue(&q, t);
87+
}
88+
}
89+
90+
}
91+
int leafcount(struct Node * root)
92+
{
93+
int x, y;
94+
if (root != NULL)
95+
{
96+
x = leafcount(root->lchild);
97+
y = leafcount(root->rchild);
98+
if (root->lchild == NULL && root->rchild == NULL) // condition for leaf node
99+
return x + y + 1;
100+
else
101+
return x + y;
102+
}
103+
return 0;
104+
}
105+
int deg2count(struct Node* root)
106+
{
107+
int x, y;
108+
if (root != NULL)
109+
{
110+
x = deg2count(root->lchild);
111+
y = deg2count(root->rchild);
112+
if (root->lchild != NULL && root->rchild != NULL) // condition for node with deg 1
113+
return x + y + 1;
114+
else
115+
return x + y;
116+
}
117+
return 0;
118+
}
119+
int deg_1_2count(struct Node* root)
120+
{
121+
int x, y;
122+
if (root != NULL)
123+
{
124+
x = deg_1_2count(root->lchild);
125+
y = deg_1_2count(root->rchild);
126+
if (root->lchild != NULL || root->rchild != NULL) // condition for node having deg 1 as well as 2
127+
return x + y + 1;
128+
else
129+
return x + y;
130+
}
131+
return 0;
132+
}
133+
int deg1count(struct Node* root)
134+
{
135+
int x, y;
136+
if (root != NULL)
137+
{
138+
x = deg1count(root->lchild);
139+
y = deg1count(root->rchild);
140+
if (root->lchild != NULL ^ root->rchild != NULL) // condition for node having deg 1
141+
return x + y + 1;
142+
else
143+
return x + y;
144+
}
145+
return 0;
146+
}
147+
148+
int main()
149+
{
150+
TreeCreate();
151+
printf("total no of leaf node is %d \n",leafcount(root));
152+
printf("total no of node with deg 2 is %d \n", deg2count(root));
153+
printf("total no of node with deg 2 as well as 1 is %d \n", deg_1_2count(root));
154+
printf("total no of node with deg 1 is %d \n", deg1count(root));
155+
156+
}

0 commit comments

Comments
 (0)