Skip to content

Commit 4ea314d

Browse files
committed
Added solution to find if a tree is subtree of given tree
1 parent 5ab3f3b commit 4ea314d

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.eprogrammerz.examples.algorithm.trees;
2+
3+
import org.junit.Test;
4+
5+
import static junit.framework.TestCase.assertFalse;
6+
import static junit.framework.TestCase.assertTrue;
7+
8+
public class CheckSubtree {
9+
10+
@Test
11+
public void testSubtree() {
12+
13+
/**
14+
* 10
15+
* / \
16+
* 7 12
17+
* / \ \
18+
* 4 8 15
19+
* \
20+
* 17
21+
*
22+
*/
23+
24+
Node root = new Node(10);
25+
root.left = new Node(7);
26+
root.right = new Node(12);
27+
root.left.left = new Node(4);
28+
root.left.right = new Node(8);
29+
root.right.right = new Node(15);
30+
root.right.right.right = new Node(17);
31+
32+
/**
33+
* 7
34+
* / \
35+
* 4 8
36+
*/
37+
Node that = new Node(7);
38+
that.left = new Node(4);
39+
that.right = new Node(8);
40+
41+
assertTrue(root.isSubtree(that));
42+
43+
/**
44+
* 7
45+
* / \
46+
* 4 8
47+
* \
48+
* 10
49+
*/
50+
that.left.right = new Node(10);
51+
assertFalse(root.isSubtree(that));
52+
}
53+
}

src/main/java/com/eprogrammerz/examples/algorithm/trees/Node.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.eprogrammerz.examples.algorithm.trees;
22

3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
36
public class Node {
47
int data;
58
Node left, right;
@@ -60,6 +63,64 @@ public boolean validate() {
6063
return true;
6164
}
6265

66+
/**
67+
* Checks if tree is subtree of current tree
68+
*
69+
* @return true if tree is subtree, else false
70+
*/
71+
public boolean isSubtree(Node that) {
72+
// do bfs on this
73+
// if node found same as of that.root
74+
// then clear the queue of tree
75+
// start bfs for both, each queue poll should match
76+
77+
if (that == null) return false;
78+
79+
Queue<Node> thisQueue = new LinkedList<>();
80+
thisQueue.add(this);
81+
82+
while (!thisQueue.isEmpty()) {
83+
Node thisNode = thisQueue.poll();
84+
85+
if (thisNode.data == that.data) {
86+
return isSubtree(thisNode, that);
87+
}
88+
89+
if (thisNode.left != null) {
90+
thisQueue.add(thisNode.left);
91+
}
92+
93+
if (thisNode.right != null) {
94+
thisQueue.add(thisNode.right);
95+
}
96+
}
97+
98+
return false;
99+
}
100+
101+
private boolean isSubtree(Node t1, Node t2) {
102+
103+
Queue<Node> t1Queue = new LinkedList<>();
104+
t1Queue.add(t1);
105+
106+
Queue<Node> t2Queue = new LinkedList<>();
107+
t2Queue.add(t2);
108+
109+
while (!t1Queue.isEmpty() && !t2Queue.isEmpty()) {
110+
Node t1Node = t1Queue.poll();
111+
Node t2Node = t2Queue.poll();
112+
113+
if (t1Node.data != t2Node.data) return false;
114+
115+
if (t1Node.left != null) t1Queue.add(t1Node.left);
116+
if (t1Node.right != null) t1Queue.add(t1Node.right);
117+
118+
if (t2Node.left != null) t2Queue.add(t2Node.left);
119+
if (t2Node.right != null) t2Queue.add(t2Node.right);
120+
}
121+
return t1Queue.isEmpty() && t2Queue.isEmpty();
122+
}
123+
63124
@Override
64125
public String toString() {
65126
return "(" + data + ")";

0 commit comments

Comments
 (0)