Invert Binary Tree in DAA

Invert Binary Tree: A binary tree is a tree in which each node of the tree contains two children, i.e., left children and right children. Let us suppose we have given a binary tree, and the task is to invert the given binary tree. Inverting a Tree will create the mirror of it.

For Example:

Input:

Invert Binary Tree in DAA

Output:  

Invert Binary Tree in DAA

Explanation: Inverting the given binary tree will flip all the nodes as a mirror image.

The approach used in the below program to solve this problem is as follows:

Inverting a binary tree is also known as the mirroring of the given tree, but to invert a binary tree, we will create a temporary node that will keep the nodes in each level and swap the left node as well as a right node.

  • Take input nodes of the binary Tree.
  • A function flipNode(node*root) takes the root node as input and helps to create the mirror of the node.
  • Swap the left node and right node.
  • A function invertBinaryTree(node*root) takes the root node and calls the flipNode(root) function to convert a binary tree in its invert form.

Example:

#include<iostream> using namespace std; 
struct treenode
{  
int data;  
treenode*left; 
 treenode*right;
 };  
struct treenode* createNode(int d)
{    
 struct treenode*root= new treenode;  
   root->data= d;     root->left= NULL;     root->right= NULL;     return root;   
  }  
void flipNodes(treenode*root)
{    
 if(root==NULL)
{        
 return;    
 }   
 treenode*temp= root->left;    root->left= root->right;    root->right= temp; 
} 
treenode*invertBinaryTree(treenode*root)
{    
 flipNodes(root);     return root; 
}  
int heightofTree(treenode*root)
{    
 if(root==NULL)
{      
   return 0;   
  }     
return max(heightofTree(root->left), heightofTree(root->right))+1;
 } 
 void givenlevel(treenode*root, int lev)
{  
   if(root==NULL)
{    
     return;  
   }    
 if(lev==1)
{         
cout<<root->data<<" ";    
 }    
 else if(lev>1)
{      
   givenlevel(root->left,lev-1);
         givenlevel(root->right,lev-1);
     } 
}
  void printLevelwise(treenode*root)
{   
  int height= heightofTree(root);
     int i;     for(i=1;i<=height;i++)
{       
  givenlevel(root,i); 
   }
 }  
int main()
{ 
struct treenode*root= NULL;
 root= createNode(4); 
root->left= createNode(2); 
root->right= createNode(7);
 root->left->right= createNode(3);
 root->left->left= createNode(1);
 root->right->left= createNode(6);
 root->right->right= createNode(9);
 printLevelwise(root); 
cout<<"After Inverting:"<<endl; invertBinaryTree(root);
 printLevelwise(root);  
return 0; }

Running the above code will generate the output as follows:

Output: 

 4 2 7 1 3 6 9
 After Inverting:
 4 7 2 6 9 1 3 

Related Topics

DAA: Dijkstra’s Algorithm (Shortest Path)

Dijkstra’s Algorithm (Shortest Path) Dijkstra’s algorithm finds the shortest distance from a source to all the vertices in a graph. This algorithm is used in network protocols like IS-IS and OSPF(Open...

3 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: Algorithm to Find the Maximum Width of a Tree

Algorithm to Find the Maximum Width of a Tree The width of a binary tree is defined as the maximum number of nodes at a given level. The level having the...

5 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: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 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.

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: 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.

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: 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: 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.

DAA: Application of DFS and BFS

Application of DFS and BFS Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which...

3 minutes read.

DAA: Interpolation Search Algorithm

Interpolation Search Algorithm There is no doubt that binary search is a great algorithm with average time complexity of log n. The feature of discarding one half of the array reduces...

4 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.

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.

Invert Binary Tree in DAA

Invert Binary Tree: A binary tree is a tree in which each node of the tree contains two children, i.e., left children and right children. Let us suppose we have...

2 minutes read.

DAA: Insert a node in Binary Search Tree

Insert a node in Binary Search Tree (BST) We have a Binary search tree and a key. Insert the key in the binary search tree if not present. In the above figure,...

4 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.

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.