×

Symmetric binary tree

Implementation

// writing a C++ program to check whether a given binary tree is symmetric or not.
#include <bits/stdc++.h>
using namespace std;


// creating a binary tree node.
struct __Nod {
	int ky;
	struct __Nod *Lft, *Rt;
};


// creating a new utility function that will eventually help us in creating a new node. 
__Nod* new__Nod(int ky)
{
	__Nod* temp = new __Nod;
	temp->ky = ky;
	temp->Lft = temp->Rt = NILL;
	return (temp);
}


// this function will return the actual value if the given tree with the root nodes as root1 and root2 are their mirrors. 
bool isMirror(struct __Nod* root1, struct __Nod* root2)
{
	// In case both the trees are empty or vacant; they are mirror images of the same. 
	if (root1 == NILL && root2 == NILL)
		return true;


	//To be two trees to be identical or their mirror images then, the following conditions should be followed: - 
	// 1.) The key present in the root node should be the same. 
	// 2.) The left subtree of the left tree, along with the right subtree of the right tree, should be identical or mirror images. 
	// 3.) The right subtree of the left tree and the left subtree of the right tree should be mirror images of each other. 
	if (root1 && root2 && root1->ky == root2->ky)
		return isMirror(root1->Lft, root2->Rt)
			&& mirror(root1->Rt, root2->Lft);


	// If the above-given conditions don't match or give actual value, then root1 and 2 do not mirror images of each other.


	return false;
}


// We must return the actual value if the tree is symmetric or its mirror image. 
bool isSymmetric(struct __Nod* root)
{
	// Checking whether the tree is identical or not.
	return isMirror(root, root);
}


// writing the main code.
int main()
{
	__Nod* root = new__Nod(1);
	root->Lft = new__Nod(2);
	root->Rt = new__Nod(2);
	root->Lft->Lft = new__Nod(3);
	root->Lft->Rt = new__Nod(4);
	root->Rt->Lft = new__Nod(4);
	root->Rt->Rt = new__Nod(3);


	if (isSymmetric(root))
		cout << "Symmetric";
	else
		cout << "Not symmetric";
	return 0;
}

Output:

Symmetric binary tree

Example 2)

// writing a C program to check whether a given binary tree is symmetric or not.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// creating a binary tree node.
typedef struct __Nod {
	int ky;
	struct __Nod *Lft, *Rt;
} __Nod;
// creating a new utility function that will eventually help us in creating a new node. 
__Nod* new__Nod(int ky)
{
	__Nod* temp = (__Nod *)malloc(sizeof(__Nod));
	temp->ky = ky;
	temp->Lft = temp->Rt = NILL;
	return (temp);
}
// this function will return the actual value if the given tree with the root nodes as root1 and root2 are their mirrors. 
bool isMirror(__Nod* root1, __Nod* root2)
{
	// In case both the trees are empty or vacant; they are mirror images of the same. 
	if (root1 == NILL && root2 == NILL)
		return true;


	//To be two trees to be identical or their mirror images then, the following conditions should be followed: - 
// 1.) The key present in the root node should be the same. 
	// 2.) The left subtree of the left tree, along with the right subtree of the right tree, should be identical or mirror images. 
	// 3.) The right subtree of the left tree and the left subtree of the right tree should be mirror images of each other. 
	if (root1 && root2 && root1->ky == root2->ky)
		return isMirror(root1->Lft, root2->Rt)
			&& isMirror(root1->Rt, root2->Lft);


	// If the above-given conditions don't match or give actual value, then root1 and 2 do not mirror images of each other.
	return false;
}
// We must return the actual value if the tree is symmetric or its mirror image. 
bool isSymmetric(__Nod* root)
{
// Checking whether the tree is identical or not.
	return isMirror(root, root);
}
// writing the main code.
int main()
{
	__Nod* root = new__Nod(1);
	root->Lft = new__Nod(2);
	root->Rt = new__Nod(2);
	root->Lft->Lft = new__Nod(3);
	root->Lft->Rt = new__Nod(4);
	root->Rt->Lft = new__Nod(4);
	root->Rt->Rt = new__Nod(3);


	if (isSymmetric(root))
		printf("Symmetric");
	else
		printf("Not symmetric");
	return 0;
}

Output:

Symmetric binary tree

Example 3)

// Java program to check if a binary tree is symmetric or not
class __Nod {
	int ky;
	__Nod Lft, Rt;
	__Nod(int item)
	{
		ky = item;
		Lft = Rt = NILL;
	}
}


class BinaryTree {
	__Nod root;


	// returns true if trees with roots as root1 and
	// root2 are mirror
	boolean mirror(__Nod __Nod1, __Nod __Nod2)
	{
		// if both trees are empty, then they are the mirror image
		if (__Nod1 == NILL && __Nod2 == NILL)
			return true;


		// For two trees to be mirror images, the following
		// three conditions must be true
		// 1.) Their root __Nod's key must be the same
		// 2.) The left subtree of the left tree and the right subtree
		// of Rt tree have to be mirror images
		// 3.) Rt subtree of Lft tree and Lft subtree
		// of Rt tree have to be mirror images
		if (__Nod1 != NILL && __Nod2 != NILL
			&& __Nod1.ky == __Nod2.ky)
			return (isMirror(__Nod1.Lft, __Nod2.Rt)
					&& isMirror(__Nod1.Rt, __Nod2.Lft));


		// if none of the above conditions is true, then
		// root1 and root2 are not mirror images
		return false;
	}


	// returns true if the tree is symmetric i.e
	// mirror image of itself
	boolean isSymmetric()
	{
		// check if the tree is a mirror of itself
		return isMirror(root, root);
	}


	// Driver code
	public static void main(String args[])
	{
		BinaryTree tree = new BinaryTree();
		tree.root = new __Nod(1);
		tree.root.Lft = new __Nod(2);
		tree.root.Rt = new __Nod(2);
		tree.root.Lft.Lft = new __Nod(3);
		tree.root.Lft.Rt = new __Nod(4);
		tree.root.Rt.Lft = new __Nod(4);
		tree.root.Rt.Rt = new __Nod(3);
		boolean output = tree.isSymmetric();
		if (output == true)
			System.out.println("Symmetric");
		else
			System.out.println("Not symmetric");
	}
}

Output:

Symmetric binary tree

Related Topics

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition. Types of searching There are two...

4 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

8 minutes read.

Deletion in B+ Tree

Make a search for the leaf node that containing the key value by taking the value in a key value. If the required key value is found, then it will remove...

6 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Selection Sort

In each iteration of the selection sort algorithm, the smallest item from an unsorted list is chosen and placed at the top of the unsorted list. Algorithm of Selection Sorting In order...

3 minutes read.

Asymptotic Notation

Asymptotic notation is expressions that are used to represent the complexity of algorithms. The complexity of the algorithm is analyzed from two perspectives:  Time complexitySpace complexity Time complexity The time complexity of an algorithm is the...

3 minutes read.

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end...

3 minutes read.

Given a Generate all Structurally Unique Binary Search Trees

Implementation // Creating a C++ program that will help us build all the binary search trees for the keys from 1 to n.  #include <bits/stdc++.h> using namespace std; // creating a structure that will...

8 minutes read.

Introduction to 1D-Arrays

One Dimensional Array Technical Definitions The simplest version of an Array is a One-Dimensional Array, in which the items are stored linearly and may be accessed individually by supplying the index value...

6 minutes read.

CSS Text-indent

Text-indent The Text-indent property of CSS is used to set any first line’s indentation inside a text’s block. It describes the horizontal space amount that puts establish before the text line. It...

3 minutes read.

What is B tree?

What do you mean by B Tree in Data Structures? In the technological world, a B tree is simply a well-managed and coordinated tree and an integral part of the data...

6 minutes read.

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

4 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.