Home » Complete Binary Tree

Complete Binary Tree

by Online Tutorials Library

Complete Binary Tree

A Complete Binary Tree is a type of binary tree that satisfies some particular set of conditions. These conditions are:

  • Each level in the complete binary should be completely filled only other than the last level of the Complete Binary Tree. Complete filling of a particular level means that each parent that is present in that particular level should have exactly two children nodes, i.e., the left node and the right node. If any of the parents in a level doesn’t have both the right child and the left then it will not be considered as completely filled.
  • Another condition that is required for a binary to satisfy in order to be called a Complete Binary Tree is that the last level of the tree should have all the keys as left as possible. That means if a parent node is present in the last level of a Complete Binary Tree then it should have the child as only the left child node.

If any of the binary trees satisfy these two particular conditions mentioned above then that binary tree is labeled as a Complete Binary Tree.

So, now for a better understanding let us write a C++ code for the creation and display of the complete binary tree.

Code:

Output:

Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.    1  Enter the data that you want to add to the Complete binary tree::  12        Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.    1  Enter the data that you want to add to the Complete binary tree::  14  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.    1   Enter the data that you want to add to the Complete binary tree::  16       Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.    1  Enter the data that you want to add to the Complete binary tree::  18  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.    1  Enter the data that you want to add to the Complete binary tree::  15  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.    1  Enter the data that you want to add to the Complete binary tree::  24  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.    2  Contents of the Complete binary tree are::  12 14 16 18 15 24   Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  N  

So, in the C++ program that we have written and executed above, we have provided a menu-driven approach to add data in the complete binary tree and also verify the addition operation by printing the data present in the complete binary tree.

Here first we added six nodes to the complete binary tree by calling the insert function that will add or append a new node to the existing complete binary tree. The value for these new nodes of the complete binary tree is taken by the user at the time of adding a node to the complete binary tree. So, the user enters any of the options displayed in the menu and the operation is performed successfully.

So, now let us write code for the Complete Binary Tree in the Java programming language.

Code:

Output:

Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  1  Enter the data that you want to add to the Complete binary tree::  11  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  1  Enter the data that you want to add to the Complete binary tree::  23  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  1   Enter the data that you want to add to the Complete binary tree::  16  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  1  Enter the data that you want to add to the Complete binary tree::  76  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  1  Enter the data that you want to add to the Complete binary tree::  43  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  1  Enter the data that you want to add to the Complete binary tree::  33  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  1  Enter the data that you want to add to the Complete binary tree::  92  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  1  Enter the data that you want to add to the Complete binary tree::  31  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations from the menu Displayed below::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.  2  Contents of the Complete binary tree are::  31 76 23 43 11 33 16 92   Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  n  

So, in the Java program that we have written and executed above, we have provided a menu-driven approach to add data in the complete binary tree that will provide two functionalities; one for adding data to the complete binary tree and with the help of the other functions, we can also verify the addition operation by printing the data present in the complete binary tree.

Here, first we added eight nodes having values 31, 76, 23, 43, 11, 33, 16, and 92 respectively to the complete binary tree by calling the insert function that will add or append a new node to the existing complete binary tree but creating a new complete binary tree if the node that is going to be added is the first node that will act as a root of the complete binary tree. The value for these new nodes of the complete binary tree is taken by the user at the time of adding a node to the complete binary tree and is passed as a parameter to the insert function to the complete binary tree’s add function. So, the user enters any of the options displayed in the menu and the operation is performed successfully. If the option added by the user is other than the specified operations; an error message is displayed showing “Please enter a valid option from the menu to proceed further.”. And in order to exit the program, the user needs to add ‘N’ or ‘n’ and to proceed with the program for further options ‘Y’ or ‘y’ is added by the user.

So, now let us write code for the Complete Binary Tree in the Python programming language.

Code:

Output:

Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  23  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  76  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  42  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  11  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      30    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  56  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  68  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  03  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  44  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      1  Enter the data that you want to add to the Complete binary tree::  17  Data Added Successfully.    1  Enter the data that you want to add to the Complete binary tree::  28  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    y  Please Choose one of the Operations::  1. To Insert Data in the Complete binary tree.  2. To Display Data from the Complete binary tree.      2  Contents of the Complete binary tree are::  23 76 42 11 56 68 3 44 17 28  Type [N or n] to terminate the program.  Type [Y or y] to continue the program.    N  

So, in the Python program that we have written and executed above, we have provided a menu-driven approach to add data in the complete binary tree that will provide two functionalities; one for adding data to the complete binary tree and with the help of the other functions, we can also verify the addition operation by printing the data present in the complete binary tree.

Here first we added nodes to the complete binary tree by calling the insert function that will add or append a new node to the existing complete binary tree but creating a new complete binary tree if the node that is going to be added is the first node that will act as a root of the complete binary tree. The value for these new nodes of the complete binary tree is taken by the user at the time of adding a node to the complete binary tree and is passed as a parameter to the insert function to the complete binary tree’s add function. So, the user enters any of the options displayed in the menu and the operation is performed successfully. If the option added by the user is other than the specified operations an error message is displayed showing “Please enter a valid option from the menu to proceed further.”. And in order to exit the program, the user needs to add ‘N’ or ‘n’ and to proceed with the program for further options ‘Y’ or ‘y’ is added by the user.

So, this article explains the Complete Binary Tree Data Structure and what are the basic functions or operations that we can perform on a Complete Binary Tree. We also understood the usage of the Complete Binary Tree in various programming languages like Java, Python, and C++ along with their functionalities that are required to perform the basic operations on this Data Structure. Other than these examples, there are various scenarios where we can use the Complete Binary Tree.


You may also like