A Tree is a hierarchical data structure that consists of nodes connected by edges. Each node contains a data and may have zero or more child nodes. In this tutorial, we are going to explore about another type of tree called a Binary Tree in details with examples, types, operations, applications, and their advantages and disadvantages.
What is a Binary Tree?
A Binary Tree is a fundamental data structure in computer science that organizes data hierarchically. In Binary tree, each node can have maximum two child nodes, referred to as the left child and the right child. The topmost node is called the root and the bottom-most nodes are called leaves.
Binary trees are widely used due to their simplicity and efficiency in various applications such as data storage and retrieval, expression evaluation, network routing etc. and can also be used to implement various algorithms such as searching, sorting, and graph algorithms.
In a Binary Tree, every node has three parts:
Data
Reference of left child node
Reference of right child node
Binary Tree Terminologies:
Node: In a tree, Node is an entity that contains keys and pointer (reference) to its child.
Edge: In a tree, connecting links of any two nodes are known as edges. All nodes in a tree connected by edges.
Root Node: In a tree, a Root Node is a first node or top most node of the tree. Every tree must have a root node and root node does not have a parent node. There will be only one root in a tree.
Parent Node: In a tree, the node that is a node's predecessor is called a Parent node. A parent node has a branch from it to any other node.
Child Node: In a tree, the descendant node of any node is called a child node.
Leaf Node: In a tree, A node which does not have any child node is called a leaf node. A leaf node is the bottom-most node of the tree.
Internal Node: In a tree, the node that has at least one child node is called an Internal Node. Nodes other than leaf nodes in a tree are Internal Nodes. Internal nodes are also known as 'Non-Terminal' nodes.
Depth of a Node: In a tree, the total number of children of a node is called a Degree of that node. The highest degree of a node among all the nodes in a tree is called a Degree of Tree.
Height of a Node: In the tree, the total number of edges from a leaf node to a particular node in the longest path is called the height of that node.
Level of a Node: In a tree, the root node is said to be at level 0, and the children of the root node at level 1 and, the children of the nodes at level 1, will be at level 2 and so on.
Path: In a tree, the sequence of Nodes and Edges from one node to another node is called as PATH between that two Nodes. Length of a Path is total number of nodes in that path.
Subtree: In a tree, a subtree will be formed on every single parent node by each child node.
Binary Tree Representation
Syntax to create a Node of Binary Tree in Java
Types of Binary Tree:
Binary Trees are following types:
Full Binary Tree
Perfect Binary Tree
Complete Binary Tree
Degenerate or Pathological Tree
Skewed Binary Tree
Balanced Binary Tree
Apart from the above, there are also some very important types of Binary Tree:
Binary Search Tree
AVL Tree
Red Black Tree
B Tree
B+ Tree
Operations On Binary Tree in Data Structure:
We can perform the following types of opearations on a binary tree data structure:
Insertion
Deletion
Searching
Traversing
1. Insertion
Insertion in a binary tree means adding a new node in such a way that the tree maintains its properties.
Example of Insert Operation in Binary Tree
Output
Binary tree before insertion: 50 30 70 40
Binary tree after insertion: 50 30 60 70 40
2. Deletion
Example of Delete Operation in Binary Tree
Output
Binary Tree before deletion operation: 50 30 60 20 40
Binary Tree after deleting 30 : 50 60 20 40
3. Searching
Example of Search Operation in Binary Tree
Output
40 is found in the binary tree
70 is not found in the binary tree
4. Traversing
Tree traversals are methods used to visit and process all the nodes in a binary tree data structure in a specific order. Tree Traversal algorithms can be categories mainly in two part, DFS and BFS: