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?**

**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.

**Some terminology of Complete Binary Tree:**

**Some terminology of Complete Binary Tree:**– Node in which no edge is coming from the parent. Example -node A**Root**– Node having some incoming edge is called child. Example – nodes B, F are the child of A and C respectively.**Child**– Nodes having the same parent are sibling. Example- D, E are siblings as they have the same parent B.**Sibling**– Number of children of a particular parent. Example- Degree of A is 2 and Degree of C is 1. Degree of D is 0.**Degree of a node**– Leaf nodes are external nodes and non leaf nodes are internal nodes.**Internal/External nodes**– 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.**Level**– 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.**Height**

**Properties of Complete Binary Tree:**

**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
is**d****2**.^{d} - In a complete binary tree with
nodes height of the tree is**n**.**log(n+1)** - All the levels
are completely full.**except the last level**

**Perfect Binary Tree**** vs Complete Binary Tree:**

**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

**, the maximum number of nodes is**

**h**

**2**

^{h+1}**.**

**-1**A ** complete** binary tree of height h is a perfect binary tree up to height

**, and in the last level element are stored in left to right order.**

**h-1****Example 1:**

The height of the given binary tree is 2 and the maximum number of nodes in that tree is n= 2^{h+1}-1 = 2^{2+1}-1 = 2^{3}-1 = ** 7**.

Hence we can conclude it is

**.**

**a perfect binary tree**Now for a complete binary tree, It is full up to height

**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**

**h-1**In the array, all the elements are stored continuously.

**Example 2:**

Height of the given binary tree is 2 and the maximum number of nodes that should be there are 2^{h+1 }– 1 = 2^{2+1 }– 1 = 2^{3 }– 1 = ** 7**.

But the number of nodes in the tree is

**. Hence it is**

**6****.**

**not a perfect binary tree**Now for a complete binary tree, It is full up to height

**i.e.;**

**h-1****, and the last level element are stored in left to right order. Hence this is a**

**1****. Store the element in an array and it will be like;**

**complete binary tree****Example 3:**

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**The elements in the array are not continuous.

**Full Binary Tree vs Complete Binary tree:**

**Full Binary Tree vs Complete Binary tree:**For a full binary tree, every node has either 2 children or 0 children.

**Example 1:**

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:

**Example 2:**

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:

**Example 3:**

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:

**Example 4:**

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:**

**Creation of Complete Binary Tree:**We know a ** complete binary tree** is a tree in which except for the last level (say

**)all the other level has (**

**l****) nodes and the nodes are lined up from left to right side.**

**2l**It can be represented using an array. If the parent is it index

**so the left child is at**

**i****and the right child is at**

**2i+1****.**

**2i+2****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

**it from the queue.**

**pop**** Step 4:** Enqueue the new data.

**Illustration:**

Consider the below array:

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

)02. The next element (at index =

) will be left and third element (index =1) will be right child of root23. fourth (index =

) and fifth element (index =3) will be the left and right child of B node44. Next element (index =

) will be left child of the node D5This 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:**

**Application of the Complete binary tree:**- Heap Sort
- Heap sort-based data structure

__Check if a given binary tree is complete or not:__ Follow

**to check if the given binary tree is complete or not.**

**this post**