Full Binary Tree vs. Complete Binary TreeWhat is a Full binary tree?A full binary tree can be defined as a binary tree in which all the nodes have 0 or two children. In other words, the full binary tree can be defined as a binary tree in which all the nodes have two children except the leaf nodes. The below tree is a full binary tree: The above tree is a full binary tree as all the nodes except the leaf nodes have two children. Full Binary tree theorem: Consider a Binary tree T to be a nonempty tree then:
What is a complete binary tree?A binary tree is said to be a complete binary tree when all the levels are completely filled except the last level, which is filled from the left. The below tree is a complete binary tree: The complete binary tree is similar to the full binary tree except for the two differences which are given below:
Let's understand the above points through an example: Consider the below tree: The above tree is a complete binary tree, but not a full binary tree as node 6 does not have its right sibling. Creation of Complete Binary Tree Suppose we have an array of 6 elements shown as below: The above array contains 6 elements, i.e., 1, 2, 3, 4, 5, 6. The following are the steps to be used to create a complete binary tree: Step 1: First, we will select the first element of the array, i.e., 1, and make a root node of the tree. The number of elements available in the first level is 1. Step 2: Now, we will select the second and third elements of the array. Keep the second element and third element of the array as the left and right child of the root node respectively shown as below: As we can observe above that the number of elements available in the second level is 2. Step 3: Now, we will select the next two elements from the array, i.e., 4 and 5. Keep these two elements on the left and right of node 2 shown as below: As we can observe above that nodes 4 and 5 are the left and right child of node 2 respectively. Step 4: Now, we will select the last element of the array, i.e., 6, and keep it as left child of the node 3 as we know that in a complete binary tree, the nodes are filled from the left side shown as below: As we can observe that the second level contains 3 elements. Let's understand the differences between complete and full binary tree through the images.
Next TopicTrie data structure
