m2skills

mirror rec bt java

Jun 4th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. // program to create mirror of a given binary tree recursively
  2.  
  3. /**
  4.  * Created by MOHIT on 25-05-2018.
  5.  */
  6. import java.util.*;
  7. import static java.lang.Integer.max;
  8.  
  9. // node class
  10. class node{
  11.     int data;
  12.     node left;
  13.     node right;
  14.  
  15.     // function that returns a pointer to new node
  16.     public node(int element){
  17.         this.data = element;
  18.         this.left = null;
  19.         this.right = null;
  20.        
  21.     }
  22. };
  23.  
  24.  
  25. public class BinaryTree {
  26.     // function to return mirror of the binary tree
  27.     static void mirror_tree_recursive(node root){
  28.         if (root == null){
  29.             return;
  30.         }else{
  31.             // creating mirror using postorder traversal
  32.             mirror_tree_recursive(root.left);
  33.             mirror_tree_recursive(root.right);
  34.            
  35.             node temp = root.left;
  36.             root.left = root.right;
  37.             root.right = temp;
  38.         }
  39.     }
  40.     // function to print the preorder traversal of a binary tree
  41.     public static void preorder(node root) {
  42.         if (root == null) {
  43.             return;
  44.         }
  45.         System.out.print(root.data + " ");
  46.         preorder(root.left);
  47.         preorder(root.right);
  48.     }
  49.  
  50.  
  51.     public static void main(String arg[]) {
  52.         node m1 = new node(8);
  53.         m1.left = new node(3);
  54.         m1.right = new node(10);
  55.         m1.left.left = new node(1);
  56.         m1.left.right = new node(6);
  57.         m1.left.right.left = new node(4);
  58.         m1.left.right.right = new node(7);
  59.         m1.right.right = new node(14);
  60.         m1.right.right.left = new node(13);
  61.  
  62.         node m2 = new node(8);
  63.         m2.right = new node(3);
  64.         m2.left = new node(10);
  65.         m2.left.left = new node(14);
  66.         m2.right.left = new node(6);
  67.         m2.right.right = new node(1);
  68.         m2.left.left.right = new node(13);
  69.         m2.right.left.left = new node(7);
  70.         m2.right.left.right = new node(4);
  71.  
  72.         System.out.println("Tree #1 before mirroring : ");
  73.         preorder(m1);
  74.         System.out.println("\nTree #1 after mirroring : ");
  75.         mirror_tree_recursive(m1);
  76.         preorder(m1);
  77.         System.out.println("\n\nTree #2 is mirror of Tree #1 to check the correctness : ");
  78.         System.out.println("TREE #1 : ");
  79.         preorder(m1);
  80.         System.out.println("\nTREE #2 : ");
  81.         preorder(m2);
  82.  
  83.     }
  84. }
Add Comment
Please, Sign In to add comment