Added same trees algorithm check with a unit test (#3845)

Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:
Albina Gimaletdinova 2023-01-13 23:07:56 +03:00 committed by GitHub
parent 44c05bf7db
commit 351e85d264
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,82 @@
package com.thealgorithms.datastructures.trees;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* Given 2 binary trees.
* This code checks whether they are the same (structurally identical and have the same values) or not.
* <p>
* Example:
* 1. Binary trees:
* 1 1
* / \ / \
* 2 3 2 3
* /\ /\ /\ /\
* 4 5 6 7 4 5 6 7
* These trees are the same, so the code returns 'true'.
* <p>
* 2. Binary trees:
* 1 1
* / \
* 2 2
* These trees are NOT the same (the structure differs), so the code returns 'false'.
* <p>
* This solution implements the breadth-first search (BFS) algorithm.
* For each tree we create a queue and iterate the trees using these queues.
* On each step we check the nodes for equality, and if the nodes are not the same, return false.
* Otherwise, add children nodes to the queues and continue traversing the trees.
* <p>
* Complexities:
* O(N) - time, where N is the number of nodes in a binary tree,
* O(N) - space, where N is the number of nodes in a binary tree.
*
* @author Albina Gimaletdinova on 13/01/2023
*/
public class SameTreesCheck {
public static boolean check(BinaryTree.Node p, BinaryTree.Node q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
Deque<BinaryTree.Node> q1 = new ArrayDeque<>();
Deque<BinaryTree.Node> q2 = new ArrayDeque<>();
q1.add(p);
q2.add(q);
while (!q1.isEmpty() && !q2.isEmpty()) {
BinaryTree.Node first = q1.poll();
BinaryTree.Node second = q2.poll();
// check that some node can be null
// if the check is true: both nodes are null or both nodes are not null
if (!equalNodes(first, second)) return false;
if (first != null) {
if (!equalNodes(first.left, second.left)) return false;
if (first.left != null) {
q1.add(first.left);
q2.add(second.left);
}
if (!equalNodes(first.right, second.right)) return false;
if (first.right != null) {
q1.add(first.right);
q2.add(second.right);
}
}
}
return true;
}
private static boolean equalNodes(BinaryTree.Node p, BinaryTree.Node q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
return p.data == q.data;
}
}

View File

@ -0,0 +1,71 @@
package com.thealgorithms.datastructures.trees;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Albina Gimaletdinova on 12/01/2023
*/
public class SameTreesCheckTest {
@Test
public void testBothRootsAreNull() {
assertTrue(SameTreesCheck.check(null, null));
}
@Test
public void testOneRootIsNull() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{100});
assertFalse(SameTreesCheck.check(root, null));
}
@Test
public void testSingleNodeTreesAreSame() {
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{100});
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{100});
assertTrue(SameTreesCheck.check(p, q));
}
/*
1 1
/ \ / \
2 3 2 3
/\ /\ /\ /\
4 5 6 7 4 5 6 7
*/
@Test
public void testSameTreesIsSuccessful() {
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
assertTrue(SameTreesCheck.check(p, q));
}
/*
1 1
/ \ / \
2 3 2 3
/\ /\ /\ /
4 5 6 7 4 5 6
*/
@Test
public void testSameTreesFails() {
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6});
assertFalse(SameTreesCheck.check(p, q));
}
/*
1 1
/ \
2 2
*/
@Test
public void testTreesWithDifferentStructure() {
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2});
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, null, 2});
assertFalse(SameTreesCheck.check(p, q));
}
}