In computer science, various data structures help in arranging data in different forms. Among them, trees are widely used abstract data structures that simulate a hierarchical tree structure. A tree usually has a root value and subtrees that are formed by the child nodes from its parent nodes. Trees are non-linear data structures.
A general tree data structure has no limitation on the number of child nodes it can hold. Yet, this is not the case with a binary tree. This article will learn about a specific tree data structure – binary tree and binary tree types.
What Is Binary Tree Data Structure?
A binary tree is a tree-type non-linear data structure with a maximum of two children for each parent. Every node in a binary tree has a left and right reference along with the data element. The node at the top of the hierarchy of a tree is called the root node. The nodes that hold other sub-nodes are the parent nodes.
A parent node has two child nodes: the left child and right child. Hashing, routing data for network traffic, data compression, preparing binary heaps, and binary search trees are some of the applications that use a binary tree.
Terminologies associated with Binary Trees and Binary Tree Types
- Node: It represents a termination point in a tree.
- Root: A tree’s topmost node.
- Parent: Each node (apart from the root) in a tree that has at least one sub-node of its own is called a parent node.
- Child: A node that straightway came from a parent node when moving away from the root is the child node.
- Leaf Node: These are external nodes. They are the nodes that have no child.
- Internal Node: As the name suggests, these are inner nodes with at least one child.
- Depth of a Tree: The number of edges from the tree’s node to the root is.
- Height of a Tree: It is the number of edges from the node to the deepest leaf. The tree height is also considered the root height.
As you are now familiar with the terminologies associated with the binary tree and its types, it is time to understand the binary tree components.
Binary Tree Components
There are three binary tree components. Every binary tree node has these three components associated with it. It becomes an essential concept for programmers to understand these three binary tree components:
- Data element
- Pointer to left subtree
- Pointer to right subtree
These three binary tree components represent a node. The data resides in the middle. The left pointer points to the child node, forming the left sub-tree. The right pointer points to the child node at its right, creating the right subtree.
Binary Tree Types
There are various binary tree types, and each of these binary tree types has unique characteristics. Here are each of the binary tree types in detail:
1. Full Binary Tree
It is a special kind of a binary tree that has either zero children or two children. It means that all the nodes in that binary tree should either have two child nodes of its parent node or the parent node is itself the leaf node or the external node.
In other words, a full binary tree is a unique binary tree where every node except the external node has two children. When it holds a single child, such a binary tree will not be a full binary tree. Here, the quantity of leaf nodes is equal to the number of internal nodes plus one. The equation is like L=I+1, where L is the number of leaf nodes, and I is the number of internal nodes.
Here is the structure of a full binary tree:
2. Complete Binary Tree
A complete binary tree is another specific type of binary tree where all the tree levels are filled entirely with nodes, except the lowest level of the tree. Also, in the last or the lowest level of this binary tree, every node should possibly reside on the left side. Here is the structure of a complete binary tree:
3. Perfect Binary Tree
A binary tree is said to be ‘perfect’ if all the internal nodes have strictly two children, and every external or leaf node is at the same level or same depth within a tree. A perfect binary tree having height ‘h’ has 2h – 1 node. Here is the structure of a perfect binary tree:
4. Balanced Binary Tree
A binary tree is said to be ‘balanced’ if the tree height is O(logN), where ‘N’ is the number of nodes. In a balanced binary tree, the height of the left and the right subtrees of each node should vary by at most one. An AVL Tree and a Red-Black Tree are some common examples of data structure that can generate a balanced binary search tree. Here is an example of a balanced binary tree:
5. Degenerate Binary Tree
A binary tree is said to be a degenerate binary tree or pathological binary tree if every internal node has only a single child. Such trees are similar to a linked list performance-wise. Here is an example of a degenerate binary tree:
Benefits Of A Binary Tree
- The search operation in a binary tree is faster as compared to other trees
- Only two traversals are enough to provide the elements in sorted order
- It is easy to pick up the maximum and minimum elements
- Graph traversal also uses binary trees
- Converting different postfix and prefix expressions are possible using binary trees
The binary tree is one of the most widely used trees in the data structure. Each of the binary tree types has its unique features. These data structures have specific requirements in applied computer science. upGrad offers various courses in data science, machine learning, big data, and more.
If you are curious to learn about data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.