Skip to content

Commit 74e3a87

Browse files
committed
Tree isomorphism
1 parent 9762fd6 commit 74e3a87

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

com/williamfiset/algorithms/graphtheory/treealgorithms/TreeIsomorphism.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static boolean areIsomorphic(TreeNode root1, TreeNode root2) {
127127
}
128128

129129
// Constructs the canonical form representation of a tree as a string.
130-
private static String encode(TreeNode node) {
130+
public static String encode(TreeNode node) {
131131
if (node == null) {
132132
return "()";
133133
}

com/williamfiset/algorithms/graphtheory/treealgorithms/TreeIsomorphismWithBfs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static String encodeTree(List<List<Integer>> tree) {
139139

140140
// Two nodes remain and we need to combine their labels
141141
String l2 = map[leafs.get(1)];
142-
return (l1.compareTo(l2) < 0) ? (l1 + l2) : (l2 + l1);
142+
return ((l1.compareTo(l2) < 0) ? (l1 + l2) : (l2 + l1));
143143
}
144144

145145
public static boolean treesAreIsomorphic(List<List<Integer>> tree1, List<List<Integer>> tree2) {

javatests/com/williamfiset/algorithms/graphtheory/treealgorithms/TreeIsomorphismWithBfsTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
// To run this test in isolation from root folder:
2+
//
3+
// $ gradle test --tests
4+
// javatests.com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphismWithBfsTest
5+
16
package javatests.com.williamfiset.algorithms.graphtheory.treealgorithms;
27

38
import static com.google.common.truth.Truth.assertThat;
49
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphismWithBfs.addUndirectedEdge;
510
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphismWithBfs.createEmptyTree;
611
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphismWithBfs.encodeTree;
712
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphismWithBfs.treesAreIsomorphic;
13+
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphism.TreeNode;
814

915
import java.util.*;
1016
import org.junit.*;
@@ -119,4 +125,59 @@ public void testSlidesExample() {
119125
String expectedEncoding = "(((()())(()())())((())()()())(()()()))";
120126
assertThat(treeEncoding).isEqualTo(expectedEncoding);
121127
}
128+
129+
@Test
130+
public void t() {
131+
List<List<Integer>> tree = createEmptyTree(10);
132+
133+
TreeNode node0 = new TreeNode(0);
134+
TreeNode node1 = new TreeNode(1);
135+
TreeNode node2 = new TreeNode(2);
136+
TreeNode node3 = new TreeNode(3);
137+
TreeNode node4 = new TreeNode(4);
138+
TreeNode node5 = new TreeNode(5);
139+
TreeNode node6 = new TreeNode(6);
140+
TreeNode node7 = new TreeNode(7);
141+
TreeNode node8 = new TreeNode(8);
142+
TreeNode node9 = new TreeNode(9);
143+
144+
node0.addChildren(node1, node2, node3);
145+
node1.addChildren(node4, node5);
146+
node5.addChildren(node9);
147+
node2.addChildren(node6, node7);
148+
node3.addChildren(node8);
149+
150+
System.out.println(com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphism.encode(node0));
151+
152+
// (((())())(()())(()))
153+
// ((())())
154+
// (()())
155+
// (())
156+
//
157+
158+
// (()())
159+
// (())
160+
// (())
161+
162+
// ((()())(()))
163+
// ((())())
164+
//
165+
// ((()())(()))((())())
166+
167+
168+
// (((()())(()))((())()))
169+
// (()())
170+
// (())
171+
//
172+
// ((())())
173+
//
174+
}
122175
}
176+
177+
178+
179+
180+
181+
182+
183+

slides/graphtheory/trees_slides.key

59.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)