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.
- Construct Complete Binary Tree from its Linked List Representation
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Height of a complete binary tree (or Heap) with N nodes
- Construct a complete binary tree from given array in level order fashion
- Prufer Code to Tree Creation
- Flatten a binary tree into linked list | Set-2
- Flatten a binary tree into linked list | Set-3
- Flatten a binary tree into linked list
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Convert a Binary Tree into Doubly Linked List in spiral fashion