Check if two trees are mirror
Here given code implementation process.
/*
C Program
Check if two trees are mirror
*/
#include <stdio.h>
#include <stdlib.h>
//Binary Tree node
struct Node
{
int data;
struct Node *left, *right;
};
//This is creating a binary tree node and return this
struct Node *get_node(int data)
{
// Create dynamic node
struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
if (new_node != NULL)
{
//Set data and pointer values
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return new_node;
}
//Display pre order elements
void preorder(struct Node *node)
{
if (node != NULL)
{
//Print node value
printf(" %d", node->data);
preorder(node->left);
preorder(node->right);
}
}
//Determine whether given trees are mirror tree to each other or not
int check_mirror_tree(struct Node *first, struct Node *second)
{
if (first == NULL && second == NULL)
{
//When both tree node is empty
return 1;
}
else if (first == NULL || second == NULL || first->data != second->data)
{
//When the node of either tree is empty or when the node values are not equals.
return 0;
}
//Recursively checking mirror nodes
return check_mirror_tree(first->left, second->right) && check_mirror_tree(first->right, second->left);
}
//Handles the request to display mirror tree result
void is_mirror_trees(struct Node *first, struct Node *second)
{
// Display the node elements of given binary tree
printf("\n Given First Tree \n");
preorder(first);
printf("\n Given Second Tree \n");
preorder(second);
printf("\n -------------------");
if (check_mirror_tree(first, second))
{
//When Trees are mirrors
printf("\n Trees are Mirror \n\n");
}
else
{
printf("\n Trees are not Mirror \n\n");
}
}
int main()
{
struct Node *root1 = NULL;
struct Node *root2 = NULL;
struct Node *root3 = NULL;
/*
constructor binary tree
-----------------
10
/ \
2 3
/ \ \
8 7 1
/
6
-----------------
First Tree
*/
root1 = get_node(10);
root1->left = get_node(2);
root1->left->left = get_node(8);
root1->left->right = get_node(7);
root1->left->right->left = get_node(6);
root1->right = get_node(3);
root1->right->right = get_node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\
6
-----------------
Second Tree
*/
root2 = get_node(10);
root2->right = get_node(2);
root2->right->right = get_node(8);
root2->right->left = get_node(7);
root2->right->left->right = get_node(6);
root2->left = get_node(3);
root2->left->left = get_node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\ \
1 6
-----------------
Third Tree
*/
root3 = get_node(10);
root3->right = get_node(2);
root3->right->right = get_node(8);
root3->right->left = get_node(7);
root3->right->left->right = get_node(6);
root3->left = get_node(3);
root3->left->left = get_node(1);
root3->left->left->right = get_node(1);
// Test Case
is_mirror_trees(root1, root2);
is_mirror_trees(root1, root3);
return 0;
}
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
/*
Java Program
Check if two trees are mirror
*/
//Binary Tree node
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public Node root;
public BinaryTree()
{
//Set initial tree root to null
this.root = null;
}
//Display pre order elements
public void preorder(Node node)
{
if (node != null)
{
//Print node value
System.out.print(" " + node.data);
preorder(node.left);
preorder(node.right);
}
}
//Determine whether given trees are mirror tree to each other or not
public boolean check_mirror_tree(Node first, Node second)
{
if (first == null && second == null)
{
//When both tree node is empty
return true;
}
else if (first == null || second == null || first.data != second.data)
{
//When the node of either tree is empty or when the node values are not equals.
return false;
}
//Recursively checking mirror nodes
return check_mirror_tree(first.left, second.right) && check_mirror_tree(first.right, second.left);
}
//Handles the request to display mirror tree result
public void is_mirror_trees(Node other)
{
// Display the node elements of given binary tree
System.out.print("\n Given First Tree \n");
preorder(this.root);
System.out.print("\n Given Second Tree \n");
preorder(other);
System.out.print("\n -------------------");
if (check_mirror_tree(this.root, other))
{
//When Trees are mirrors
System.out.print("\n Trees are Mirror \n\n");
}
else
{
System.out.print("\n Trees are not Mirror \n\n");
}
}
public static void main(String[] args)
{
//Create tree objects
BinaryTree tree1 = new BinaryTree();
BinaryTree tree2 = new BinaryTree();
BinaryTree tree3 = new BinaryTree();
/*
constructor binary tree
-----------------
10
/ \
2 3
/ \ \
8 7 1
/
6
-----------------
First Tree
*/
tree1.root = new Node(10);
tree1.root.left = new Node(2);
tree1.root.left.left = new Node(8);
tree1.root.left.right = new Node(7);
tree1.root.left.right.left = new Node(6);
tree1.root.right = new Node(3);
tree1.root.right.right = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\
6
-----------------
Second Tree
*/
tree2.root = new Node(10);
tree2.root.right = new Node(2);
tree2.root.right.right = new Node(8);
tree2.root.right.left = new Node(7);
tree2.root.right.left.right = new Node(6);
tree2.root.left = new Node(3);
tree2.root.left.left = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\ \
1 6
-----------------
Third Tree
*/
tree3.root = new Node(10);
tree3.root.right = new Node(2);
tree3.root.right.right = new Node(8);
tree3.root.right.left = new Node(7);
tree3.root.right.left.right = new Node(6);
tree3.root.left = new Node(3);
tree3.root.left.left = new Node(1);
tree3.root.left.left.right = new Node(1);
// Test Case
tree1.is_mirror_trees(tree2.root);
tree1.is_mirror_trees(tree3.root);
}
}
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Check if two trees are mirror
*/
// Binary Tree node
class Node
{
public: int data;
Node *left;
Node *right;
Node(int data)
{
// Set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BinaryTree
{
public: Node *root;
BinaryTree()
{
// Set initial tree root to null
this->root = NULL;
}
// Display pre order elements
void preorder(Node *node)
{
if (node != NULL)
{
// Print node value
cout << " " << node->data;
this->preorder(node->left);
this->preorder(node->right);
}
}
// Determine whether given trees are mirror tree to each other or not
bool check_mirror_tree(Node *first, Node *second)
{
if (first == NULL && second == NULL)
{
// When both tree node is empty
return true;
}
else if (first == NULL || second == NULL || first->data != second->data)
{
// When the node of either tree is empty or when the node values are not equals.
return false;
}
// Recursively checking mirror nodes
return this->check_mirror_tree(first->left, second->right) && this->check_mirror_tree(first->right, second->left);
}
// Handles the request to display mirror tree result
void is_mirror_trees(Node *other)
{
// Display the node elements of given binary tree
cout << "\n Given First Tree \n";
this->preorder(this->root);
cout << "\n Given Second Tree \n";
this->preorder(other);
cout << "\n -------------------";
if (this->check_mirror_tree(this->root, other))
{
// When Trees are mirrors
cout << "\n Trees are Mirror \n\n";
}
else
{
cout << "\n Trees are not Mirror \n\n";
}
}
};
int main()
{
// Create tree objects
BinaryTree tree1 = BinaryTree();
BinaryTree *tree2 = new BinaryTree();
BinaryTree *tree3 = new BinaryTree();
/*
constructor binary tree
-----------------
10
/ \
2 3
/ \ \
8 7 1
/
6
-----------------
First Tree
*/
tree1.root = new Node(10);
tree1.root->left = new Node(2);
tree1.root->left->left = new Node(8);
tree1.root->left->right = new Node(7);
tree1.root->left->right->left = new Node(6);
tree1.root->right = new Node(3);
tree1.root->right->right = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\
6
-----------------
Second Tree
*/
tree2->root = new Node(10);
tree2->root->right = new Node(2);
tree2->root->right->right = new Node(8);
tree2->root->right->left = new Node(7);
tree2->root->right->left->right = new Node(6);
tree2->root->left = new Node(3);
tree2->root->left->left = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\ \
1 6
-----------------
Third Tree
*/
tree3->root = new Node(10);
tree3->root->right = new Node(2);
tree3->root->right->right = new Node(8);
tree3->root->right->left = new Node(7);
tree3->root->right->left->right = new Node(6);
tree3->root->left = new Node(3);
tree3->root->left->left = new Node(1);
tree3->root->left->left->right = new Node(1);
// Test Case
tree1.is_mirror_trees(tree2->root);
tree1.is_mirror_trees(tree3->root);
return 0;
}
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
// Include namespace system
using System;
/*
C# Program
Check if two trees are mirror
*/
// Binary Tree node
public class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public Node root;
public BinaryTree()
{
// Set initial tree root to null
this.root = null;
}
// Display pre order elements
public void preorder(Node node)
{
if (node != null)
{
// Print node value
Console.Write(" " + node.data);
preorder(node.left);
preorder(node.right);
}
}
// Determine whether given trees are mirror tree to each other or not
public Boolean check_mirror_tree(Node first, Node second)
{
if (first == null && second == null)
{
// When both tree node is empty
return true;
}
else if (first == null || second == null || first.data != second.data)
{
// When the node of either tree is empty or when the node values are not equals.
return false;
}
// Recursively checking mirror nodes
return check_mirror_tree(first.left, second.right) && check_mirror_tree(first.right, second.left);
}
// Handles the request to display mirror tree result
public void is_mirror_trees(Node other)
{
// Display the node elements of given binary tree
Console.Write("\n Given First Tree \n");
preorder(this.root);
Console.Write("\n Given Second Tree \n");
preorder(other);
Console.Write("\n -------------------");
if (check_mirror_tree(this.root, other))
{
// When Trees are mirrors
Console.Write("\n Trees are Mirror \n\n");
}
else
{
Console.Write("\n Trees are not Mirror \n\n");
}
}
public static void Main(String[] args)
{
// Create tree objects
BinaryTree tree1 = new BinaryTree();
BinaryTree tree2 = new BinaryTree();
BinaryTree tree3 = new BinaryTree();
/*
constructor binary tree
-----------------
10
/ \
2 3
/ \ \
8 7 1
/
6
-----------------
First Tree
*/
tree1.root = new Node(10);
tree1.root.left = new Node(2);
tree1.root.left.left = new Node(8);
tree1.root.left.right = new Node(7);
tree1.root.left.right.left = new Node(6);
tree1.root.right = new Node(3);
tree1.root.right.right = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\
6
-----------------
Second Tree
*/
tree2.root = new Node(10);
tree2.root.right = new Node(2);
tree2.root.right.right = new Node(8);
tree2.root.right.left = new Node(7);
tree2.root.right.left.right = new Node(6);
tree2.root.left = new Node(3);
tree2.root.left.left = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\ \
1 6
-----------------
Third Tree
*/
tree3.root = new Node(10);
tree3.root.right = new Node(2);
tree3.root.right.right = new Node(8);
tree3.root.right.left = new Node(7);
tree3.root.right.left.right = new Node(6);
tree3.root.left = new Node(3);
tree3.root.left.left = new Node(1);
tree3.root.left.left.right = new Node(1);
// Test Case
tree1.is_mirror_trees(tree2.root);
tree1.is_mirror_trees(tree3.root);
}
}
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
<?php
/*
Php Program
Check if two trees are mirror
*/
// Binary Tree node
class Node
{
public $data;
public $left;
public $right;
function __construct($data)
{
// Set node value
$this->data = $data;
$this->left = null;
$this->right = null;
}
}
class BinaryTree
{
public $root;
function __construct()
{
// Set initial tree root to null
$this->root = null;
}
// Display pre order elements
public function preorder($node)
{
if ($node != null)
{
// Print node value
echo " ". $node->data;
$this->preorder($node->left);
$this->preorder($node->right);
}
}
// Determine whether given trees are mirror tree to each other or not
public function check_mirror_tree($first, $second)
{
if ($first == null && $second == null)
{
// When both tree node is empty
return true;
}
else if ($first == null || $second == null || $first->data != $second->data)
{
// When the node of either tree is empty or when the node values are not equals.
return false;
}
// Recursively checking mirror nodes
return $this->check_mirror_tree($first->left, $second->right) && $this->check_mirror_tree($first->right, $second->left);
}
// Handles the request to display mirror tree result
public function is_mirror_trees($other)
{
// Display the node elements of given binary tree
echo "\n Given First Tree \n";
$this->preorder($this->root);
echo "\n Given Second Tree \n";
$this->preorder($other);
echo "\n -------------------";
if ($this->check_mirror_tree($this->root, $other))
{
// When Trees are mirrors
echo "\n Trees are Mirror \n\n";
}
else
{
echo "\n Trees are not Mirror \n\n";
}
}
}
function main()
{
// Create tree objects
$tree1 = new BinaryTree();
$tree2 = new BinaryTree();
$tree3 = new BinaryTree();
/*
constructor binary tree
-----------------
10
/ \
2 3
/ \ \
8 7 1
/
6
-----------------
First Tree
*/
$tree1->root = new Node(10);
$tree1->root->left = new Node(2);
$tree1->root->left->left = new Node(8);
$tree1->root->left->right = new Node(7);
$tree1->root->left->right->left = new Node(6);
$tree1->root->right = new Node(3);
$tree1->root->right->right = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\
6
-----------------
Second Tree
*/
$tree2->root = new Node(10);
$tree2->root->right = new Node(2);
$tree2->root->right->right = new Node(8);
$tree2->root->right->left = new Node(7);
$tree2->root->right->left->right = new Node(6);
$tree2->root->left = new Node(3);
$tree2->root->left->left = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\ \
1 6
-----------------
Third Tree
*/
$tree3->root = new Node(10);
$tree3->root->right = new Node(2);
$tree3->root->right->right = new Node(8);
$tree3->root->right->left = new Node(7);
$tree3->root->right->left->right = new Node(6);
$tree3->root->left = new Node(3);
$tree3->root->left->left = new Node(1);
$tree3->root->left->left->right = new Node(1);
// Test Case
$tree1->is_mirror_trees($tree2->root);
$tree1->is_mirror_trees($tree3->root);
}
main();
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
/*
Node Js Program
Check if two trees are mirror
*/
// Binary Tree node
class Node
{
constructor(data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
constructor()
{
// Set initial tree root to null
this.root = null;
}
// Display pre order elements
preorder(node)
{
if (node != null)
{
// Print node value
process.stdout.write(" " + node.data);
this.preorder(node.left);
this.preorder(node.right);
}
}
// Determine whether given trees are mirror tree to each other or not
check_mirror_tree(first, second)
{
if (first == null && second == null)
{
// When both tree node is empty
return true;
}
else if (first == null || second == null || first.data != second.data)
{
// When the node of either tree is empty or when the node values are not equals.
return false;
}
// Recursively checking mirror nodes
return this.check_mirror_tree(first.left, second.right) && this.check_mirror_tree(first.right, second.left);
}
// Handles the request to display mirror tree result
is_mirror_trees(other)
{
// Display the node elements of given binary tree
process.stdout.write("\n Given First Tree \n");
this.preorder(this.root);
process.stdout.write("\n Given Second Tree \n");
this.preorder(other);
process.stdout.write("\n -------------------");
if (this.check_mirror_tree(this.root, other))
{
// When Trees are mirrors
process.stdout.write("\n Trees are Mirror \n\n");
}
else
{
process.stdout.write("\n Trees are not Mirror \n\n");
}
}
}
function main()
{
// Create tree objects
var tree1 = new BinaryTree();
var tree2 = new BinaryTree();
var tree3 = new BinaryTree();
/*
constructor binary tree
-----------------
10
/ \
2 3
/ \ \
8 7 1
/
6
-----------------
First Tree
*/
tree1.root = new Node(10);
tree1.root.left = new Node(2);
tree1.root.left.left = new Node(8);
tree1.root.left.right = new Node(7);
tree1.root.left.right.left = new Node(6);
tree1.root.right = new Node(3);
tree1.root.right.right = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\
6
-----------------
Second Tree
*/
tree2.root = new Node(10);
tree2.root.right = new Node(2);
tree2.root.right.right = new Node(8);
tree2.root.right.left = new Node(7);
tree2.root.right.left.right = new Node(6);
tree2.root.left = new Node(3);
tree2.root.left.left = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\ \
1 6
-----------------
Third Tree
*/
tree3.root = new Node(10);
tree3.root.right = new Node(2);
tree3.root.right.right = new Node(8);
tree3.root.right.left = new Node(7);
tree3.root.right.left.right = new Node(6);
tree3.root.left = new Node(3);
tree3.root.left.left = new Node(1);
tree3.root.left.left.right = new Node(1);
// Test Case
tree1.is_mirror_trees(tree2.root);
tree1.is_mirror_trees(tree3.root);
}
main();
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
# Python 3 Program
# Check if two trees are mirror
# Binary Tree node
class Node :
def __init__(self, data) :
# Set node value
self.data = data
self.left = None
self.right = None
class BinaryTree :
def __init__(self) :
# Set initial tree root to null
self.root = None
# Display pre order elements
def preorder(self, node) :
if (node != None) :
# Print node value
print(" ", node.data, end = "")
self.preorder(node.left)
self.preorder(node.right)
# Determine whether given trees are mirror tree to each other or not
def check_mirror_tree(self, first, second) :
if (first == None and second == None) :
# When both tree node is empty
return True
elif(first == None or second == None or first.data != second.data) :
# When the node of either tree is empty or when the node values are not equals.
return False
# Recursively checking mirror nodes
return self.check_mirror_tree(first.left, second.right) and self.check_mirror_tree(first.right, second.left)
# Handles the request to display mirror tree result
def is_mirror_trees(self, other) :
# Display the node elements of given binary tree
print("\n Given First Tree \n", end = "")
self.preorder(self.root)
print("\n Given Second Tree \n", end = "")
self.preorder(other)
print("\n -------------------", end = "")
if (self.check_mirror_tree(self.root, other)) :
# When Trees are mirrors
print("\n Trees are Mirror \n\n", end = "")
else :
print("\n Trees are not Mirror \n\n", end = "")
def main() :
# Create tree objects
tree1 = BinaryTree()
tree2 = BinaryTree()
tree3 = BinaryTree()
#
# constructor binary tree
# -----------------
# 10
# / \
# 2 3
# / \ \
# 8 7 1
# /
# 6
# -----------------
# First Tree
#
tree1.root = Node(10)
tree1.root.left = Node(2)
tree1.root.left.left = Node(8)
tree1.root.left.right = Node(7)
tree1.root.left.right.left = Node(6)
tree1.root.right = Node(3)
tree1.root.right.right = Node(1)
#
# constructor binary tree
# -----------------
# 10
# / \
# 3 2
# / / \
# 1 7 8
# \
# 6
#
# -----------------
# Second Tree
#
tree2.root = Node(10)
tree2.root.right = Node(2)
tree2.root.right.right = Node(8)
tree2.root.right.left = Node(7)
tree2.root.right.left.right = Node(6)
tree2.root.left = Node(3)
tree2.root.left.left = Node(1)
#
# constructor binary tree
# -----------------
# 10
# / \
# 3 2
# / / \
# 1 7 8
# \ \
# 1 6
#
# -----------------
# Third Tree
#
tree3.root = Node(10)
tree3.root.right = Node(2)
tree3.root.right.right = Node(8)
tree3.root.right.left = Node(7)
tree3.root.right.left.right = Node(6)
tree3.root.left = Node(3)
tree3.root.left.left = Node(1)
tree3.root.left.left.right = Node(1)
# Test Case
tree1.is_mirror_trees(tree2.root)
tree1.is_mirror_trees(tree3.root)
if __name__ == "__main__": main()
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
# Ruby Program
# Check if two trees are mirror
# Binary Tree node
class Node
# Define the accessor and reader of class Node
attr_reader :data, :left, :right
attr_accessor :data, :left, :right
def initialize(data)
# Set node value
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()
# Set initial tree root to null
self.root = nil
end
# Display pre order elements
def preorder(node)
if (node != nil)
# Print node value
print(" ", node.data)
self.preorder(node.left)
self.preorder(node.right)
end
end
# Determine whether given trees are mirror tree to each other or not
def check_mirror_tree(first, second)
if (first == nil && second == nil)
# When both tree node is empty
return true
elsif(first == nil || second == nil || first.data != second.data)
# When the node of either tree is empty or when the node values are not equals.
return false
end
# Recursively checking mirror nodes
return self.check_mirror_tree(first.left, second.right) && self.check_mirror_tree(first.right, second.left)
end
# Handles the request to display mirror tree result
def is_mirror_trees(other)
# Display the node elements of given binary tree
print("\n Given First Tree \n")
self.preorder(self.root)
print("\n Given Second Tree \n")
self.preorder(other)
print("\n -------------------")
if (self.check_mirror_tree(self.root, other))
# When Trees are mirrors
print("\n Trees are Mirror \n\n")
else
print("\n Trees are not Mirror \n\n")
end
end
end
def main()
# Create tree objects
tree1 = BinaryTree.new()
tree2 = BinaryTree.new()
tree3 = BinaryTree.new()
#
# constructor binary tree
# -----------------
# 10
# / \
# 2 3
# / \ \
# 8 7 1
# /
# 6
# -----------------
# First Tree
#
tree1.root = Node.new(10)
tree1.root.left = Node.new(2)
tree1.root.left.left = Node.new(8)
tree1.root.left.right = Node.new(7)
tree1.root.left.right.left = Node.new(6)
tree1.root.right = Node.new(3)
tree1.root.right.right = Node.new(1)
#
# constructor binary tree
# -----------------
# 10
# / \
# 3 2
# / / \
# 1 7 8
# \
# 6
#
# -----------------
# Second Tree
#
tree2.root = Node.new(10)
tree2.root.right = Node.new(2)
tree2.root.right.right = Node.new(8)
tree2.root.right.left = Node.new(7)
tree2.root.right.left.right = Node.new(6)
tree2.root.left = Node.new(3)
tree2.root.left.left = Node.new(1)
#
# constructor binary tree
# -----------------
# 10
# / \
# 3 2
# / / \
# 1 7 8
# \ \
# 1 6
#
# -----------------
# Third Tree
#
tree3.root = Node.new(10)
tree3.root.right = Node.new(2)
tree3.root.right.right = Node.new(8)
tree3.root.right.left = Node.new(7)
tree3.root.right.left.right = Node.new(6)
tree3.root.left = Node.new(3)
tree3.root.left.left = Node.new(1)
tree3.root.left.left.right = Node.new(1)
# Test Case
tree1.is_mirror_trees(tree2.root)
tree1.is_mirror_trees(tree3.root)
end
main()
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
/*
Scala Program
Check if two trees are mirror
*/
// Binary Tree node
class Node(var data: Int , var left: Node , var right: Node)
{
def this(data: Int)
{
this(data, null, null);
}
}
class BinaryTree(var root: Node)
{
def this()
{
this(null);
}
// Display pre order elements
def preorder(node: Node): Unit = {
if (node != null)
{
// Print node value
print(" " + node.data);
preorder(node.left);
preorder(node.right);
}
}
// Determine whether given trees are mirror tree to each other or not
def check_mirror_tree(first: Node, second: Node): Boolean = {
if (first == null && second == null)
{
// When both tree node is empty
return true;
}
else if (first == null || second == null || first.data != second.data)
{
// When the node of either tree is empty or when the node values are not equals.
return false;
}
// Recursively checking mirror nodes
return check_mirror_tree(first.left, second.right) && check_mirror_tree(first.right, second.left);
}
// Handles the request to display mirror tree result
def is_mirror_trees(other: Node): Unit = {
// Display the node elements of given binary tree
print("\n Given First Tree \n");
preorder(this.root);
print("\n Given Second Tree \n");
preorder(other);
print("\n -------------------");
if (check_mirror_tree(this.root, other))
{
// When Trees are mirrors
print("\n Trees are Mirror \n\n");
}
else
{
print("\n Trees are not Mirror \n\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Create tree objects
var tree1: BinaryTree = new BinaryTree();
var tree2: BinaryTree = new BinaryTree();
var tree3: BinaryTree = new BinaryTree();
/*
constructor binary tree
-----------------
10
/ \
2 3
/ \ \
8 7 1
/
6
-----------------
First Tree
*/
tree1.root = new Node(10);
tree1.root.left = new Node(2);
tree1.root.left.left = new Node(8);
tree1.root.left.right = new Node(7);
tree1.root.left.right.left = new Node(6);
tree1.root.right = new Node(3);
tree1.root.right.right = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\
6
-----------------
Second Tree
*/
tree2.root = new Node(10);
tree2.root.right = new Node(2);
tree2.root.right.right = new Node(8);
tree2.root.right.left = new Node(7);
tree2.root.right.left.right = new Node(6);
tree2.root.left = new Node(3);
tree2.root.left.left = new Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\ \
1 6
-----------------
Third Tree
*/
tree3.root = new Node(10);
tree3.root.right = new Node(2);
tree3.root.right.right = new Node(8);
tree3.root.right.left = new Node(7);
tree3.root.right.left.right = new Node(6);
tree3.root.left = new Node(3);
tree3.root.left.left = new Node(1);
tree3.root.left.left.right = new Node(1);
// Test Case
tree1.is_mirror_trees(tree2.root);
tree1.is_mirror_trees(tree3.root);
}
}
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
/*
Swift 4 Program
Check if two trees are mirror
*/
// Binary Tree node
class Node
{
var data: Int;
var left: Node? ;
var right: Node? ;
init(_ data: Int)
{
// Set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
class BinaryTree
{
var root: Node? ;
init()
{
// Set initial tree root to null
self.root = nil;
}
// Display pre order elements
func preorder(_ node: Node? )
{
if (node != nil)
{
// Print node value
print(" ", node!.data, terminator: "");
self.preorder(node!.left);
self.preorder(node!.right);
}
}
// Determine whether given trees are mirror tree to each other or not
func check_mirror_tree(_ first: Node? , _ second : Node? )->Bool
{
if (first == nil && second == nil)
{
// When both tree node is empty
return true;
}
else if (first == nil || second == nil || first!.data != second!.data)
{
// When the node of either tree is empty or when the node values are not equals.
return false;
}
// Recursively checking mirror nodes
return self.check_mirror_tree(first!.left, second!.right) && self.check_mirror_tree(first!.right, second!.left);
}
// Handles the request to display mirror tree result
func is_mirror_trees(_ other: Node? )
{
// Display the node elements of given binary tree
print("\n Given First Tree \n", terminator: "");
self.preorder(self.root);
print("\n Given Second Tree \n", terminator: "");
self.preorder(other);
print("\n -------------------", terminator: "");
if (self.check_mirror_tree(self.root, other))
{
// When Trees are mirrors
print("\n Trees are Mirror \n\n", terminator: "");
}
else
{
print("\n Trees are not Mirror \n\n", terminator: "");
}
}
}
func main()
{
// Create tree objects
let tree1: BinaryTree = BinaryTree();
let tree2: BinaryTree? = BinaryTree();
let tree3: BinaryTree? = BinaryTree();
/*
constructor binary tree
-----------------
10
/ \
2 3
/ \ \
8 7 1
/
6
-----------------
First Tree
*/
tree1.root = Node(10);
tree1.root!.left = Node(2);
tree1.root!.left!.left = Node(8);
tree1.root!.left!.right = Node(7);
tree1.root!.left!.right!.left = Node(6);
tree1.root!.right = Node(3);
tree1.root!.right!.right = Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\
6
-----------------
Second Tree
*/
tree2!.root = Node(10);
tree2!.root!.right = Node(2);
tree2!.root!.right!.right = Node(8);
tree2!.root!.right!.left = Node(7);
tree2!.root!.right!.left!.right = Node(6);
tree2!.root!.left = Node(3);
tree2!.root!.left!.left = Node(1);
/*
constructor binary tree
-----------------
10
/ \
3 2
/ / \
1 7 8
\ \
1 6
-----------------
Third Tree
*/
tree3!.root = Node(10);
tree3!.root!.right = Node(2);
tree3!.root!.right!.right = Node(8);
tree3!.root!.right!.left = Node(7);
tree3!.root!.right!.left!.right = Node(6);
tree3!.root!.left = Node(3);
tree3!.root!.left!.left = Node(1);
tree3!.root!.left!.left!.right = Node(1);
// Test Case
tree1.is_mirror_trees(tree2!.root);
tree1.is_mirror_trees(tree3!.root);
}
main();
Output
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 2 7 6 8
-------------------
Trees are Mirror
Given First Tree
10 2 8 7 6 3 1
Given Second Tree
10 3 1 1 2 7 6 8
-------------------
Trees are not Mirror
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