Site icon

Check binary tree is Quasi-Isomorphic of other tree in java (DFS / Example)

Example – Quasi-isomorphic binary trees in java.

Example 1: Given two Identical binary trees in java (Fig 1).

Fig 1: Identical binary trees

Example 2: Binary tree & mirror binary trees in java.

Fig 2: Mirror binary trees

Example 3: One binary tree is not quasi-isomorphic of other binary tree.

Fig 3: Trees are not quasi- isomorphic

Example 4: Quasi-Isomorphic binary trees are shown (Fig 4)

Fig 4: Identical & mirror binary trees
  1. Nodes A, B, C  & Nodes P,Q,R of tree1 & tree2 are identical subtree trees.
  2. Nodes D, G, H & Nodes S,V,W of tree1 & tree2 are identical subtree trees.
  3. Nodes C, E, F, I ,J & Nodes R,T,U,X,Y of tree1 & tree2 are mirror subtree of binary trees.
  4. Both trees are quasi-isomorphic.

Algorithm: check whether binary trees are quasi isomorphic (recursive).

Program – check binary trees are quasi isomorphic (recursive algorithm)

1.) QuasiIsomorphicBinaryTree :

package org.learn.Question;

public class QuasiIsomorphicBinaryTree {
	
	public static boolean quasiIsomorphicBinaryTree(Node tree1, Node tree2) {
		if(tree1 == null && tree2 == null) {
			return true;
		}
		
		if(tree1 == null || tree2 == null) {
			return false;
		}
		
		//Check identical case
		if(quasiIsomorphicBinaryTree(tree1.left,tree2.left) 
				&& quasiIsomorphicBinaryTree(tree1.right,tree2.right))
			return true;
		
		//Check mirror case
		if(quasiIsomorphicBinaryTree(tree1.left,tree2.right) 
				&& quasiIsomorphicBinaryTree(tree1.right,tree2.left))
			return true;
		
		return false;
	}
}

2.) App Class:

package org.learn.Client;

import org.learn.Question.Node;
import org.learn.Question.QuasiIsomorphicBinaryTree;

public class App 
{
	public static void main( String[] args )
    {		
		//level 0
		Node tree1 = Node.createNode(45); //A
		//level 1
		tree1.left = Node.createNode(20); //B
		tree1.right = Node.createNode(65);//C
		// left sub tree
		tree1.left.left = Node.createNode(49);//D
		tree1.left.left.left = Node.createNode(30);//G
		tree1.left.left.right = Node.createNode(45); //H

		// right subtree
		tree1.right.left = Node.createNode(30); //E
		tree1.right.right = Node.createNode(60);//F
		
		tree1.right.right.left = Node.createNode(65);//I	
		tree1.right.right.right = Node.createNode(55);//J	
		
		
		Node tree2 = Node.createNode(70); //P
		//level 1
		tree2.left = Node.createNode(55); //Q
		tree2.right = Node.createNode(85);//R
		// left sub tree
		tree2.left.left = Node.createNode(30);//S
		tree2.left.left.left = Node.createNode(80);//T
		tree2.left.left.right = Node.createNode(30); //U

		// right subtree
		tree2.right.left = Node.createNode(40); //V
		tree2.right.right = Node.createNode(65); //W
		tree2.right.left.left = Node.createNode(52); //X
		tree2.right.left.right = Node.createNode(10); //Y

		boolean isSame = QuasiIsomorphicBinaryTree.quasiIsomorphicBinaryTree(tree1, tree2);
		if(isSame) {
			System.out.println("Binary trees are Quasi-Isomorphic");
		} else {
			System.out.println("Binary trees are not Quasi-Isomorphic");
		}
    }
}

3.) Node class :

package org.learn.Question;

public class Node {
	public int data;
	public Node left;
	public Node right;

	public Node(int num) {
		this.data = num;
		this.left = null;
		this.right = null;
	}

	public Node() {
		this.left = null;
		this.right = null;
	}

	public static Node createNode(int number) {
		return new Node(number);
	}
}

Output – check binary trees are quasi isomorphic in java

Binary trees are Quasi-Isomorphic

Download Code – Quasi-Isomorphic binary trees (recursive algorithm)

 

Exit mobile version