Check For Symmetric Binary Tree

Here given code implementation process.

/*
    C Program 
    Check for Symmetric Binary Tree
*/
#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 tree are mirror tree or not
int is_mirror_tree(struct Node *left_side, struct Node *right_side)
{
	if (left_side == NULL && right_side == NULL)
	{
		//When tree node is empty
		return 1;
	}
	else if (left_side == NULL || right_side == NULL || left_side->data != right_side->data)
	{
		//When subtree are empty or when the node values are not same
		return 0;
	}
	//recursively checking mirror nodes
	return is_mirror_tree(left_side->left, right_side->right) && is_mirror_tree(left_side->right, right_side->left);
}
//Handles the request to display symmetric tree result
void check_symmetric_tree(struct Node *root)
{
	// Display the node elements of given binary tree
	printf("\n Given Tree \n");
	preorder(root);
	if (is_mirror_tree(root, root))
	{
		//When tree are symmetric
		printf("\n Tree are Symmetric \n\n");
	}
	else
	{
		printf("\n Tree are not Symmetric \n\n");
	}
}
int main()
{
	struct Node *root1 = NULL;
	struct Node *root2 = NULL;
	struct Node *root3 = NULL;
	/*
	constructor binary tree
	-----------------
	    10                            
	   /   \    
	  2     2     
	 /       \               
	8         8   
	-----------------
	First Tree
	*/
	root1 = get_node(10);
	root1->left = get_node(2);
	root1->left->left = get_node(8);
	root1->right = get_node(2);
	root1->right->right = get_node(8);
	/*
	constructor binary tree
	-----------------
	    10                            
	   /   \    
	  3     3     
	 /     /  \               
	8     7    8
	       
	-----------------
	Second Tree
	*/
	root2 = get_node(10);
	root2->right = get_node(3);
	root2->right->right = get_node(8);
	root2->right->left = get_node(7);
	root2->left = get_node(3);
	root2->left->left = get_node(8);
	/*
	constructor binary tree
	-----------------
	    20                            
	   /   \    
	  3     3     
	 /        \               
	1          1
	 \        /
	  6      6  
	        
	-----------------
	Third Tree
	*/
	root3 = get_node(30);
	root3->right = get_node(3);
	root3->right->right = get_node(1);
	root3->right->right->left = get_node(6);
	root3->left = get_node(3);
	root3->left->left = get_node(1);
	root3->left->left->right = get_node(6);
	//  Test Case
	check_symmetric_tree(root1);
	check_symmetric_tree(root2);
	check_symmetric_tree(root3);
	return 0;
}

Output

 Given Tree
  10  2  8  2  8
 Tree are Symmetric


 Given Tree
  10  3  8  3  7  8
 Tree are not Symmetric


 Given Tree
  30  3  1  6  3  1  6
 Tree are Symmetric
/*
    Java Program 
    Check for Symmetric Binary Tree
*/

// 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 tree are mirror tree or not
	public boolean is_mirror_tree(Node left_side, Node right_side)
	{
		if (left_side == null && right_side == null)
		{
			//When tree node is empty
			return true;
		}
		else if (left_side == null || right_side == null || left_side.data != right_side.data)
		{
			//When subtree are empty or when the node values are not same
			return false;
		}
		//recursively checking mirror nodes
		return is_mirror_tree(left_side.left, right_side.right) && is_mirror_tree(left_side.right, right_side.left);
	}
	//Handles the request to display symmetric tree result
	public void check_symmetric_tree()
	{
		// Display the node elements of given binary tree
		System.out.print("\n Given Tree \n");
		preorder(root);
		if (is_mirror_tree(this.root, this.root))
		{
			//When tree are symmetric
			System.out.print("\n Tree are Symmetric \n\n");
		}
		else
		{
			System.out.print("\n Tree are not Symmetric \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     2     
		 /       \               
		8         8   
		-----------------
		First Tree
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.right = new Node(2);
		tree1.root.right.right = new Node(8);
		/*
		    constructor binary tree
		    -----------------
		        10                            
		       /   \    
		      3     3     
		     /     /  \               
		    8     7    8
		           
		    -----------------
		    Second Tree
		*/
		tree2.root = new Node(10);
		tree2.root.right = new Node(3);
		tree2.root.right.right = new Node(8);
		tree2.root.right.left = new Node(7);
		tree2.root.left = new Node(3);
		tree2.root.left.left = new Node(8);
		/*
		    constructor binary tree
		    -----------------
		        20                            
		       /   \    
		      3     3     
		     /        \               
		    1          1
		     \        /
		      6      6  
		            
		    -----------------
		    Third Tree
		*/
		tree3.root = new Node(30);
		tree3.root.right = new Node(3);
		tree3.root.right.right = new Node(1);
		tree3.root.right.right.left = new Node(6);
		tree3.root.left = new Node(3);
		tree3.root.left.left = new Node(1);
		tree3.root.left.left.right = new Node(6);
		//  Test Cases
		tree1.check_symmetric_tree();
		tree2.check_symmetric_tree();
		tree3.check_symmetric_tree();
	}
}

Output

 Given Tree
  10  2  8  2  8
 Tree are Symmetric


 Given Tree
  10  3  8  3  7  8
 Tree are not Symmetric


 Given Tree
  30  3  1  6  3  1  6
 Tree are Symmetric
// Include header file
#include <iostream>
using namespace std;

/*
     C++ Program 
     Check for Symmetric Binary Tree
*/

//  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 tree are mirror tree or not
	bool is_mirror_tree(Node *left_side, Node *right_side)
	{
		if (left_side == NULL && right_side == NULL)
		{
			// When tree node is empty
			return true;
		}
		else if (left_side == NULL || right_side == NULL 
                 || left_side->data != right_side->data)
		{
			// When subtree are empty or when the node values are not same
			return false;
		}
		// recursively checking mirror nodes
		return this->is_mirror_tree(left_side->left, right_side->right) 
      && this->is_mirror_tree(left_side->right, right_side->left);
	}
	// Handles the request to display symmetric tree result
	void check_symmetric_tree()
	{
		//  Display the node elements of given binary tree
		cout << "\n Given Tree \n";
		this->preorder(this->root);
		if (this->is_mirror_tree(this->root, this->root))
		{
			// When tree are symmetric
			cout << "\n Tree are Symmetric \n\n";
		}
		else
		{
			cout << "\n Tree are not Symmetric \n\n";
		}
	}
};
int main()
{
	// Create tree objects
	BinaryTree tree1 = BinaryTree();
	BinaryTree tree2 = BinaryTree();
	BinaryTree tree3 = BinaryTree();
	/*
	  		constructor binary tree
	  		-----------------
	  		    10                            
	  		   /   \    
	  		  2     2     
	  		 /       \               
	  		8         8   
	  		-----------------
	  		First Tree
	*/
	tree1.root = new Node(10);
	tree1.root->left = new Node(2);
	tree1.root->left->left = new Node(8);
	tree1.root->right = new Node(2);
	tree1.root->right->right = new Node(8);
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      3     3     
	  		     /     /  \               
	  		    8     7    8
	  		    -----------------
	  		    Second Tree
	*/
	tree2.root = new Node(10);
	tree2.root->right = new Node(3);
	tree2.root->right->right = new Node(8);
	tree2.root->right->left = new Node(7);
	tree2.root->left = new Node(3);
	tree2.root->left->left = new Node(8);
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        20                            
	  		       /   \    
	  		      3     3     
	  		     /        \               
	  		    1          1
	  		     \        /
	  		      6      6  
	  		    -----------------
	  		    Third Tree
	*/
	tree3.root = new Node(30);
	tree3.root->right = new Node(3);
	tree3.root->right->right = new Node(1);
	tree3.root->right->right->left = new Node(6);
	tree3.root->left = new Node(3);
	tree3.root->left->left = new Node(1);
	tree3.root->left->left->right = new Node(6);
	//   Test Cases
	tree1.check_symmetric_tree();
	tree2.check_symmetric_tree();
	tree3.check_symmetric_tree();
	return 0;
}

Output

 Given Tree
  10  2  8  2  8
 Tree are Symmetric


 Given Tree
  10  3  8  3  7  8
 Tree are not Symmetric


 Given Tree
  30  3  1  6  3  1  6
 Tree are Symmetric
// Include namespace system
using System;

/*
     C# Program 
     Check for Symmetric Binary Tree
*/

//  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 tree are mirror tree or not
	public Boolean is_mirror_tree(Node left_side, Node right_side)
	{
		if (left_side == null && right_side == null)
		{
			// When tree node is empty
			return true;
		}
		else if (left_side == null || right_side == null || left_side.data != right_side.data)
		{
			// When subtree are empty or when the node values are not same
			return false;
		}
		// recursively checking mirror nodes
		return is_mirror_tree(left_side.left, right_side.right) && is_mirror_tree(left_side.right, right_side.left);
	}
	// Handles the request to display symmetric tree result
	public void check_symmetric_tree()
	{
		//  Display the node elements of given binary tree
		Console.Write("\n Given Tree \n");
		preorder(root);
		if (is_mirror_tree(this.root, this.root))
		{
			// When tree are symmetric
			Console.Write("\n Tree are Symmetric \n\n");
		}
		else
		{
			Console.Write("\n Tree are not Symmetric \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     2     
		  		 /       \               
		  		8         8   
		  		-----------------
		  		First Tree
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.right = new Node(2);
		tree1.root.right.right = new Node(8);
		/*
		  		    constructor binary tree
		  		    -----------------
		  		        10                            
		  		       /   \    
		  		      3     3     
		  		     /     /  \               
		  		    8     7    8
		  		    -----------------
		  		    Second Tree
		*/
		tree2.root = new Node(10);
		tree2.root.right = new Node(3);
		tree2.root.right.right = new Node(8);
		tree2.root.right.left = new Node(7);
		tree2.root.left = new Node(3);
		tree2.root.left.left = new Node(8);
		/*
		  		    constructor binary tree
		  		    -----------------
		  		        20                            
		  		       /   \    
		  		      3     3     
		  		     /        \               
		  		    1          1
		  		     \        /
		  		      6      6  
		  		    -----------------
		  		    Third Tree
		*/
		tree3.root = new Node(30);
		tree3.root.right = new Node(3);
		tree3.root.right.right = new Node(1);
		tree3.root.right.right.left = new Node(6);
		tree3.root.left = new Node(3);
		tree3.root.left.left = new Node(1);
		tree3.root.left.left.right = new Node(6);
		//   Test Cases
		tree1.check_symmetric_tree();
		tree2.check_symmetric_tree();
		tree3.check_symmetric_tree();
	}
}

Output

 Given Tree
  10  2  8  2  8
 Tree are Symmetric


 Given Tree
  10  3  8  3  7  8
 Tree are not Symmetric


 Given Tree
  30  3  1  6  3  1  6
 Tree are Symmetric
<?php
/*
     Php Program 
     Check for Symmetric Binary Tree
*/

//  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 tree are mirror tree or not
	public	function is_mirror_tree($left_side, $right_side)
	{
		if ($left_side == null && $right_side == null)
		{
			// When tree node is empty
			return true;
		}
		else if ($left_side == null || $right_side == null || $left_side->data != $right_side->data)
		{
			// When subtree are empty or when the node values are not same
			return false;
		}
		// recursively checking mirror nodes
		return $this->is_mirror_tree($left_side->left, $right_side->right) && $this->is_mirror_tree($left_side->right, $right_side->left);
	}
	// Handles the request to display symmetric tree result
	public	function check_symmetric_tree()
	{
		//  Display the node elements of given binary tree
		echo "\n Given Tree \n";
		$this->preorder($this->root);
		if ($this->is_mirror_tree($this->root, $this->root))
		{
			// When tree are symmetric
			echo "\n Tree are Symmetric \n\n";
		}
		else
		{
			echo "\n Tree are not Symmetric \n\n";
		}
	}
}

function main()
{
	// Create tree objects
	$tree1 = new BinaryTree();
	$tree2 = new BinaryTree();
	$tree3 = new BinaryTree();
	/*
	  		constructor binary tree
	  		-----------------
	  		    10                            
	  		   /   \    
	  		  2     2     
	  		 /       \               
	  		8         8   
	  		-----------------
	  		First Tree
	*/
	$tree1->root = new Node(10);
	$tree1->root->left = new Node(2);
	$tree1->root->left->left = new Node(8);
	$tree1->root->right = new Node(2);
	$tree1->root->right->right = new Node(8);
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      3     3     
	  		     /     /  \               
	  		    8     7    8
	  		    -----------------
	  		    Second Tree
	*/
	$tree2->root = new Node(10);
	$tree2->root->right = new Node(3);
	$tree2->root->right->right = new Node(8);
	$tree2->root->right->left = new Node(7);
	$tree2->root->left = new Node(3);
	$tree2->root->left->left = new Node(8);
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        20                            
	  		       /   \    
	  		      3     3     
	  		     /        \               
	  		    1          1
	  		     \        /
	  		      6      6  
	  		    -----------------
	  		    Third Tree
	*/
	$tree3->root = new Node(30);
	$tree3->root->right = new Node(3);
	$tree3->root->right->right = new Node(1);
	$tree3->root->right->right->left = new Node(6);
	$tree3->root->left = new Node(3);
	$tree3->root->left->left = new Node(1);
	$tree3->root->left->left->right = new Node(6);
	//   Test Cases
	$tree1->check_symmetric_tree();
	$tree2->check_symmetric_tree();
	$tree3->check_symmetric_tree();
}
main();

Output

 Given Tree
  10  2  8  2  8
 Tree are Symmetric


 Given Tree
  10  3  8  3  7  8
 Tree are not Symmetric


 Given Tree
  30  3  1  6  3  1  6
 Tree are Symmetric
/*
     Node Js Program 
     Check for Symmetric Binary Tree
*/
//  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 tree are mirror tree or not
	is_mirror_tree(left_side, right_side)
	{
		if (left_side == null && right_side == null)
		{
			// When tree node is empty
			return true;
		}
		else if (left_side == null || right_side == null || left_side.data != right_side.data)
		{
			// When subtree are empty or when the node values are not same
			return false;
		}
		// recursively checking mirror nodes
		return this.is_mirror_tree(left_side.left, right_side.right) && this.is_mirror_tree(left_side.right, right_side.left);
	}
	// Handles the request to display symmetric tree result
	check_symmetric_tree()
	{
		//  Display the node elements of given binary tree
		process.stdout.write("\n Given Tree \n");
		this.preorder(this.root);
		if (this.is_mirror_tree(this.root, this.root))
		{
			// When tree are symmetric
			process.stdout.write("\n Tree are Symmetric \n\n");
		}
		else
		{
			process.stdout.write("\n Tree are not Symmetric \n\n");
		}
	}
}

function main()
{
	// Create tree objects
	var tree1 = new BinaryTree();
	var tree2 = new BinaryTree();
	var tree3 = new BinaryTree();
	/*
	  		constructor binary tree
	  		-----------------
	  		    10                            
	  		   /   \    
	  		  2     2     
	  		 /       \               
	  		8         8   
	  		-----------------
	  		First Tree
	*/
	tree1.root = new Node(10);
	tree1.root.left = new Node(2);
	tree1.root.left.left = new Node(8);
	tree1.root.right = new Node(2);
	tree1.root.right.right = new Node(8);
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      3     3     
	  		     /     /  \               
	  		    8     7    8
	  		    -----------------
	  		    Second Tree
	*/
	tree2.root = new Node(10);
	tree2.root.right = new Node(3);
	tree2.root.right.right = new Node(8);
	tree2.root.right.left = new Node(7);
	tree2.root.left = new Node(3);
	tree2.root.left.left = new Node(8);
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        20                            
	  		       /   \    
	  		      3     3     
	  		     /        \               
	  		    1          1
	  		     \        /
	  		      6      6  
	  		    -----------------
	  		    Third Tree
	*/
	tree3.root = new Node(30);
	tree3.root.right = new Node(3);
	tree3.root.right.right = new Node(1);
	tree3.root.right.right.left = new Node(6);
	tree3.root.left = new Node(3);
	tree3.root.left.left = new Node(1);
	tree3.root.left.left.right = new Node(6);
	//   Test Cases
	tree1.check_symmetric_tree();
	tree2.check_symmetric_tree();
	tree3.check_symmetric_tree();
}
main();

Output

 Given Tree
  10  2  8  2  8
 Tree are Symmetric


 Given Tree
  10  3  8  3  7  8
 Tree are not Symmetric


 Given Tree
  30  3  1  6  3  1  6
 Tree are Symmetric
#     Python 3 Program 
#     Check for Symmetric Binary Tree

#  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 tree are mirror tree or not
	def is_mirror_tree(self, left_side, right_side) :
		if (left_side == None and right_side == None) :
			# When tree node is empty
			return True
		
		elif(left_side == None or right_side == None or left_side.data != right_side.data) :
			# When subtree are empty or when the node values are not same
			return False
		
		# recursively checking mirror nodes
		return self.is_mirror_tree(left_side.left, right_side.right) and self.is_mirror_tree(left_side.right, right_side.left)
	
	# Handles the request to display symmetric tree result
	def check_symmetric_tree(self) :
		#  Display the node elements of given binary tree
		print("\n Given Tree \n", end = "")
		self.preorder(self.root)
		if (self.is_mirror_tree(self.root, self.root)) :
			# When tree are symmetric
			print("\n Tree are Symmetric \n\n", end = "")
		else :
			print("\n Tree are not Symmetric \n\n", end = "")
		
	

def main() :
	# Create tree objects
	tree1 = BinaryTree()
	tree2 = BinaryTree()
	tree3 = BinaryTree()
	# 
	# 		constructor binary tree
	# 		-----------------
	# 		    10                            
	# 		   /   \    
	# 		  2     2     
	# 		 /       \               
	# 		8         8   
	# 		-----------------
	# 		First Tree
	# 		
	
	tree1.root = Node(10)
	tree1.root.left = Node(2)
	tree1.root.left.left = Node(8)
	tree1.root.right = Node(2)
	tree1.root.right.right = Node(8)
	# 
	# 		    constructor binary tree
	# 		    -----------------
	# 		        10                            
	# 		       /   \    
	# 		      3     3     
	# 		     /     /  \               
	# 		    8     7    8
	# 		           
	# 		    -----------------
	# 		    Second Tree
	# 		
	
	tree2.root = Node(10)
	tree2.root.right = Node(3)
	tree2.root.right.right = Node(8)
	tree2.root.right.left = Node(7)
	tree2.root.left = Node(3)
	tree2.root.left.left = Node(8)
	# 
	# 		    constructor binary tree
	# 		    -----------------
	# 		        20                            
	# 		       /   \    
	# 		      3     3     
	# 		     /        \               
	# 		    1          1
	# 		     \        /
	# 		      6      6  
	# 		            
	# 		    -----------------
	# 		    Third Tree
	# 		
	
	tree3.root = Node(30)
	tree3.root.right = Node(3)
	tree3.root.right.right = Node(1)
	tree3.root.right.right.left = Node(6)
	tree3.root.left = Node(3)
	tree3.root.left.left = Node(1)
	tree3.root.left.left.right = Node(6)
	#   Test Cases
	tree1.check_symmetric_tree()
	tree2.check_symmetric_tree()
	tree3.check_symmetric_tree()

if __name__ == "__main__": main()

Output

 Given Tree
   10   2   8   2   8
 Tree are Symmetric


 Given Tree
   10   3   8   3   7   8
 Tree are not Symmetric


 Given Tree
   30   3   1   6   3   1   6
 Tree are Symmetric
#     Ruby Program 
#     Check for Symmetric Binary Tree

#  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 tree are mirror tree or not
	def is_mirror_tree(left_side, right_side) 
		if (left_side == nil && right_side == nil) 
			# When tree node is empty
			return true
		elsif(left_side == nil || right_side == nil || left_side.data != right_side.data) 
			# When subtree are empty or when the node values are not same
			return false
		end

		# recursively checking mirror nodes
		return self.is_mirror_tree(left_side.left, right_side.right) && self.is_mirror_tree(left_side.right, right_side.left)
	end

	# Handles the request to display symmetric tree result
	def check_symmetric_tree() 
		#  Display the node elements of given binary tree
		print("\n Given Tree \n")
		self.preorder(root)
		if (self.is_mirror_tree(self.root, self.root)) 
			# When tree are symmetric
			print("\n Tree are Symmetric \n\n")
		else 
			print("\n Tree are not Symmetric \n\n")
		end

	end

end

def main() 
	# Create tree objects
	tree1 = BinaryTree.new()
	tree2 = BinaryTree.new()
	tree3 = BinaryTree.new()
	# 
	# 		constructor binary tree
	# 		-----------------
	# 		    10                            
	# 		   /   \    
	# 		  2     2     
	# 		 /       \               
	# 		8         8   
	# 		-----------------
	# 		First Tree
	# 		
	
	tree1.root = Node.new(10)
	tree1.root.left = Node.new(2)
	tree1.root.left.left = Node.new(8)
	tree1.root.right = Node.new(2)
	tree1.root.right.right = Node.new(8)
	# 
	# 		    constructor binary tree
	# 		    -----------------
	# 		        10                            
	# 		       /   \    
	# 		      3     3     
	# 		     /     /  \               
	# 		    8     7    8
	# 		           
	# 		    -----------------
	# 		    Second Tree
	# 		
	
	tree2.root = Node.new(10)
	tree2.root.right = Node.new(3)
	tree2.root.right.right = Node.new(8)
	tree2.root.right.left = Node.new(7)
	tree2.root.left = Node.new(3)
	tree2.root.left.left = Node.new(8)
	# 
	# 		    constructor binary tree
	# 		    -----------------
	# 		        20                            
	# 		       /   \    
	# 		      3     3     
	# 		     /        \               
	# 		    1          1
	# 		     \        /
	# 		      6      6  
	# 		            
	# 		    -----------------
	# 		    Third Tree
	# 		
	
	tree3.root = Node.new(30)
	tree3.root.right = Node.new(3)
	tree3.root.right.right = Node.new(1)
	tree3.root.right.right.left = Node.new(6)
	tree3.root.left = Node.new(3)
	tree3.root.left.left = Node.new(1)
	tree3.root.left.left.right = Node.new(6)
	#   Test Cases
	tree1.check_symmetric_tree()
	tree2.check_symmetric_tree()
	tree3.check_symmetric_tree()
end

main()

Output

 Given Tree 
  10  2  8  2  8
 Tree are Symmetric 


 Given Tree 
  10  3  8  3  7  8
 Tree are not Symmetric 


 Given Tree 
  30  3  1  6  3  1  6
 Tree are Symmetric 

/*
     Scala Program 
     Check for Symmetric Binary Tree
*/

//  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 tree are mirror tree or not
	def is_mirror_tree(left_side: Node, right_side: Node): Boolean = {
		if (left_side == null && right_side == null)
		{
			// When tree node is empty
			return true;
		}
		else if (left_side == null || right_side == null || left_side.data != right_side.data)
		{
			// When subtree are empty or when the node values are not same
			return false;
		}
		// recursively checking mirror nodes
		return is_mirror_tree(left_side.left, right_side.right) && is_mirror_tree(left_side.right, right_side.left);
	}
	// Handles the request to display symmetric tree result
	def check_symmetric_tree(): Unit = {
		//  Display the node elements of given binary tree
		print("\n Given Tree \n");
		preorder(root);
		if (is_mirror_tree(this.root, this.root))
		{
			// When tree are symmetric
			print("\n Tree are Symmetric \n\n");
		}
		else
		{
			print("\n Tree are not Symmetric \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     2     
		  		 /       \               
		  		8         8   
		  		-----------------
		  		First Tree
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.right = new Node(2);
		tree1.root.right.right = new Node(8);
		/*
		  		    constructor binary tree
		  		    -----------------
		  		        10                            
		  		       /   \    
		  		      3     3     
		  		     /     /  \               
		  		    8     7    8
		  		    -----------------
		  		    Second Tree
		*/
		tree2.root = new Node(10);
		tree2.root.right = new Node(3);
		tree2.root.right.right = new Node(8);
		tree2.root.right.left = new Node(7);
		tree2.root.left = new Node(3);
		tree2.root.left.left = new Node(8);
		/*
		  		    constructor binary tree
		  		    -----------------
		  		        20                            
		  		       /   \    
		  		      3     3     
		  		     /        \               
		  		    1          1
		  		     \        /
		  		      6      6  
		  		    -----------------
		  		    Third Tree
		*/
		tree3.root = new Node(30);
		tree3.root.right = new Node(3);
		tree3.root.right.right = new Node(1);
		tree3.root.right.right.left = new Node(6);
		tree3.root.left = new Node(3);
		tree3.root.left.left = new Node(1);
		tree3.root.left.left.right = new Node(6);
		//   Test Cases
		tree1.check_symmetric_tree();
		tree2.check_symmetric_tree();
		tree3.check_symmetric_tree();
	}
}

Output

 Given Tree
  10  2  8  2  8
 Tree are Symmetric


 Given Tree
  10  3  8  3  7  8
 Tree are not Symmetric


 Given Tree
  30  3  1  6  3  1  6
 Tree are Symmetric
/*
     Swift 4 Program 
     Check for Symmetric Binary Tree
*/

//  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 tree are mirror tree or not
	func is_mirror_tree(_ left_side: Node? , _ right_side : Node? )->Bool
	{
		if (left_side == nil && right_side == nil)
		{
			// When tree node is empty
			return true;
		}
		else if (left_side == nil || right_side == nil || left_side!.data != right_side!.data)
		{
			// When subtree are empty or when the node values are not same
			return false;
		}
		// recursively checking mirror nodes
		return self.is_mirror_tree(left_side!.left, right_side!.right) && self.is_mirror_tree(left_side!.right, right_side!.left);
	}
	// Handles the request to display symmetric tree result
	func check_symmetric_tree()
	{
		//  Display the node elements of given binary tree
		print("\n Given Tree \n", terminator: "");
		self.preorder(self.root);
		if (self.is_mirror_tree(self.root, self.root))
		{
			// When tree are symmetric
			print("\n Tree are Symmetric \n\n", terminator: "");
		}
		else
		{
			print("\n Tree are not Symmetric \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     2     
  		 /       \               
  		8         8   
  		-----------------
  		First Tree
*/
	tree1.root = Node(10);
	tree1.root!.left = Node(2);
	tree1.root!.left!.left = Node(8);
	tree1.root!.right = Node(2);
	tree1.root!.right!.right = Node(8);
	/*
  		    constructor binary tree
  		    -----------------
  		        10                            
  		       /   \    
  		      3     3     
  		     /     /  \               
  		    8     7    8
  		    -----------------
  		    Second Tree
*/
	tree2.root = Node(10);
	tree2.root!.right = Node(3);
	tree2.root!.right!.right = Node(8);
	tree2.root!.right!.left = Node(7);
	tree2.root!.left = Node(3);
	tree2.root!.left!.left = Node(8);
	/*
  		    constructor binary tree
  		    -----------------
  		        20                            
  		       /   \    
  		      3     3     
  		     /        \               
  		    1          1
  		     \        /
  		      6      6  
  		    -----------------
  		    Third Tree
*/
	tree3.root = Node(30);
	tree3.root!.right = Node(3);
	tree3.root!.right!.right = Node(1);
	tree3.root!.right!.right!.left = Node(6);
	tree3.root!.left = Node(3);
	tree3.root!.left!.left = Node(1);
	tree3.root!.left!.left!.right = Node(6);
	//   Test Cases
	tree1.check_symmetric_tree();
	tree2.check_symmetric_tree();
	tree3.check_symmetric_tree();
}
main();

Output

 Given Tree
   10   2   8   2   8
 Tree are Symmetric


 Given Tree
   10   3   8   3   7   8
 Tree are not Symmetric


 Given Tree
   30   3   1   6   3   1   6
 Tree are Symmetric


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







© 2021, kalkicode.com, All rights reserved