Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced B Tree in Data Structure Convert Sorted List to Binary Search Tree Flattening a Linked List Given a Perfect Binary Tree, Reverse Alternate Levels Left View of Binary Tree What are Forest Trees in Data Structure Compare Balanced Binary Tree and Complete Binary Tree Diameter of a Binary Tree Given a Binary Tree Check the Zig Zag Traversal Given a Binary Tree Print the Shortest Path Given a Binary Tree Return All Root To Leaf Paths Given a Binary Tree Swap Nodes at K Height Given a Binary Tree Find Its Minimum Depth Given a Binary Tree Print the Pre Order Traversal in Recursive Given a Generate all Structurally Unique Binary Search Trees Perfect Binary Tree Threaded Binary Trees

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