Skip to content

Commit 351e85d

Browse files
albina-astrDebasish Biswas
and
Debasish Biswas
authored
Added same trees algorithm check with a unit test (#3845)
Co-authored-by: Debasish Biswas <[email protected]>
1 parent 44c05bf commit 351e85d

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
/**
7+
* Given 2 binary trees.
8+
* This code checks whether they are the same (structurally identical and have the same values) or not.
9+
* <p>
10+
* Example:
11+
* 1. Binary trees:
12+
* 1 1
13+
* / \ / \
14+
* 2 3 2 3
15+
* /\ /\ /\ /\
16+
* 4 5 6 7 4 5 6 7
17+
* These trees are the same, so the code returns 'true'.
18+
* <p>
19+
* 2. Binary trees:
20+
* 1 1
21+
* / \
22+
* 2 2
23+
* These trees are NOT the same (the structure differs), so the code returns 'false'.
24+
* <p>
25+
* This solution implements the breadth-first search (BFS) algorithm.
26+
* For each tree we create a queue and iterate the trees using these queues.
27+
* On each step we check the nodes for equality, and if the nodes are not the same, return false.
28+
* Otherwise, add children nodes to the queues and continue traversing the trees.
29+
* <p>
30+
* Complexities:
31+
* O(N) - time, where N is the number of nodes in a binary tree,
32+
* O(N) - space, where N is the number of nodes in a binary tree.
33+
*
34+
* @author Albina Gimaletdinova on 13/01/2023
35+
*/
36+
public class SameTreesCheck {
37+
public static boolean check(BinaryTree.Node p, BinaryTree.Node q) {
38+
if (p == null && q == null) {
39+
return true;
40+
}
41+
if (p == null || q == null) {
42+
return false;
43+
}
44+
45+
Deque<BinaryTree.Node> q1 = new ArrayDeque<>();
46+
Deque<BinaryTree.Node> q2 = new ArrayDeque<>();
47+
q1.add(p);
48+
q2.add(q);
49+
while (!q1.isEmpty() && !q2.isEmpty()) {
50+
BinaryTree.Node first = q1.poll();
51+
BinaryTree.Node second = q2.poll();
52+
// check that some node can be null
53+
// if the check is true: both nodes are null or both nodes are not null
54+
if (!equalNodes(first, second)) return false;
55+
56+
if (first != null) {
57+
if (!equalNodes(first.left, second.left)) return false;
58+
if (first.left != null) {
59+
q1.add(first.left);
60+
q2.add(second.left);
61+
}
62+
63+
if (!equalNodes(first.right, second.right)) return false;
64+
if (first.right != null) {
65+
q1.add(first.right);
66+
q2.add(second.right);
67+
}
68+
}
69+
}
70+
return true;
71+
}
72+
73+
private static boolean equalNodes(BinaryTree.Node p, BinaryTree.Node q) {
74+
if (p == null && q == null) {
75+
return true;
76+
}
77+
if (p == null || q == null) {
78+
return false;
79+
}
80+
return p.data == q.data;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
/**
9+
* @author Albina Gimaletdinova on 12/01/2023
10+
*/
11+
public class SameTreesCheckTest {
12+
@Test
13+
public void testBothRootsAreNull() {
14+
assertTrue(SameTreesCheck.check(null, null));
15+
}
16+
17+
@Test
18+
public void testOneRootIsNull() {
19+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{100});
20+
assertFalse(SameTreesCheck.check(root, null));
21+
}
22+
23+
@Test
24+
public void testSingleNodeTreesAreSame() {
25+
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{100});
26+
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{100});
27+
assertTrue(SameTreesCheck.check(p, q));
28+
}
29+
30+
/*
31+
1 1
32+
/ \ / \
33+
2 3 2 3
34+
/\ /\ /\ /\
35+
4 5 6 7 4 5 6 7
36+
*/
37+
@Test
38+
public void testSameTreesIsSuccessful() {
39+
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
40+
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
41+
assertTrue(SameTreesCheck.check(p, q));
42+
}
43+
44+
45+
/*
46+
1 1
47+
/ \ / \
48+
2 3 2 3
49+
/\ /\ /\ /
50+
4 5 6 7 4 5 6
51+
*/
52+
@Test
53+
public void testSameTreesFails() {
54+
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
55+
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6});
56+
assertFalse(SameTreesCheck.check(p, q));
57+
}
58+
59+
/*
60+
1 1
61+
/ \
62+
2 2
63+
*/
64+
@Test
65+
public void testTreesWithDifferentStructure() {
66+
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2});
67+
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, null, 2});
68+
assertFalse(SameTreesCheck.check(p, q));
69+
}
70+
}
71+

0 commit comments

Comments
 (0)