#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_Q_SIZE 500
struct
node {
int
data;
struct
node* left;
struct
node* right;
};
struct
node** createQueue(
int
*,
int
*);
void
enQueue(
struct
node**,
int
*,
struct
node*);
struct
node* deQueue(
struct
node**,
int
*);
bool
isQueueEmpty(
int
* front,
int
* rear);
bool
isCompleteBT(
struct
node* root)
{
if
(root == NULL)
return
true
;
int
rear, front;
struct
node** queue = createQueue(&front, &rear);
bool
flag =
false
;
enQueue(queue, &rear, root);
while
(!isQueueEmpty(&front, &rear)) {
struct
node* temp_node = deQueue(queue, &front);
if
(temp_node->left) {
if
(flag ==
true
)
return
false
;
enQueue(queue, &rear,
temp_node->left);
}
else
flag =
true
;
if
(temp_node->right) {
if
(flag ==
true
)
return
false
;
enQueue(
queue, &rear,
temp_node->right);
}
else
flag =
true
;
}
return
true
;
}
struct
node** createQueue(
int
* front,
int
* rear)
{
struct
node** queue = (
struct
node**)
malloc
(
sizeof
(
struct
node*) * MAX_Q_SIZE);
*front = *rear = 0;
return
queue;
}
void
enQueue(
struct
node** queue,
int
* rear,
struct
node* new_node)
{
queue[*rear] = new_node;
(*rear)++;
}
struct
node* deQueue(
struct
node** queue,
int
* front)
{
(*front)++;
return
queue[*front - 1];
}
bool
isQueueEmpty(
int
* front,
int
* rear)
{
return
(*rear == *front);
}
struct
node* newNode(
int
data)
{
struct
node* node
= (
struct
node*)
malloc
(
sizeof
(
struct
node));
node->data = data;
node->left = NULL;
node->right = NULL;
return
(node);
}
int
main()
{
struct
node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(6);
if
(isCompleteBT(root) ==
true
)
printf
(
"Complete Binary Tree"
);
else
printf
(
"NOT Complete Binary Tree"
);
return
0;
}