×

Search anything:

# Complete Binary Tree

#### Algorithms Problems on Binary Tree

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article, we will learn about the what is the tree,Binary tree and complete binary tree also how it differs from a general Binary Tree in terms of different operations.

• Introduction To Tree Data Structure
• Introduction To Complete Binary Tree
• Complete Binary Tree Terminology :
• Complete Binary Tree Properties
• Algorithm
• Implementation
• Complete Binary Tree Applications
• Introduction To General Binary Tree
• Implementation of General Binary Tree
• Comparision Between General Tree & Complete Binary Tree:
• Quiz

# Introduction To Tree Data Structure

The tree is a hierarchical structure that contains a collection of nodes. Where each node presents its value and edges connect them. It also consists of root, parent, child, sibling path, leaf node, etc.

There are different types of trees:

1. General tree
2. Binary tree
3. Binary search tree
4. Avl tree
5. B-tree

# Complete Binary Tree:

A complete binary tree is a binary tree data structure where all level of tree is completely filled by nodes except lowest one.
It is filed from left to right. It contains some similarity of full binary tree, but it is not full binary tree. Cause full binary tree contain zero and two nodes. The last leaf element of tree doesnâ€™t contain right node.

**Binary tree can have in the range of 1 and 2h nodes at the last level h of tree.
**

A perfect tree is always complete binary tree but a complete tree is not perfect tree.
A complete binary tree can be represented using an array.
The number of internal nodes in a complete binary tree of n nodes is [ n/2].

Why complete binary tree has no right sibling?

Before understanding Why complete binary tree has no right sibling let revise some property of tree.

1. A complete binary tree has two nodes at each level
2. All levels of a tree are filled except the last level.
3. All level is filled from left to right direction.

Simplification :

Now letâ€™s try to simplify it. Now consider the level of a tree by L .
If a binary tree is completely filled then at each level tree has 2L nodes.
But it is complete binary treeâ€¦Where all the levels of a tree are filled except the last level.So, it doesnâ€™t contain 2L node except last one.
Also, they are they are filed from left to right direction. So, it is not possible for a tree to have a right child node without left child.

## ** Tree Terminology** :

Root â€“ A special node which dont have any parent node.
Parent :Immedidiate predecessor of node .
Child â€“ Immedidiate successor of node .
Sibling â€“ Nodes with the same parent are sibling.
Path - Number of successive edges from source to destination node .
Degree of a node â€“ Number of children of any node.
Leaf node - node with degree 0 is leaf node.
Height â€“ Number of edges to destination node

## Properties

1. All leaf node level is n or n-1.
2. All level are filled from left to right.
3. Last node doesnot hav right sibling.
4. Height of the tree is log(n+1).
5. All leaves have the same depth.

## Algorithm :

Step 1 :Start.
Step 2 :Count the number of nodes.
Step 3 :Set index to 0. Eg .i=0
Step 4 :Set count to 0. Eg .count =0
Step 5 :Start recursion of the binary tree.
Step 6 :if current node is NULL
Return true.
Step 7 :If i>=count
Return false.
Step 8 : Perform recursion for left and right sub tree. And print result.
Step 9: Stop.

## Implementation

Code In Python For Complete Binary Tree:

``````# program to check binary tree is complete binary tree or not

class Node:

def __init__(self, item):
self.item = item
self.left = None
self.right = None

# count nodes
def count_nodes(root):
if root is None:
return 0
return (1 + count_nodes(root.left) + count_nodes(root.right))

# Check if  tree is complete binary tree or not
def is_complete(root, index, numberNodes):

# Check tree is empty or not
if root is None:
return True

if index >= numberNodes:
return False

return (is_complete(root.left, 2 * index + 1, numberNodes)
and is_complete(root.right, 2 * index + 2, numberNodes))

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)

node_count = count_nodes(root)
index = 0

# check tree is complte or not
if is_complete(root, index, node_count):
print("This tree is complete Binary Tree")
else:
print("This tree is not complete Binary Tree")

``````

## Output :

This tree is complete Binary Tree

## Explanation:

In this program first count the number of nodes in the binary tree. if it donâ€™t have any node then return 0. and then start recursion of the binary tree from the root . set index (i) to 0 and count to 0.
if the index of current node is under is null, then the tree is a complete binary tree. then return true.
if index of the current node is greater than or equal to the number of nodes in the binary tree then the tree is not a complete binary. and then return false. print the appropite message on user screen.

## Applications

1. Heap-based data structures
2. heap sort.
3. Binary Heap

Binary Heap

A Binary Heap is a Binary Tree . It has following properties.

1. It is a complete tree .
2. All levels are filled except the last level.
3. It contain Min Heap or Max Heap.
This property makes heap to be suitable to store in an array.

Array Represenation:
In a Min Binary Heap the key present at root must be minimum among all keys.
A Binary Heap is a Complete Binary Tree. It can represented using as an array also .

• Arr[0] represent is root node .
• Arr[(i-1)/2] represent parent node.
• Arr[(2i)+1] represent left node.
• Arr[(2i)+2] represent right node.

# **General Binary Tree **

A binary tree is a tree data structure where each node of the tree can have a maximum of two children. Where the left node is called the left child and the right node is called the right node. Children of nodes of a binary tree are ordered.

## Implementation

Program for General Binary Tree:

``````class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data

def insert(self, data):
# Compare the new value with the parent node
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data

# Print the binary tree
def PrintTree(self):
if self.left:
self.left.PrintTree()
print( self.data),
if self.right:
self.right.PrintTree()

# insert nodes
root = Node(20)
root.insert(8)
root.insert(4)
root.insert(3)
# Print the binary tree
root.PrintTree()
``````

3 4 8 20

### Explaintaion :

This will insert specific data in to tree.

## Comparision Between General Tree and Complete Binary Tree:

Parameter General binary tree Complete binary tree
Count Of Node The general tree is a tree in which each node can have two children or nodes. A complete binary tree is a binary tree data structure where all level of the tree is filled by nodes except the lowest one.
Tree representation Tree representation is easy. Tree representation somewhere is easy
last level of tree In a general binary tree, a node in the last level can have two children. A complete binary tree node in the last level can have any child which is present on the left side.
Order of nodes There is no order for nodes. In a complete binary tree, the node should be filled from the left to right.
Naming Convention A general binary tree is also known as a binary tree. A complete binary tree is also known as an almost complete binary tree.
Limitation A binary tree canâ€™t have more than two child nodes. A binary tree canâ€™t have more than one child node.
Degree of a Node The degree of a node is 2 The degree of a node is 1
Subtree While in a binary tree, there are mainly two subtrees: left-node and right-node. While in a binary tree, there are mainly two subtrees: left-node.
Node Position All the nodes lean towards the left and right. All the nodes lean towards only one side. left side.
Application Decision tree , Expression evaluation and Sorting. Heap-based , data structures , heap sort

## Question

#### Which of the following statement is not true in the conext of tree terminlogy ?

Root node doesnot have parent node.
leaf node has deggree 1
Nodes with the same parent are sibling.
Child Immedidiate successor of node .
The node which does not have any child node is considered as a leaf node. The degree of a leaf node is 0.This are Present at Bottom of the tree.

## Question 2

#### What is complete Binary tree ?

A binary tree where all level of tree is completely filled by nodes except lowest one.
A binary tree where all node have degree 2.
A binary tree having degree 0.
A binary tree where all node has exactly 0 and 2 children
A binary tree in which all level of tree are filled by left to right direction except lowest one. A binary tree where all node has exactly 0 and 2 children is considerd as full binary tree .

#### Reshma Patil

Reshma Patil is a student at Institute Of Civil And Rural Engineering, Gargoti.

Improved & Reviewed by:

Complete Binary Tree