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.
Complete Binary TreeSome 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.
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:
A Binary TreeThe 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
Element stored in an array level by levelIn the array, all the elements are stored continuously.
Example 2:
A binary treeHeight 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;
Element stored in an array level by levelExample 3:
A binary treeThe 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.
Element stored in an array level by levelThe 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:
A binary treeIn 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:
Element stored in an array level by levelExample 2:
A binary TreeIn 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:
Element stored in an array level by levelExample 3:
A binary treeIn 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:
Element stored in an array level by levelExample 4:
a binary treeIn 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.
Complete binary tree and its array representationAlgorithm:
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)
A is taken as root2. The next element (at index = 1) will be left and third element (index = 2) will be right child of root
B as left child and D as right child3. fourth (index = 3) and fifth element (index = 4) will be the left and right child of B node
E and F are left and right child of B4. Next element (index = 5) will be left child of the node D
G is made left child of D nodeThis 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.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem