Swapping Nodes algorithm Implementation using java (binary tree)

Swapping subtrees of a node means that if initially node has left subtree L and right subtree R, then after swapping left subtree will be R and right subtree L.



  1. import java.io.*;
  2. import java.util.*;

  3. public class Solution {

  4.     public static void main(String[] args) {
  5.         Scanner stdIn=new Scanner(System.in);
  6.         int totalNodesInTree=stdIn.nextInt();
  7.         Queue <Node> nodesInCurrentLevel=new LinkedList<Node>();
  8.         
  9.         //Initially we have Node(1) in the tree;
  10.         Node root=new Node(1);
  11.         nodesInCurrentLevel.add(root);
  12.         Node current;

  13.         /*
  14.          * Building the Tree by adding nodes to the tree in level order from input via stdIn
  15.         */
  16.         int i=0;
  17.         while(i<totalNodesInTree && !nodesInCurrentLevel.isEmpty()){
  18.                 //Reading data for left child
  19.                 int leftVal=stdIn.nextInt();
  20.                 //Reading data for right child
  21.                 int rightVal=stdIn.nextInt(); 
  22.                 current=nodesInCurrentLevel.remove();
  23.                 /*
  24.                     Upon pointing current.left={leftChild} and current.right={rightChild}
  25.                     We push the {leftChild} and {rightChild} into the queue if not null
  26.                 */
  27.                 insertLeft(current,leftVal,nodesInCurrentLevel);    
  28.                 insertRight(current,rightVal,nodesInCurrentLevel);
  29.                 i++;
  30.         }    
  31.        
  32.         //Performing swaps
  33.         int numberOfSwaps=0;
  34.         if(totalNodesInTree>0)numberOfSwaps=stdIn.nextInt();
  35.         for (i=0; i<numberOfSwaps; i++){
  36.             swapLeftRightAtLevelKN(root,stdIn.nextInt(),1,1);
  37.             printTree(root);
  38.             System.out.println("");
  39.         }
  40.         
  41.                
  42.     }
  43.     
  44.     public static void swapLeftRightAtLevelKN(Node root, int levelK, int n, int currentLevel){
  45.         if(root==null) return;
  46.         if(currentLevel==levelK*n){
  47.             Node temp=root.left;
  48.             root.left=root.right;
  49.             root.right=temp;
  50.             n++;
  51.         }
  52.         swapLeftRightAtLevelKN(root.left, levelK, n,  currentLevel+1);
  53.         swapLeftRightAtLevelKN(root.right, levelK, n, currentLevel+1);
  54.     }
  55.     
  56.     public static void insertLeft(Node root, int val, Queue <Node> nodesInCurrentLevel){
  57.         if(val==-1){
  58.             root.left=null;
  59.         }else{
  60.             root.left=new Node(val);
  61.             nodesInCurrentLevel.add(root.left);
  62.         }
  63.     }
  64.     
  65.      
  66.     public static void insertRight(Node root, int val, Queue <Node> nodesInCurrentLevel){
  67.         if(val==-1){
  68.             root.right=null;
  69.         }else{
  70.             root.right=new Node(val);
  71.             nodesInCurrentLevel.add(root.right);
  72.         }
  73.     }
  74.     
  75.     
  76.     public static void printTree(Node root){
  77.         if(root==null) return;
  78.         printTree(root.left);
  79.         System.out.printf("%d ",root.data);
  80.         printTree(root.right);
  81.     }
  82. }

  83. class Node{
  84.     int data;
  85.     Node left=null;
  86.     Node right=null;
  87.     public Node(int data){
  88.         this.data=data;
  89.     }
  90. }




Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation