Skip to main content

Construct a mirror tree from the given binary tree

Constructing Mirror Binary Tree

Here given code implementation process.

/*
    C Program 
    Construct a mirror tree from the given binary tree
*/
#include <stdio.h>
#include <stdlib.h>

// Tree Node
struct TreeNode
{
	int data;
	struct TreeNode *left;
	struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
	struct TreeNode *root;
};
// Create new tree
struct BinaryTree *newTree()
{
	// Create dynamic node
	struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
	if (tree != NULL)
	{
		tree->root = NULL;
	}
	else
	{
		printf("Memory Overflow to Create tree Tree\n");
	}
	//return new tree
	return tree;
}
// returns a new node of tree
struct TreeNode *newNode(int data)
{
	// Create dynamic node
	struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
	if (node != NULL)
	{
		//Set data and pointer values
		node->data = data;
		node->left = NULL;
		node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return node;
}
struct TreeNode *createMirror(struct TreeNode *node)
{
	if (node == NULL)
	{
		return NULL;
	}
	// Create a new node
	struct TreeNode *n = newNode(node->data);
	// Get the left node of created node
	n->left = createMirror(node->right);
	// Get the right node of created node
	n->right = createMirror(node->left);
	// Returns the constructed node
	return n;
}
// Print tree elements
void preorder(struct TreeNode *node)
{
	if (node != NULL)
	{
		//Print node value
		printf("  %d", node->data);
		preorder(node->left);
		preorder(node->right);
	}
}
int main()
{
	// Define tree
	struct BinaryTree *tree = newTree();
	struct BinaryTree *mirror = newTree();
	/*
	         1
	       /   \
	      2     8
	     / \   / \
	    3  10 6   5
	       /   \   \
	      7     4   9
	   
	-----------------------
	    Binary Tree
	-----------------------
	*/
	tree->root = newNode(1);
	tree->root->left = newNode(2);
	tree->root->right = newNode(8);
	tree->root->left->left = newNode(3);
	tree->root->left->right = newNode(10);
	tree->root->left->right->left = newNode(7);
	tree->root->right->left = newNode(6);
	tree->root->right->right = newNode(5);
	tree->root->right->left->right = newNode(4);
	tree->root->right->right->right = newNode(9);
	printf("\n Actual Binary Tree \n");
	preorder(tree->root);
	mirror->root = createMirror(tree->root);
	/*
	         1
	       /   \
	      8     2
	     / \   / \
	    5   6 10  3
	   /   /   \
	  9   4     7
	-----------------------
	Constructed Mirror Binary Tree 
	-----------------------
	*/
	printf("\n Constructed Mirror Tree \n");
	preorder(mirror->root);
	return 0;
}

Output

 Actual Binary Tree
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree
  1  8  5  9  6  4  2  10  7  3
/*
  Java Program
  Construct a mirror tree from the given binary tree
*/
// Tree Node
class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	// Returning a mirror tree of given binary tree
	public TreeNode createMirror(TreeNode node)
	{
		if (node == null)
		{
			return null;
		}
		// Create a new node
		TreeNode n = new TreeNode(node.data);
		// Get the left node of created node
		n.left = createMirror(node.right);
		// Get the right node of created node
		n.right = createMirror(node.left);
		// Returns the constructed node
		return n;
	}
	// Print tree elements
	public void preorder(TreeNode node)
	{
		if (node != null)
		{
			//Print node value
			System.out.print("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	public static void main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		BinaryTree mirror = new BinaryTree();
		/*
             1
           /   \
          2     8
         / \   / \
        3  10 6   5
           /   \   \
          7     4   9
       
        -----------------------
            Binary Tree
        -----------------------
        */
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(10);
		tree.root.left.right.left = new TreeNode(7);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.right = new TreeNode(5);
		tree.root.right.left.right = new TreeNode(4);
		tree.root.right.right.right = new TreeNode(9);
		System.out.print("\n Actual Binary Tree \n");
		tree.preorder(tree.root);
		mirror.root = tree.createMirror(tree.root);
		/*
		         1
		       /   \
		      8     2
		     / \   / \
		    5   6 10  3
		   /   /   \
		  9   4     7
		-----------------------
		Constructed Mirror Binary Tree 
		-----------------------
		*/
		System.out.print("\n Constructed Mirror Tree \n");
		tree.preorder(mirror.root);
	}
}

Output

 Actual Binary Tree
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree
  1  8  5  9  6  4  2  10  7  3
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  Construct a mirror tree from the given binary tree
*/
// Tree Node
class TreeNode
{
	public: int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public: TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	// Returning a mirror tree of given binary tree
	TreeNode *createMirror(TreeNode *node)
	{
		// Returns the constructed node
		if (node == NULL)
		{
			return NULL;
		}
		// Create a new node
		TreeNode *n = new TreeNode(node->data);
		// Get the left node of created node
		n->left = this->createMirror(node->right);
		// Get the right node of created node
		n->right = this->createMirror(node->left);
		return n;
	}
	// Print tree elements
	void preorder(TreeNode *node)
	{
		if (node != NULL)
		{
			//Print node value
			cout << "  " << node->data;
			this->preorder(node->left);
			this->preorder(node->right);
		}
	}
};
int main()
{
	BinaryTree tree = BinaryTree();
	BinaryTree mirror = BinaryTree();
	/*
	             1
	           /   \
	          2     8
	         / \   / \
	        3  10 6   5
	           /   \   \
	          7     4   9
	       
	        -----------------------
	            Binary Tree
	        -----------------------
	        
	*/
	tree.root = new TreeNode(1);
	tree.root->left = new TreeNode(2);
	tree.root->right = new TreeNode(8);
	tree.root->left->left = new TreeNode(3);
	tree.root->left->right = new TreeNode(10);
	tree.root->left->right->left = new TreeNode(7);
	tree.root->right->left = new TreeNode(6);
	tree.root->right->right = new TreeNode(5);
	tree.root->right->left->right = new TreeNode(4);
	tree.root->right->right->right = new TreeNode(9);
	cout << "\n Actual Binary Tree \n";
	tree.preorder(tree.root);
	mirror.root = tree.createMirror(tree.root);
	/*
	         1
	       /   \
	      8     2
	     / \   / \
	    5   6 10  3
	   /   /   \
	  9   4     7
	-----------------------
	Constructed Mirror Binary Tree 
	-----------------------
	*/
	cout << "\n Constructed Mirror Tree \n";
	tree.preorder(mirror.root);
	return 0;
}

Output

 Actual Binary Tree
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree
  1  8  5  9  6  4  2  10  7  3
// Include namespace system
using System;
/*
  C# Program
  Construct a mirror tree from the given binary tree
*/
// Tree Node
public class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	// Returning a mirror tree of given binary tree
	public TreeNode createMirror(TreeNode node)
	{
		// Returns the constructed node
		if (node == null)
		{
			return null;
		}
		// Create a new node
		TreeNode n = new TreeNode(node.data);
		// Get the left node of created node
		n.left = createMirror(node.right);
		// Get the right node of created node
		n.right = createMirror(node.left);
		return n;
	}
	// Print tree elements
	public void preorder(TreeNode node)
	{
		if (node != null)
		{
			//Print node value
			Console.Write("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	public static void Main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		BinaryTree mirror = new BinaryTree();
		/*
		             1
		           /   \
		          2     8
		         / \   / \
		        3  10 6   5
		           /   \   \
		          7     4   9
		       
		        -----------------------
		            Binary Tree
		        -----------------------
		        
		*/
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(10);
		tree.root.left.right.left = new TreeNode(7);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.right = new TreeNode(5);
		tree.root.right.left.right = new TreeNode(4);
		tree.root.right.right.right = new TreeNode(9);
		Console.Write("\n Actual Binary Tree \n");
		tree.preorder(tree.root);
		mirror.root = tree.createMirror(tree.root);
		/*
		         1
		       /   \
		      8     2
		     / \   / \
		    5   6 10  3
		   /   /   \
		  9   4     7
		-----------------------
		Constructed Mirror Binary Tree 
		-----------------------
		*/
		Console.Write("\n Constructed Mirror Tree \n");
		tree.preorder(mirror.root);
	}
}

Output

 Actual Binary Tree
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree
  1  8  5  9  6  4  2  10  7  3
<?php
/*
  Php Program
  Construct a mirror tree from the given binary tree
*/
// Tree Node
class TreeNode
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinaryTree
{
	public $root;

	function __construct()
	{
		$this->root = null;
	}
	// Returning a mirror tree of given binary tree
	public	function createMirror($node)
	{
		// Returns the constructed node
		if ($node == null)
		{
			return null;
		}
		// Create a new node
		$n = new TreeNode($node->data);
		// Get the left node of created node
		$n->left = $this->createMirror($node->right);
		// Get the right node of created node
		$n->right = $this->createMirror($node->left);
		return $n;
	}
	// Print tree elements
	public	function preorder($node)
	{
		if ($node != null)
		{
			//Print node value
			echo "  ". $node->data;
			$this->preorder($node->left);
			$this->preorder($node->right);
		}
	}
}

function main()
{
	$tree = new BinaryTree();
	$mirror = new BinaryTree();
	/*
	             1
	           /   \
	          2     8
	         / \   / \
	        3  10 6   5
	           /   \   \
	          7     4   9
	       
	        -----------------------
	            Binary Tree
	        -----------------------
	        
	*/
	$tree->root = new TreeNode(1);
	$tree->root->left = new TreeNode(2);
	$tree->root->right = new TreeNode(8);
	$tree->root->left->left = new TreeNode(3);
	$tree->root->left->right = new TreeNode(10);
	$tree->root->left->right->left = new TreeNode(7);
	$tree->root->right->left = new TreeNode(6);
	$tree->root->right->right = new TreeNode(5);
	$tree->root->right->left->right = new TreeNode(4);
	$tree->root->right->right->right = new TreeNode(9);
	echo "\n Actual Binary Tree \n";
	$tree->preorder($tree->root);
	$mirror->root = $tree->createMirror($tree->root);
	/*
	         1
	       /   \
	      8     2
	     / \   / \
	    5   6 10  3
	   /   /   \
	  9   4     7
	-----------------------
	Constructed Mirror Binary Tree 
	-----------------------
	*/
	echo "\n Constructed Mirror Tree \n";
	$tree->preorder($mirror->root);
}
main();

Output

 Actual Binary Tree
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree
  1  8  5  9  6  4  2  10  7  3
/*
  Node Js Program
  Construct a mirror tree from the given binary tree
*/
// Tree Node
class TreeNode
{
	constructor(data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	// Returning a mirror tree of given binary tree
	createMirror(node)
	{
		// Returns the constructed node
		if (node == null)
		{
			return null;
		}
		// Create a new node
		var n = new TreeNode(node.data);
		// Get the left node of created node
		n.left = this.createMirror(node.right);
		// Get the right node of created node
		n.right = this.createMirror(node.left);
		return n;
	}
	// Print tree elements
	preorder(node)
	{
		if (node != null)
		{
			//Print node value
			process.stdout.write("  " + node.data);
			this.preorder(node.left);
			this.preorder(node.right);
		}
	}
}

function main()
{
	var tree = new BinaryTree();
	var mirror = new BinaryTree();
	/*
	             1
	           /   \
	          2     8
	         / \   / \
	        3  10 6   5
	           /   \   \
	          7     4   9
	       
	        -----------------------
	            Binary Tree
	        -----------------------
	        
	*/
	tree.root = new TreeNode(1);
	tree.root.left = new TreeNode(2);
	tree.root.right = new TreeNode(8);
	tree.root.left.left = new TreeNode(3);
	tree.root.left.right = new TreeNode(10);
	tree.root.left.right.left = new TreeNode(7);
	tree.root.right.left = new TreeNode(6);
	tree.root.right.right = new TreeNode(5);
	tree.root.right.left.right = new TreeNode(4);
	tree.root.right.right.right = new TreeNode(9);
	process.stdout.write("\n Actual Binary Tree \n");
	tree.preorder(tree.root);
	mirror.root = tree.createMirror(tree.root);
	/*
	         1
	       /   \
	      8     2
	     / \   / \
	    5   6 10  3
	   /   /   \
	  9   4     7
	-----------------------
	Constructed Mirror Binary Tree 
	-----------------------
	*/
	process.stdout.write("\n Constructed Mirror Tree \n");
	tree.preorder(mirror.root);
}
main();

Output

 Actual Binary Tree
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree
  1  8  5  9  6  4  2  10  7  3
#   Python 3 Program
#   Construct a mirror tree from the given binary tree

#  Tree Node
class TreeNode :
	
	def __init__(self, data) :
		self.data = data
		self.left = None
		self.right = None
	

class BinaryTree :
	
	def __init__(self) :
		self.root = None
	
	#  Returning a mirror tree of given binary tree
	def createMirror(self, node) :
		if (node == None) :
			return None
		
		#  Create a new node
		n = TreeNode(node.data)
		#  Get the left node of created node
		n.left = self.createMirror(node.right)
		#  Get the right node of created node
		n.right = self.createMirror(node.left)
		#  Returns the constructed node
		return n
	
	#  Print tree elements
	def preorder(self, node) :
		if (node != None) :
			# Print node value
			print("  ", node.data, end = "")
			self.preorder(node.left)
			self.preorder(node.right)
		
	

def main() :
	tree = BinaryTree()
	mirror = BinaryTree()
	# 
	#          1
	#        /   \
	#       2     8
	#      / \   / \
	#     3  10 6   5
	#        /   \   \
	#       7     4   9
	#    
	#     -----------------------
	#         Binary Tree
	#     -----------------------
	#         
	
	tree.root = TreeNode(1)
	tree.root.left = TreeNode(2)
	tree.root.right = TreeNode(8)
	tree.root.left.left = TreeNode(3)
	tree.root.left.right = TreeNode(10)
	tree.root.left.right.left = TreeNode(7)
	tree.root.right.left = TreeNode(6)
	tree.root.right.right = TreeNode(5)
	tree.root.right.left.right = TreeNode(4)
	tree.root.right.right.right = TreeNode(9)
	print("\n Actual Binary Tree ")
	tree.preorder(tree.root)
	mirror.root = tree.createMirror(tree.root)
	# 
	#          1
	#        /   \
	#       8     2
	#      / \   / \
	#     5   6 10  3
	#    /   /   \
	#   9   4     7
	# -----------------------
	# Constructed Mirror Binary Tree 
	# -----------------------
	
	print("\n Constructed Mirror Tree ")
	tree.preorder(mirror.root)

if __name__ == "__main__": main()

Output

 Actual Binary Tree
   1   2   3   10   7   8   6   4   5   9
 Constructed Mirror Tree
   1   8   5   9   6   4   2   10   7   3
#   Ruby Program
#   Construct a mirror tree from the given binary tree

#  Tree Node
class TreeNode  
	# Define the accessor and reader of class TreeNode  
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
 
	
	def initialize(data) 
		self.data = data
		self.left = nil
		self.right = nil
	end

end

class BinaryTree  
	# Define the accessor and reader of class BinaryTree  
	attr_reader :root
	attr_accessor :root
 
	
	def initialize() 
		self.root = nil
	end

	#  Returning a mirror tree of given binary tree
	def createMirror(node) 
		if (node == nil) 
			return nil
		end

		#  Create a new node
		n = TreeNode.new(node.data)
		#  Get the left node of created node
		n.left = self.createMirror(node.right)
		#  Get the right node of created node
		n.right = self.createMirror(node.left)
		#  Returns the constructed node
		return n
	end

	#  Print tree elements
	def preorder(node) 
		if (node != nil) 
			# Print node value
			print("  ", node.data)
			self.preorder(node.left)
			self.preorder(node.right)
		end

	end

end

def main() 
	tree = BinaryTree.new()
	mirror = BinaryTree.new()
	# 
	#          1
	#        /   \
	#       2     8
	#      / \   / \
	#     3  10 6   5
	#        /   \   \
	#       7     4   9
	#    
	#     -----------------------
	#         Binary Tree
	#     -----------------------
	#         
	
	tree.root = TreeNode.new(1)
	tree.root.left = TreeNode.new(2)
	tree.root.right = TreeNode.new(8)
	tree.root.left.left = TreeNode.new(3)
	tree.root.left.right = TreeNode.new(10)
	tree.root.left.right.left = TreeNode.new(7)
	tree.root.right.left = TreeNode.new(6)
	tree.root.right.right = TreeNode.new(5)
	tree.root.right.left.right = TreeNode.new(4)
	tree.root.right.right.right = TreeNode.new(9)
	print("\n Actual Binary Tree \n")
	tree.preorder(tree.root)
	mirror.root = tree.createMirror(tree.root)
	# 
	#          1
	#        /   \
	#       8     2
	#      / \   / \
	#     5   6 10  3
	#    /   /   \
	#   9   4     7
	# -----------------------
	# Constructed Mirror Binary Tree 
	# -----------------------
	
	print("\n Constructed Mirror Tree \n")
	tree.preorder(mirror.root)
end

main()

Output

 Actual Binary Tree 
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree 
  1  8  5  9  6  4  2  10  7  3
/*
  Scala Program
  Construct a mirror tree from the given binary tree
*/
// Tree Node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null);
	}
	// Returning a mirror tree of given binary tree
	def createMirror(node: TreeNode): TreeNode = {
		// Returns the constructed node
		if (node == null)
		{
			return null;
		}
		// Create a new node
		var n: TreeNode = new TreeNode(node.data);
		// Get the left node of created node
		n.left = this.createMirror(node.right);
		// Get the right node of created node
		n.right = this.createMirror(node.left);
		return n;
	}
	// Print tree elements
	def preorder(node: TreeNode): Unit = {
		if (node != null)
		{
			//Print node value
			print("  " + node.data);
			this.preorder(node.left);
			this.preorder(node.right);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var tree: BinaryTree = new BinaryTree();
		var mirror: BinaryTree = new BinaryTree();
		/*
		         1
		       /   \
		      2     8
		     / \   / \
		    3  10 6   5
		       /   \   \
		      7     4   9
		   
		    -----------------------
		        Binary Tree
		    -----------------------
		        
		*/
		tree.root = new TreeNode(1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(10);
		tree.root.left.right.left = new TreeNode(7);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.right = new TreeNode(5);
		tree.root.right.left.right = new TreeNode(4);
		tree.root.right.right.right = new TreeNode(9);
		print("\n Actual Binary Tree \n");
		tree.preorder(tree.root);
		mirror.root = tree.createMirror(tree.root);
		/*
		         1
		       /   \
		      8     2
		     / \   / \
		    5   6 10  3
		   /   /   \
		  9   4     7
		-----------------------
		Constructed Mirror Binary Tree 
		-----------------------
		*/
		print("\n Constructed Mirror Tree \n");
		tree.preorder(mirror.root);
	}
}

Output

 Actual Binary Tree
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree
  1  8  5  9  6  4  2  10  7  3
/*
  Swift 4 Program
  Construct a mirror tree from the given binary tree
*/
// Tree Node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	// Returning a mirror tree of given binary tree
	func createMirror(_ node: TreeNode? )->TreeNode?
	{
		// Returns the constructed node
		if (node == nil)
		{
			return nil;
		}
		// Create a new node
		let n: TreeNode? = TreeNode(node!.data);
		// Get the left node of created node
		n!.left = self.createMirror(node!.right);
		// Get the right node of created node
		n!.right = self.createMirror(node!.left);
		return n;
	}
	// Print tree elements
	func preorder(_ node: TreeNode? )
	{
		if (node  != nil)
		{
			//Print node value
			print("  ", node!.data, terminator: "");
			self.preorder(node!.left);
			self.preorder(node!.right);
		}
	}
}
func main()
{
	let tree: BinaryTree = BinaryTree();
	let mirror: BinaryTree? = BinaryTree();
	/*
	         1
	       /   \
	      2     8
	     / \   / \
	    3  10 6   5
	       /   \   \
	      7     4   9
	   
	    -----------------------
	        Binary Tree
	    -----------------------
	        
	*/
	tree.root = TreeNode(1);
	tree.root!.left = TreeNode(2);
	tree.root!.right = TreeNode(8);
	tree.root!.left!.left = TreeNode(3);
	tree.root!.left!.right = TreeNode(10);
	tree.root!.left!.right!.left = TreeNode(7);
	tree.root!.right!.left = TreeNode(6);
	tree.root!.right!.right = TreeNode(5);
	tree.root!.right!.left!.right = TreeNode(4);
	tree.root!.right!.right!.right = TreeNode(9);
	print("\n Actual Binary Tree ");
	tree.preorder(tree.root);
	mirror!.root = tree.createMirror(tree.root);
	/*
	         1
	       /   \
	      8     2
	     / \   / \
	    5   6 10  3
	   /   /   \
	  9   4     7
	-----------------------
	Constructed Mirror Binary Tree 
	-----------------------
	*/
	print("\n Constructed Mirror Tree ");
	tree.preorder(mirror!.root);
}
main();

Output

 Actual Binary Tree
   1   2   3   10   7   8   6   4   5   9
 Constructed Mirror Tree
   1   8   5   9   6   4   2   10   7   3
/*
  Kotlin Program
  Construct a mirror tree from the given binary tree
*/
// Tree Node
class TreeNode
{
	var data: Int;
	var left: TreeNode ? ;
	var right: TreeNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	// Returning a mirror tree of given binary tree
	fun createMirror(node: TreeNode ? ): TreeNode ?
	{
		// Returns the constructed node
		if (node == null)
		{
			return null;
		}
		// Create a new node
		var n: TreeNode = TreeNode(node.data);
		// Get the left node of created node
		n.left = this.createMirror(node.right);
		// Get the right node of created node
		n.right = this.createMirror(node.left);
		return n;
	}
	// Print tree elements
	fun preorder(node: TreeNode ? ): Unit
	{
		if (node != null)
		{
			//Print node value
			print("  " + node.data);
			this.preorder(node.left);
			this.preorder(node.right);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var tree: BinaryTree = BinaryTree();
	var mirror: BinaryTree = BinaryTree();
	/*
	         1
	       /   \
	      2     8
	     / \   / \
	    3  10 6   5
	       /   \   \
	      7     4   9
	   
	    -----------------------
	        Binary Tree
	    -----------------------
	        
	*/
	tree.root = TreeNode(1);
	tree.root?.left = TreeNode(2);
	tree.root?.right = TreeNode(8);
	tree.root?.left?.left = TreeNode(3);
	tree.root?.left?.right = TreeNode(10);
	tree.root?.left?.right?.left = TreeNode(7);
	tree.root?.right?.left = TreeNode(6);
	tree.root?.right?.right = TreeNode(5);
	tree.root?.right?.left?.right = TreeNode(4);
	tree.root?.right?.right?.right = TreeNode(9);
	print("\n Actual Binary Tree \n");
	tree.preorder(tree.root);
	mirror.root = tree.createMirror(tree.root);
	/*
	         1
	       /   \
	      8     2
	     / \   / \
	    5   6 10  3
	   /   /   \
	  9   4     7
	-----------------------
	Constructed Mirror Binary Tree 
	-----------------------
	*/
	print("\n Constructed Mirror Tree \n");
	tree.preorder(mirror.root);
}

Output

 Actual Binary Tree
  1  2  3  10  7  8  6  4  5  9
 Constructed Mirror Tree
  1  8  5  9  6  4  2  10  7  3




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment