# 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

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

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

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

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

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

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

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
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

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
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

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
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

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
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

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
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

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
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

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
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

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
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.

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

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

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

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

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

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

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

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

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

1
Enter the data that you want to add to the Complete binary tree::
28

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.

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.