Binary Tree
Binary Tree
=H+1
Example-
= 2H+1 – 1
Example-
= 23+1 – 1 = 16 – 1 = 15 nodes
Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted = 15.
Property-03:
Example-
Here,
Clearly, the number of leaf nodes is one greater than number of nodes with 2 children.
NOTE
The number of leaf nodes in any binary tree depends only on the number of nodes with 2 children.
Property-04:
= 2L
Example-
= 22
=4
Thus, in a binary tree, maximum number of nodes that can be present at level-2 = 4.
Types of Binary Tree based on the number of children:
The following are the types of Binary Tree based on the number of children:
A Binary Tree is a full binary tree if every node has 0 or 2 children. The following are examples of
a full binary tree. We can also say a full binary tree is a binary tree in which all nodes except leaf
nodes have two children.
A full Binary tree is a special type of binary tree in which every parent node/internal node has
either two or no children. It is also known as a proper binary tree.
A Tree where every internal node has one child. Such trees are performance-wise same as linked
list. A degenerate or pathological tree is a tree having a single child either left or right.
A skewed binary tree is a pathological/degenerate tree in which the tree is either dominated by
the left nodes or the right nodes. Thus, there are two types of skewed binary tree: left-skewed
binary tree and right-skewed binary tree.
A Binary Tree is a Complete Binary Tree if all the levels are completely filled except possibly the
last level and the last level has all the keys as left as possible.
A complete binary tree is just like a full binary tree, but with two major differences:
• The last leaf element might not have a right sibling, i.e. a complete binary tree doesn’t
have to be a full binary tree.
A Binary tree is a Perfect Binary Tree in which all the internal nodes have two children and all leaf
nodes are at the same level.
A perfect binary tree is a type of binary tree in which every internal node has exactly two child
nodes and all the leaf nodes are at the same level.
In a Perfect Binary Tree, the number of leaf nodes is the number of internal nodes plus 1
A Perfect Binary Tree of height h (where the height of the binary tree is the number of edges in
the longest path from the root node to any leaf node in the tree, height of root node is 0) has
2ℎ+1 – 1 node. An example of a Perfect binary tree is ancestors in the family. Keep a person at
root, parents as children, parents of parents as their children.
3. Balanced Binary Tree
A binary tree is balanced if the height of the tree is O(Log n) where n is the number of nodes. For
Example, the AVL tree maintains O(Log n) height by making sure that the difference between the
heights of the left and right subtrees is at most 1. Red-Black trees maintain O(Log n) height by
making sure that the number of Black nodes on every root to leaf paths is the same and that there
are no adjacent red nodes. Balanced Binary Search trees are performance-wise good as they
provide O(log n) time for search, insert and delete.
It is a type of binary tree in which the difference between the height of the left and the right
subtree for each node is either 0 or 1. In the figure above, the root node having a value 0 is
unbalanced with a depth of 2 units.
Difference between Full and Complete Binary Tree
A binary tree is a type of data structure where each node can only have two offspring at most
named as “left” and “right” child.
A Binary Tree
There are different types of binary tree but here we are going to discuss about the difference
of Complete binary tree and Full binary tree.
A full binary tree is a binary tree in which all of the nodes have either 0 or 2 offspring. In other
terms, a full binary tree is a binary tree in which all nodes, except the leaf nodes, have two
offspring.
Complete Binary Tree:
A binary tree is said to be a complete binary tree if all its levels, except possibly the last level,
have the maximum number of possible nodes, and all the nodes in the last level appear as far
left as possible.
1. The leftmost side of the leaf node must always be filled first.
2. It isn’t necessary for the last leaf node to have a right sibling.
Check the following examples to understand the full and complete binary tree in a better way.
Example 1:
• Node C has just one child therefore, it is not a Full binary tree.
• Node C also has a right child but no left child, therefore it is also not a Complete binary
tree.
Hence, the binary tree shown above is neither complete nor full binary tree.
Example 2:
• All of the nodes have either 0 or 2 offspring, therefore, it is a Full binary tree.
• It is not a Complete binary tree because node B has no children whereas node C has
children, and according to a complete binary tree, nodes should be filled from the left side.
Hence, the binary tree shown above is a Full binary tree and it is not a Complete binary tree.
Example 3:
• Node B has just one child, therefore, it is not a full binary tree.
Hence, the binary tree shown above is a Complete binary tree and it is not a Full binary tree.
Example 4:
Complete and full
• It is a Complete binary tree because all the nodes are left filled.
• All of the nodes have either 0 or 2 offspring, therefore, it is a full binary tree.
Hence, the binary tree shown above is both a complete and a full binary tree.
S.
Complete Binary Tree Full Binary Tree
No.
In a complete binary tree, a node in the In a full binary tree, a node cannot have just
1.
last level can have only one child. one child.
In a complete binary tree, the node There is no order of filling nodes in a full
2.
should be filled from the left to right. binary tree.
Complete binary trees are mainly used Full binary tree has no application as such
3.
in heap-based data structures. but is also called a proper binary tree.
A complete binary tree is also called A full binary tree also called proper binary
4.
almost complete binary tree. tree or 2-tree.
Tree Traversal Techniques
Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike
linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical
way to traverse them, trees can be traversed in different ways. In this article, we will
discuss all the tree traversal techniques along with their uses.
Tree Traversal refers to the process of visiting or accessing each node of the tree exactly
once in a certain order. Tree traversal algorithms help us to visit and process all the nodes
of the tree. Since tree is not a linear data structure, there are multiple nodes which we
can visit after visiting a certain node. There are multiple tree traversal techniques which
decide the order in which the nodes of the tree are to be visited.
Inorder Traversal:
Inorder traversal visits the node in the order: Left -> Root -> Right
Algorithm for Inorder Traversal:
Inorder(tree)
• Traverse the left subtree, i.e., call Inorder(left->subtree)
• Visit the root.
• Traverse the right subtree, i.e., call Inorder(right->subtree)