Added same trees algorithm check with a unit test (#3845)
Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:
parent
44c05bf7db
commit
351e85d264
@ -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;
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user