Mirror Tree Java

PROGRAM TO CONVERT A BINARY TREE INTO ITS MIRROR TREE



class Node
{
    int data;
    Node left, right;
  
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
  
class BinaryTree
{
    Node root;
  
    void mirror()
    {
        root = mirror(root);
    }
  
    Node mirror(Node node)
    {
        if (node == null)
            return node;
  
        /* do the subtrees */
        Node left = mirror(node.left);
        Node right = mirror(node.right);
  
        /* swap the left and right pointers */
        node.left = right;
        node.right = left;
  
        return node;
    }
  
    void inOrder()
    {
        inOrder(root);
    }
  
    /* Helper function to test mirror(). Given a binary
       search tree, print out its data elements in
       increasing sorted order.*/
    void inOrder(Node node)
    {
        if (node == null)
            return;
  
        inOrder(node.left);
        System.out.print(node.data + " ");
  
        inOrder(node.right);
    }
  
    /* testing for example nodes */
    public static void main(String args[])
    {
        /* creating a binary tree and entering the nodes */
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
  
        /* print inorder traversal of the input tree */
        System.out.println("Inorder traversal of input tree is :");
        tree.inOrder();
        System.out.println("");
  
        /* convert tree to its mirror */
        tree.mirror();
  
        /* print inorder traversal of the minor tree */
        System.out.println("Inorder traversal of binary tree is : ");
        tree.inOrder();
  
    }
}


OUTPUT:
Inorder traversal of the constructed tree is 
4 2 5 1 3 
Inorder traversal of the mirror tree is 
3 1 5 2 4 

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java