Binary Tree Data Structure
Binary Tree Data Structure
Binary Tree Data Structure
Let us traverse the following tree with all four traversal methods:
Binary Tree
We have discussed Introduction to Binary Tree in set 1 and the Properties of Binary
Tree in Set 2. In this post, common types of Binary Trees are discussed.
Types of Binary Tree based on the number of children:
Following are the types of Binary Tree based on the number of children:
1. Full Binary Tree
2. Degenerate Binary Tree
3. Skewed Binary Trees
1. Full Binary Tree
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.
In a Perfect Binary Tree, the number of leaf nodes is the number of internal
nodes plus 1
L = I + 1 Where L = Number of leaf nodes, I = Number of internal nodes.
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 h+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.
Refer to this article to read about more on Perfect Tree
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.
Example of Balanced and Unbalanced Binary Tree
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.
Some Special Types of Trees:
On the basis of node values, the Binary Tree can be classified into the
following special types:
1. Binary Search Tree
2. AVL Tree
3. Red Black Tree
4. B Tree
5. B+ Tree
6. Segment Tree
Below Image Shows Important Special cases of binary Trees:
Binary Tree Special cases
Suppose the given linked list is 10 -> 12 -> 13 -> 23 -> 30 -> 36. For each
ith node, its children are the (2i+1)th and (2i+2)nd nodes of the list. So, the
children of 10 are 12 and 13. The children of 12 are 23 and 30. The children of
13 are 36 and NULL. All other child nodes are NULL. Hence, the complete
binary tree is :-