-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplete Binary Tree – Child Sum Property
152 lines (134 loc) · 3.45 KB
/
Complete Binary Tree – Child Sum Property
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
Write a program to build a complete binary tree and to check if it satisfies the child sum property.
Child Sum Property: For every non-leaf node, data value must be equal to sum of data values in left and right children.
#include <stdio.h>
#include <stdlib.h>
struct tree
{
int data;
struct tree *right,*left;
};
struct queue
{
int front, rear;
int size;
struct tree* *array;
};
struct tree* newNode(int data);
struct queue* createQueue(int size);
void enqueue(struct tree *root, struct queue* queue);
struct tree* dequeue(struct queue* queue);
void insert(struct tree **root, int data, struct queue* queue);
void levelOrder(struct tree* root);
int isSumProperty(struct tree* node);
int main()
{
struct tree* root = NULL;
int data;
struct queue* queue = createQueue(50);
int i,n;
printf("Enter the number of elements in the tree\n");
scanf("%d",&n);
printf("Enter the elements in the tree\n");
for(i = 1; i <= n; ++i)
{
scanf("%d",&data);
insert(&root, data, queue);
}
printf("The elements in the tree in level order are");
levelOrder(root);
printf("\n");
if(isSumProperty(root))
printf("The tree satisfies the children sum property ");
else
printf("The tree does not satisfy the children sum property ");
return 0;
}
struct tree* newNode(int data)
{
struct tree* nn = (struct tree*)malloc(sizeof(struct tree));
nn->data = data;
nn->left = NULL;
nn->right = NULL;
return nn;
}
struct queue* createQueue(int size)
{
struct queue* queue = (struct queue*)malloc(sizeof(struct queue));
queue->size = size;
queue->front = -1;
queue->rear = -1;
queue->array = (struct tree**)malloc(size*sizeof(struct tree*));
return queue;
}
void enqueue(struct tree *root, struct queue* queue)
{
if(queue->rear == queue->size-1){
return ;
}
queue->array[++queue->rear] = root;
if(queue->front == -1)
queue->front++;
}
struct tree* dequeue(struct queue* queue)
{
if(queue->front==-1)
return NULL;
struct tree *temp = queue->array[queue->front];
if(queue->front==queue->rear)
queue->rear = queue->front = -1;
else
queue->front++;
return temp;
}
void insert(struct tree **root, int data, struct queue* queue)
{
struct tree *temp = newNode(data);
if(*root == NULL)
*root = temp;
else{
struct tree *front = queue->array[queue->front];
if(front->left==NULL)
front->left = temp;
else if(front->right == NULL)
front->right = temp;
if(front->left!=NULL && front->right!=NULL)
dequeue(queue);
}
enqueue(temp,queue);
}
void levelOrder(struct tree* root)
{
struct queue *queue = createQueue(50);
struct tree* temp = root;
while(temp){
printf(" %d",temp->data);
if(temp->left){
enqueue(temp->left,queue);
}
if(temp->right){
enqueue(temp->right,queue);
}
temp = dequeue(queue);
}
}
int isSumProperty(struct tree* node)
{
int ld = 0,rd = 0;
if(node == NULL || (node->left == NULL && node->right == NULL)){
return 1;
}
else{
if(node->left!=NULL){
ld = node->left->data;
}
if(node->right!=NULL){
rd = node->right->data;
}
if(node->data == ld+rd && isSumProperty(node->left) && isSumProperty(node->right)){
return 1;
}
else{
return 0;
}
}
}