-
Notifications
You must be signed in to change notification settings - Fork 256
/
05 Recursive Count and height of tree.c
118 lines (112 loc) · 2.62 KB
/
05 Recursive Count and height of tree.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
#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 rcount(struct Node* root)
{
if (root)
return count(root->lchild) + count(root->rchild) + 1;
return 0;
}
int rheight(struct Node* root)
{
int x = 0, y = 0;
if (root == 0)
return 0;
x = height(root->lchild);
y = height(root->rchild);
if (x > y)
return x + 1;
else
return y + 1;
}
int main()
{
TreeCreate();
printf("total node count is %d \n",rcount(root));
printf("Height of Tree is %d ",rheight(root));
}