Check whether a binary tree is a full binary tree or not?

A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node.To check whether a binary tree is a full binary tree we need to test the following cases:-*1) If a binary tree node is NULL then it is a full binary tree.2) If a binary tree node does have empty left and right sub-trees, then it is a full binary tree by definition3) If a binary tree node has left and right sub-trees, then it is a part of a full binary tree by definition. In this
case recursively check if the left and right sub-trees are also binary trees themselves.4) In all other combinations of right and left sub-trees, the binary tree is not a full binary tree.*Following is the C code for checking if a binary tree is a full binary tree.// C program to check whether a given Binary Tree is full or not
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

/* Tree node structure */
struct Node
{
int key;
struct Node *left, *right;
};

/* Helper function that allocates a new node with the
given key and NULL left and right pointer. */
struct Node *newNode(char k)
{
struct Node *node = (struct Node*)malloc(sizeof(struct Node));
node->key = k;
node->right = node->left = NULL;
return node;
}

/* This function tests if a binary tree is a full binary tree. */
bool isFullTree (struct Node* root)
{
// If empty tree
if (root == NULL)
return true;

// If leaf node
if (root->left == NULL && root->right == NULL)
return true;

// If both left and right are not NULL, and left & right subtrees
// are full
if ((root->left) && (root->right))
return (isFullTree(root->left) && isFullTree(root->right));

// We reach here when none of the above if conditions work
return false;
}

// Driver Program
int main()
{
struct Node* root = NULL;
root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);

root->left->right = newNode(40);
root->left->left = newNode(50);
root->right->left = newNode(60);
root->right->right = newNode(70);

root->left->left->left = newNode(80);
root->left->left->right = newNode(90);
root->left->right->left = newNode(80);
root->left->right->right = newNode(90);
root->right->left->left = newNode(80);
root->right->left->right = newNode(90);
root->right->right->left = newNode(80);
root->right->right->right = newNode(90);

if (isFullTree(root))
printf(“The Binary Tree is fulln”);
else
printf(“The Binary Tree is not fulln”);

return(0);
}Output:The Binary Tree is full

News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *