A complete binary tree is a binary tree whose all levels except the last level are completely filled and all the leaves in the last level are all to the left side. More information about complete binary trees can be found here.
Example:
Below tree is a Complete Binary Tree (All nodes till the second last nodes are filled and all leaves are to the left side)

An iterative solution for this problem is discussed in below post.
Check whether a given Binary Tree is Complete or not | Set 1 (Using Level Order Traversal)
In this post a recursive solution is discussed.
In the array representation of a binary tree, if the parent node is assigned an index of ‘i’ and left child gets assigned an index of ‘2*i + 1’ while the right child is assigned an index of ‘2*i + 2’. If we represent the above binary tree as an array with the respective indices assigned to the different nodes of the tree above from top to down and left to right.
Hence we proceed in the following manner in order to check if the binary tree is complete binary tree.
- Calculate the number of nodes (count) in the binary tree.
- Start recursion of the binary tree from the root node of the binary tree with index (i) being set as 0 and the number of nodes in the binary (count).
- If the current node under examination is NULL, then the tree is a complete binary tree. Return true.
- If index (i) of the current node is greater than or equal to the number of nodes in the binary tree (count) i.e. (i>= count), then the tree is not a complete binary. Return false.
- Recursively check the left and right sub-trees of the binary tree for same condition. For the left sub-tree use the index as (2*i + 1) while for the right sub-tree use the index as (2*i + 2).
The time complexity of the above algorithm is O(n). Following is the code for checking if a binary tree is a complete binary tree.
Implementation:
C++
#include<bits/stdc++.h>
#include<stdbool.h>
using namespace std;
class Node
{
public :
int key;
Node *left, *right;
Node *newNode( char k)
{
Node *node = ( Node*) malloc ( sizeof ( Node));
node->key = k;
node->right = node->left = NULL;
return node;
}
};
unsigned int countNodes(Node* root)
{
if (root == NULL)
return (0);
return (1 + countNodes(root->left) +
countNodes(root->right));
}
bool isComplete ( Node* root, unsigned int index,
unsigned int number_nodes)
{
if (root == NULL)
return ( true );
if (index >= number_nodes)
return ( false );
return (isComplete(root->left, 2*index + 1, number_nodes) &&
isComplete(root->right, 2*index + 2, number_nodes));
}
int main()
{
Node n1;
Node* root = NULL;
root = n1.newNode(1);
root->left = n1.newNode(2);
root->right = n1.newNode(3);
root->left->left = n1.newNode(4);
root->left->right = n1.newNode(5);
root->right->right = n1.newNode(6);
unsigned int node_count = countNodes(root);
unsigned int index = 0;
if (isComplete(root, index, node_count))
cout << "The Binary Tree is complete\n" ;
else
cout << "The Binary Tree is not complete\n" ;
return (0);
}
|
C
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct Node
{
int key;
struct Node *left, *right;
};
struct Node *newNode( char k)
{
struct Node *node = ( struct Node*) malloc ( sizeof ( struct Node));
node->key = k;
node->right = node->left = NULL;
return node;
}
unsigned int countNodes( struct Node* root)
{
if (root == NULL)
return (0);
return (1 + countNodes(root->left) + countNodes(root->right));
}
bool isComplete ( struct Node* root, unsigned int index,
unsigned int number_nodes)
{
if (root == NULL)
return ( true );
if (index >= number_nodes)
return ( false );
return (isComplete(root->left, 2*index + 1, number_nodes) &&
isComplete(root->right, 2*index + 2, number_nodes));
}
int main()
{
struct Node* root = NULL;
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);
unsigned int node_count = countNodes(root);
unsigned int index = 0;
if (isComplete(root, index, node_count))
printf ( "The Binary Tree is complete\n" );
else
printf ( "The Binary Tree is not complete\n" );
return (0);
}
|
Java
class Node
{
int data;
Node left, right;
Node( int item) {
data = item;
left = right = null ;
}
}
class BinaryTree
{
Node root;
int countNodes(Node root)
{
if (root == null )
return ( 0 );
return ( 1 + countNodes(root.left) + countNodes(root.right));
}
boolean isComplete(Node root, int index, int number_nodes)
{
if (root == null )
return true ;
if (index >= number_nodes)
return false ;
return (isComplete(root.left, 2 * index + 1 , number_nodes)
&& isComplete(root.right, 2 * index + 2 , number_nodes));
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
Node NewRoot = null ;
tree.root = new Node( 1 );
tree.root.left = new Node( 2 );
tree.root.right = new Node( 3 );
tree.root.left.right = new Node( 5 );
tree.root.left.left = new Node( 4 );
tree.root.right.right = new Node( 6 );
int node_count = tree.countNodes(tree.root);
int index = 0 ;
if (tree.isComplete(tree.root, index, node_count))
System.out.print( "The binary tree is complete" );
else
System.out.print( "The binary tree is not complete" );
}
}
|
Python3
class Node:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def countNodes(root):
if root is None :
return 0
return ( 1 + countNodes(root.left) + countNodes(root.right))
def isComplete(root, index, number_nodes):
if root is None :
return True
if index > = number_nodes :
return False
return (isComplete(root.left , 2 * index + 1 , number_nodes)
and isComplete(root.right, 2 * index + 2 , number_nodes)
)
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.left.right = Node( 5 )
root.right.right = Node( 6 )
node_count = countNodes(root)
index = 0
if isComplete(root, index, node_count):
print ( "The Binary Tree is complete" )
else :
print ( "The Binary Tree is not complete" )
|
C#
using System;
class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
public class BinaryTree
{
Node root;
int countNodes(Node root)
{
if (root == null )
return (0);
return (1 + countNodes(root.left) +
countNodes(root.right));
}
bool isComplete(Node root, int index,
int number_nodes)
{
if (root == null )
return true ;
if (index >= number_nodes)
return false ;
return (isComplete(root.left, 2 * index + 1, number_nodes)
&& isComplete(root.right, 2 * index + 2, number_nodes));
}
public static void Main()
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.right = new Node(5);
tree.root.left.left = new Node(4);
tree.root.right.right = new Node(6);
int node_count = tree.countNodes(tree.root);
int index = 0;
if (tree.isComplete(tree.root, index, node_count))
Console.WriteLine( "The binary tree is complete" );
else
Console.WriteLine( "The binary tree is not complete" );
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
var root;
function countNodes(root) {
if (root == null )
return (0);
return (1 + countNodes(root.left) + countNodes(root.right));
}
function isComplete(root , index , number_nodes) {
if (root == null )
return true ;
if (index >= number_nodes)
return false ;
return (isComplete(root.left, 2 * index + 1, number_nodes)
&& isComplete(root.right, 2 * index + 2, number_nodes));
}
var NewRoot = null ;
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.right = new Node(5);
root.left.left = new Node(4);
root.right.right = new Node(6);
var node_count = countNodes(root);
var index = 0;
if (isComplete(root, index, node_count))
document.write( "The binary tree is complete" );
else
document.write( "The binary tree is not complete" );
</script>
|
Output
The Binary Tree is not complete
Time Complexity: O(N) where N is the number of nodes in the tree.
Space Complexity: O(h) where h is the height of given tree due to recursion call.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
31 May, 2023
Like Article
Save Article