5.1 Binary Tree and Its Types
5.1 Binary Tree and Its Types
Binary Tree
Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each element in a
binary tree can have only 2 children, we typically name them the left and right child.
A Binary tree is represented by a pointer to the topmost node (commonly known as the “root”) of the tree. If the
tree is empty, then the value of the root is NULL. Each node of a Binary Tree contains the following parts:
A) Data
B) Pointer to left child
C) Pointer to right child
The above tree is a binary tree because each node contains the utmost two children. The logical representation
of the above tree is given below:
In the above tree, node 1 contains two pointers, i.e., left and a right pointer pointing to the left and right node
respectively. The node 2 contains both the nodes (left and right node); therefore, it has two pointers (left and
right). The nodes 3, 5 and 6 are the leaf nodes, so all these nodes contain NULL pointer on both left and right
parts.
Inserting an element.
Removing an element.
Searching for an element.
Traversing the tree.
The full binary tree is also known as a strict binary tree. The tree can only be considered as the full binary tree if
each node must contain either 0 or 2 children. The full binary tree can also be defined as the tree in which each
node must contain 2 children except the leaf nodes.
In the above tree, we can observe that each node is either containing zero or two children; therefore, it is a Full
Binary tree.
2. Complete Binary Tree
The complete binary tree is a tree in which all the nodes are completely filled except the last level. In the last
level, all the nodes must be as left as possible. In a complete binary tree, the nodes should be added from the
left.
A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf nodes are at the same
level.
The degenerate binary tree is a tree in which all the internal nodes have only one children.
The above tree is also a degenerate binary tree because all the nodes have only one child. It is also known as a
left-skewed tree as all the nodes have a left child only.