DAA: Binary Tree and its Categories

Binary Tree and its Categories

The binary tree is a non-linear data structure in which there are 0 or utmost 2 nodes.  Each node has two children, i.e., left and right child. The top node is known as the root node. 

There are various fields where binary trees are used, such as data compression, hashing, routing data, and binary search trees.

An example is given in the below image:

DAA: Binary Tree and its Categories

The basic terminologies of a binary tree are as follows:

Node: The termination point in a tree is called a node.

Root: The first node in the tree is known as a root node.

Parent node: The node for which there exists atleast one subtree is known as the parent node of that subtree.

Child node: The node that is made from the parent node is called the child node.

Inner node: The node with atleast a single child is known as the inner node.

Depth of a tree: The total edges from a root node to the tree node are known as the depth of the tree.

Height of the tree: The total number of edges from the root to the leaf node is called the tree’s height.

What does a tree comprise of? 

Every binary tree contains three components:

  1. The data of the node.
  2. The left pointer to that node (To make left subtree).
  3. The right pointer to that node (To make the right subtree).
DAA: Binary Tree and its Categories

C++ code of binary tree implementation:

 #include <bits/stdc++.h>
 using namespace std;
 // Create the structure of the binary tree as above components
 struct Node { // create tree structure
           int data; // The valu of node
           struct Node* left; // The left pointer to the node
           struct Node* right; // The right pointer to the node
           Node(int val) // This constructor adds a new value to the tree having the value val
           {
                    data = val; // Initialise data
                    eft = NULL; // The left child is NULL currently
                    right = NULL; // The right child is also NULL currently
           }
 };
 int main() // Main function
 {
           struct Node* root = new Node(1); // Create a root node of binary tree
           root->left = new Node(2); // For the root node the left child has value 2
           root->right = new Node(3); // For the same root node the right child has value 3
           root->left->left = new Node(4); // Adding 4 as the left child of 2
           root->left->right = new Node(5); // Adding 4 as right child of 2  
           return 0;
 } 

Types of binary trees:

The following are the binary tree types:

  1. Full binary tree: The full binary tree has either 0 or two child nodes. In this type of tree, Either the parent node will have zero children or two children. The other name of a binary tree is a proper binary tree. Here the number of leaf nodes is the number of internal nodes + 1.  An example is given in the below image:
DAA: Binary Tree and its Categories
  • Complete binary tree: This is the type of binary tree in which all the tree levels are filled except the lowest one, which is also filled from the left. It is almost similar to a full binary tree except the all leaf elements lean towards the left, and the last leaf may not have a right sibling. An example is given in the below image:
DAA: Binary Tree and its Categories
DAA: Binary Tree and its Categories
  • Perfect binary tree: This is the type of binary tree in which every node has exactly two children, and the leaf nodes are at the same level. A perfect binary tree has (2h - 1) nodes where h is the tree’s height. An example is given in the below image:
DAA: Binary Tree and its Categories
  • Balanced binary tree: The binary tree is balanced if the height of the tree is O(logN), where N is the number of nodes in the tree. Common examples of balanced trees are AVL trees and Red-black trees.

The below image is the example of a balanced binary tree:

DAA: Binary Tree and its Categories
  • Degenerate Binary Tree: In this type of tree, the intern nodes have only one child, either the left or right child. Such trees can be seen as a linked list in terms of performance. The below image is an example of a degenerate binary tree:
DAA: Binary Tree and its Categories

Advantages of using a Binary Tree:

  1. The search operation is faster in trees when compared to other data structures.
  2. The graph traversals use a binary tree.
  3. The maximum and minimum elements can be found in lower time complexities.
  4. The prefix and postfix conversions are done using the binary tree method.

Related Topics

DAA: Continuous Tree

Continuous Tree A continuous tree is the one in which the nodes from root to leaf path, the two adjacent node values, have a difference of 1. Input :          3                     /   \                   ...

5 minutes read.

DAA: KMP Algorithm

KMP ALGORITHM The KMP algorithm is abbreviated as the "Knuth Morris Pratt” algorithm. This algorithm was developed by all of them.  This algorithm searches a pattern of length m in a string...

10 minutes read.

DAA: Rabin Karp Algorithm

Rabin Karp Algorithm The Rabin Karp or Karp Rabin algorithm is used to matching a specific pattern in the string. It uses the technique of hashing to match a specific text. There also...

6 minutes read.

DAA: Bubble Sort Algorithm

Bubble Sort Algorithm The bubble sort algorithm is also known as the sinking algorithm. In this algorithm, we iterate over the array, and it takes two adjacent elements and swaps them...

3 minutes read.

Introduction to Sorting in DAA

DAA: What is Sorting? The technique in which a data structure is rearranged in decreasing order, increasing order, or in a specified order is called sorting. We apply to sort in our...

4 minutes read.

DAA: Binary Tree and its Categories

Binary Tree and its Categories The binary tree is a non-linear data structure in which there are 0 or utmost 2 nodes.  Each node has two children, i.e., left and right...

4 minutes read.

DAA: Insertion Sort Algorithm on Singly Link List

Insertion Sort Algorithm on Singly Link List We will sort a singly link list using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The insertion sort technique works...

3 minutes read.

DAA: Construct a Tree from Inorder and Preorder Traversals

Construct a Tree from Inorder and Preorder Traversals We are given inorder and preorder traversals of a tree. We need to generate a tree from these traversals. Example: Inorder[]   = { 3, 1,...

4 minutes read.

Boyer Moore Algorithm

Boyer Moore Algorithm The Boyer Moore algorithm is a searching algorithm in which a string of length n and a pattern of length m is searched. It prints all the occurrences...

11 minutes read.

DAA: Algorithm of Right View of a Binary Tree

Algorithm of Right View of a Binary Tree The right view of a binary tree is the visible nodes from the right side of the tree. In the given tree, the visible...

5 minutes read.

DAA: Bubble Sort Algorithm on Linked List

Bubble Sort Algorithm on Linked List In this article, we will sort a Link List using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The bubble sort technique...

4 minutes read.

DAA: Dynamic Programming

Dynamic Programming Introduction The technique of breaking a problem statement into subproblems and using the optimal result of subproblems as an optimal result of the problem statement is known as dynamic programming....

2 minutes read.

Recurrence relation in DAA

Recurrence relation in DAA The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model. A recursive relation, T(n), is a recursive...

5 minutes read.

DAA: Breadth First Search (BFS) for a Graph

Breadth First Search (Bfs) For A Graph The algorithm in which all the graph nodes are traversed is known as the breadth-first search algorithm. In this algorithm, we select one node,...

5 minutes read.

Segregate the given Linked List in DAA

Segregate Even and Odd Nodes in a Linked List A linked list is a linear data structure in which each node has two blocks. One contains the node’s value or data,...

3 minutes read.

DAA: Expression Trees

Expression Trees Expression trees are those in which the leaf nodes have the values to be operated, and internal nodes contain the operator on which the leaf node will be performed. Example:...

4 minutes read.

DAA: Find the Height or Maximum Depth of a Binary Tree

Find the Height or Maximum Depth of a Binary Tree We have a binary tree structure and we need to find its height. It is defined by the distance from the...

3 minutes read.

DAA: Bottom view of a Binary Tree

Bottom view of a Binary Tree The bottom view of a binary tree is the number of nodes visible when viewed from the bottom. At every horizontal distance, there would be...

3 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Floyd Cycle Detection

Floyd Cycle Detection Floyd Cycle algorithm is one of the cycle detection algorithms to detect the cycle in a given singly linked list. In the Floyd Cycle algorithm, we have two pointers...

4 minutes read.