Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}