The Wayback Machine - https://web.archive.org/web/20241001040728/https://www.geeksforgeeks.org/complete-binary-tree/
Open In App

Complete Binary Tree

Last Updated : 03 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

We know a tree is a non-linear data structure. It has no limitation on the number of children. A binary tree has a limitation as any node of the tree has at most two children: a left and a right child.

What is a Complete Binary Tree?

A complete binary tree is a special type of binary tree where all the levels of the tree are filled completely except the lowest level nodes which are filled from as left as possible.

Image

Complete Binary Tree

Some terminology of Complete Binary Tree:

  • Root – Node in which no edge is coming from the parent. Example -node A
  • Child – Node having some incoming edge is called child. Example – nodes B, F are the child of A and C respectively.
  • Sibling – Nodes having the same parent are sibling. Example- D, E are siblings as they have the same parent B.
  • Degree of a node – Number of children of a particular parent. Example- Degree of A is 2 and Degree of C is 1. Degree of D is 0.
  • Internal/External nodes – Leaf nodes are external nodes and non leaf nodes are internal nodes.
  • Level – Count nodes in a path to reach a destination node. Example- Level of node D is 2 as nodes A and B form the path.
  • Height – Number of edges to reach the destination node, Root is at height 0. Example – Height of node E is 2 as it has two edges from the root.

Properties of Complete Binary Tree:

  • A complete binary tree is said to be a proper binary tree where all leaves have the same depth.
  • In a complete binary tree number of nodes at depth d is 2d
  • In a  complete binary tree with n nodes height of the tree is log(n+1).
  • All the levels except the last level are completely full.

Perfect Binary Tree vs Complete Binary Tree:

A binary tree of height ‘h’ having the maximum number of nodes is a perfect binary tree. 
For a given height h, the maximum number of nodes is 2h+1-1.

A complete binary tree of height h is a perfect binary tree up to height h-1, and in the last level element are stored in left to right order.

Example 1:

Image

A Binary Tree

The height of the given binary tree is 2 and the maximum number of nodes in that tree is n= 2h+1-1 =  22+1-1 =  23-1 = 7.
Hence we can conclude it is a perfect binary tree.
Now for a complete binary tree, It is full up to height h-1 i.e.; 1, and the last level elements are stored in left to right order. Hence it is a complete Binary tree also. Here is the representation of elements when stored in an array

Image

Element stored in an array level by level

In the array, all the elements are stored continuously.

Example 2:

Image

A binary tree

Height of the given binary tree is 2 and the maximum number of nodes that should be there are 2h+1 – 1 = 22+1 – 1 = 23 – 1 = 7
But the number of nodes in the tree is 6. Hence it is not a perfect binary tree.
Now for a complete binary tree, It is full up to height h-1 i.e.; 1, and the last level element are stored in left to right order. Hence this is a complete binary tree. Store the element in an array and it will be like;

Image

Element stored in an array level by level

Example 3:

Image

A binary tree

The height of the binary tree is 2 and the maximum number of nodes that can be there is 7, but there are only 5 nodes hence it is not a perfect binary tree.
In case of a complete binary tree, we see that in the last level elements are not filled from left to right order. So it is not a complete binary tree.

Image

Element stored in an array level by level

The elements in the array are not continuous.

Full Binary Tree vs Complete Binary tree:

For a full binary tree, every node has either 2 children or 0 children.

Example 1:

Image

A binary tree

In the given binary tree there is no node having degree 1, either 2 or 0 children for every node, hence it is a full binary tree.

For a complete binary tree, elements are stored in level by level and not from the leftmost side in the last level. Hence this is not a complete binary tree. The array representation is:

Image

Element stored in an array level by level

Example 2:

Image

A binary Tree

In the given binary tree there is no node having degree 1. Every node has a degree of either 2 or 0. Hence it is a full binary tree.

For a complete binary tree, elements are stored in a level by level manner and filled from the leftmost side of the last level. Hence this a complete binary tree. Below is the array representation of the tree:

Image

Element stored in an array level by level

Example 3:

Image

A binary tree

In the given binary tree node B has degree 1 which violates the property of full binary tree hence it is not a full Binary tree

For a complete binary tree, elements are stored in level by level manner and filled from the leftmost side of the last level. Hence this is a complete binary tree. Array representation of the binary tree is:

Image

Element stored in an array level by level

Example 4:
 

Image

a binary tree

In the given binary tree node C has degree 1 which violates the property of a full binary tree hence it is not a full Binary tree

For a complete binary tree, elements are stored in level by level manner and filled from the leftmost side of the last level. Here node E violates the condition. Hence this is not a complete binary tree

Creation of Complete Binary Tree:

We know a complete binary tree is a tree in which except for the last level (say l)all the other level has (2l) nodes and the nodes are lined up from left to right side.
It can be represented using an array. If the parent is it index i so the left child is at 2i+1 and the right child is at 2i+2.

Image

Complete binary tree and its array representation

Algorithm:

For the creation of a Complete Binary Tree, we require a queue data structure to keep track of the inserted nodes.

Step 1: Initialize the root with a new node when the tree is empty.

Step 2: If the tree is not empty then get the front element 

  • If the front element does not have a left child then set the left child to a new node
  • If the right child is not present set the right child as a new node

Step 3: If the node has both the children then pop it from the queue.

Step 4: Enqueue the new data.

Illustration:

Consider the below array:

1. The 1st element will the root (value at index = 0)

Image

A is taken as root

2. The next element (at index = 1) will be left and third element (index = 2) will be right child of root

Image

B as left child and D as right child

3. fourth (index = 3) and fifth element (index = 4) will be the left and right child of B node

Image

E and F are left and right child of B

4. Next element (index = 5) will be left child of the node D

Image

G is made left child of D node

This is how complete binary tree is created.

Implementation: For the implementation of building a Complete Binary Tree from level order traversal is given in this post.

Application of the Complete binary tree:

  • Heap Sort
  • Heap sort-based data structure

Check if a given binary tree is complete or not: Follow this post to check if the given binary tree is complete or not.



Previous Article
Next Article

Similar Reads

Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
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 le
10 min read
Convert a Generic Tree(N-array Tree) to Binary Tree
Prerequisite: Generic Trees(N-array Trees) In this article, we will discuss the conversion of the Generic Tree to a Binary Tree. Following are the rules to convert a Generic(N-array Tree) to a Binary Tree: The root of the Binary Tree is the Root of the Generic Tree.The left child of a node in the Generic Tree is the Left child of that node in the B
13 min read
Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
We have discussed Threaded Binary Tree. The idea of threaded binary trees is to make inorder traversal faster and do it without stack and without recursion. In a simple threaded binary tree, the NULL right pointers are used to store inorder successor. Wherever a right pointer is NULL, it is used to store inorder successor. The following diagram sho
12 min read
Check whether a binary tree is a full binary tree or not | Iterative Approach
Given a binary tree containing n nodes. The problem is to check whether the given 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 only one child node. Examples: Input : 1 / \ 2 3 / \ 4 5 Outp
8 min read
Binary Tree to Binary Search Tree Conversion using STL set
Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of the Binary Tree.This solution will use Sets of C++ STL instead of array-based solution. Examples: Example 1 Input: 10 / \ 2 7 / \ 8 4 Output: 8 / \ 4 10 / \ 2 7 Example 2 Input: 10 / \ 30 15 / \ 20 5 Output: 15 / \
12 min read
Check whether a given binary tree is skewed binary tree or not?
Given a Binary Tree check whether it is skewed binary tree or not. A skewed tree is a tree where each node has only one child node or none. Examples: Input : 5 / 4 \ 3 / 2 Output : Yes Input : 5 / 4 \ 3 / \ 2 4 Output : No The idea is to check if a node has two children. If node has two children return false, else recursively compute whether subtre
13 min read
Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
Given two binary trees S and T, the task is the check that if S is a subtree of the Tree T. For Example: Input: Tree T - 1 / \ 2 3 / \ / \ 4 5 6 7 Tree S - 2 / \ 4 5 Output: YES Explanation: The above tree is the subtree of the tree T, Hence the output is YES Approach: The idea is to traverse both the tree in Pre-order Traversal and check for each
11 min read
Check if a Binary tree is Subtree of another Binary tree | Set 3
Given two binary trees, check if the first tree is a subtree of the second one. A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T. The subtree corresponding to the root node is the entire tree; the subtree corresponding to any other node is called a proper subtree. Examples: Input: Tree-T Tree-S 1 3 / \ / 2
15 min read
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. More information about full binary trees can be found here. For Example : Recommended PracticeFull binary treeTry It! To check whether a binary tree is a full binary tre
14 min read
Check if a binary tree is subtree of another binary tree | Set 2
Given two binary trees, check if the first tree is a subtree of the second one. A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T. The subtree corresponding to the root node is the entire tree; the subtree corresponding to any other node is called a proper subtree.For example, in the following case, Tree1 i
15+ min read
Minimum swap required to convert binary tree to binary search tree
Given the array representation of Complete Binary Tree i.e, if index i is the parent, index 2*i + 1 is the left child and index 2*i + 2 is the right child. The task is to find the minimum number of swap required to convert it into Binary Search Tree. Examples: Input : arr[] = { 5, 6, 7, 8, 9, 10, 11 } Output : 3 Binary tree of the given array: Swap
8 min read
Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
Idea of Threaded Binary Tree is to make inorder traversal faster and do it without stack and without recursion. In a simple threaded binary tree, the NULL right pointers are used to store inorder successor. Wherever a right pointer is NULL, it is used to store inorder successor. Following diagram shows an example Single Threaded Binary Tree. The do
12 min read
Binary Tree to Binary Search Tree Conversion
Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of Binary Tree. Examples Example 1Input: 10 / \ 2 7 / \ 8 4Output: 8 / \ 4 10 / \ 2 7Example 2Input: 10 / \ 30 15 / \ 20 5Output: 15 / \ 10 20 / \ 5 30Solution: Following is a 3 step solution for converting Binary tre
15+ min read
Check if a Binary Tree is subtree of another binary tree | Set 1
Given two binary trees, check if the first tree is a subtree of the second one. A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T. The subtree corresponding to the root node is the entire tree; the subtree corresponding to any other node is called a proper subtree. Examples: Input: Tree S 10 / \ 4 6 \ 30 Tr
11 min read
Difference between Binary Tree and Binary Search Tree
Binary Tree Data Structure:A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right children. Binary Search Tree Data Structure (BST):A binary search tree is a hierarchical data structure where each node has at most two children, wi
2 min read
Linked complete binary tree & its creation
A complete binary tree is a binary tree where each level 'l' except the last has 2^l nodes and the nodes at the last level are all left-aligned. Complete binary trees are mainly used in heap-based data structures. The nodes in the complete binary tree are inserted from left to right in one level at a time. If a level is full, the node is inserted i
15 min read
Find the largest Complete Subtree in a given Binary Tree
Given a Binary Tree, the task is to find the size of largest Complete sub-tree in the given Binary Tree. Complete Binary Tree - A Binary tree is Complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possible. Note: All Perfect Binary Trees are Complete Binary tree but reve
15+ min read
Find LCA for K queries in Complete Binary Tree
Given an integer n. There is a complete binary tree with 2n - 1 nodes. The root of that tree is the node with the value 1, and every node with a value x has two children where the left node has the value 2*x and the right node has the value 2*x + 1, you are given K queries of type (ai, bi), and the task is to return the LCA for the node pair ai and
6 min read
Print path from root to all nodes in a Complete Binary Tree
Given a number N which is the total number of nodes in a complete binary tree where nodes are number from 1 to N sequentially level-wise. The task is to write a program to print paths from root to all of the nodes in the Complete Binary Tree.For N = 3, the tree will be: 1 / \ 2 3 For N = 7, the tree will be: 1 / \ 2 3 / \ / \ 4 5 6 7 Examples: Inpu
9 min read
Number of edges in mirror image of Complete binary tree
Given a complete binary tree of depth H. If the mirror image from the left and the right side of this tree is taken then: Right Mirrored Image: Rightmost node of the every level is connected to mirrored corresponding node. Left Mirrored Image: Left most node of the every level is connected to mirrored corresponding node. The task is to find the num
4 min read
Find value K in given Complete Binary Tree with values indexed from 1 to N
Given a complete binary tree with values indexed from 1 to N and a key K. The task is to check whether a key exists in the tree or not. Print "true" if the key exists, otherwise print "false". Complete Binary Tree: A Binary Tree is a complete Binary Tree if all the levels are completely filled except possibly the last level and the last level has a
11 min read
Sideways traversal of a Complete Binary Tree
Given a Complete Binary Tree, the task is to print the elements in the following pattern. Let's consider the tree to be: The tree is traversed in the following way: The output for the above tree is: 1 3 7 11 10 9 8 4 5 6 2 Approach: The idea is to use the modified breadth first search function to store all the nodes at every level in an array of ve
15+ min read
Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
Given an array arr[] of size N, the task is to generate a Complete Binary Tree in such a way that sum of the non-leaf nodes is minimum, whereas values of the leaf node corresponds to the array elements in an In-order Traversal of the tree and value of each non-leaf node corresponds to the product of the largest leaf value in the left sub-tree and r
6 min read
Print path from a node to root of given Complete Binary Tree
Given an integer N, the task is to find the path from the Nth node to the root of a Binary Tree of the following form: The Binary Tree is a Complete Binary Tree up to the level of the Nth node.The nodes are numbered 1 to N, starting from the root as 1.The structure of the Tree is as follows: 1 / \ 2 3 / \ / \ 4 5 6 7 ................ / \ ..........
4 min read
Difference between Full and Complete Binary Tree
A binary tree is a type of data structure where each node can only have two offspring at most named as “left” and “right” child. There are different types of binary tree but here we are going to discuss about the difference of Complete binary tree and Full binary tree. Full Binary Tree: A full binary tree is a binary tree in which all of the nodes
4 min read
Minimum nodes to be removed to make a Binary tree complete
Given a binary tree with positive values of nodes, find the minimum number of nodes that need to be removed to transform it into a complete binary tree. Return 0 if the given tree is already complete. Note: A complete binary tree is a special type of binary tree where all the levels of the tree are filled completely except possibly the lowest level
15+ min read
Count number of nodes in a complete Binary Tree
Given the root of a Complete Binary Tree consisting of N nodes, the task is to find the total number of nodes in the given Binary Tree. Examples: Input: Output: 7 Input: Output: 5 Native Approach: The simple approach to solving the given tree is to perform the DFS Traversal on the given tree and count the number of nodes in it. After traversal, pri
15+ min read
Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not.A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. See the following examples. The following trees are examples of Complete Binary Trees 1 /
15+ min read
Height of a complete binary tree (or Heap) with N nodes
Consider a Binary Heap of size N. We need to find the height of it. Examples: Input : N = 6 Output : 2 () / \ () () / \ / () () () Input : N = 9 Output : 3 () / \ () () / \ / \ () () () () / \ () ()Recommended PracticeHeight of HeapTry It! Let the size of the heap be N and the height be h. If we take a few examples, we can notice that the value of
3 min read
Article Tags :
Practice Tags :