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 in a new level.
Below are some of the complete binary trees.
1 / \ 2 3 1 / \ 2 3 / \ / 4 5 6
Below binary trees are not complete:
1 / \ 2 3 / / 4 5 1 / \ 2 3 / \ / 4 5 6 / 7
Complete binary trees are generally represented using arrays. The array representation is better because it doesn’t contain any empty slot. Given parent index i, its left child is given by 2 * i + 1 and its right child is given by 2 * i + 2. So no extra space is wasted and space to store left and right pointers is saved. However, it may be an interesting programming question to create a Complete Binary Tree using linked representation. Here Linked mean a non-array representation where left and right pointers(or references) are used to refer left and right children respectively. How to write an insert function that always adds a new node in the last level and at the leftmost available position?
To create a linked complete binary tree, we need to keep track of the nodes in a level order fashion such that the next node to be inserted lies in the leftmost position. A queue data structure can be used to keep track of the inserted nodes.
Following are steps to insert a new node in Complete Binary Tree.
1. If the tree is empty, initialize the root with new node.
2. Else, get the front node of the queue.
…….If the left child of this front node doesn’t exist, set the left child as the new node.
…….else if the right child of this front node doesn’t exist, set the right child as the new node.
3. If the front node has both the left child and right child, Dequeue() it.
4. Enqueue() the new node.
Below is the implementation:
1 2 3 4 5 6 7 8 9 10 11 12
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct Complete Binary Tree from its Linked List Representation
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Prufer Code to Tree Creation
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Tree into its Mirror Tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Construct a complete binary tree from given array in level order fashion
- Height of a complete binary tree (or Heap) with N nodes
- Find the largest Complete Subtree in a given Binary Tree
- Iterative Boundary Traversal of Complete Binary tree
- Print path from root to all nodes in a Complete Binary Tree
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Number of edges in mirror image of Complete binary tree
- Sideways traversal of a Complete Binary Tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Check if value exists in level-order sorted complete binary tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a Generic Tree(N-array Tree) to Binary Tree
Improved By : rathbhupendra