Skip to content

Commit 4320b81

Browse files
committed
Trees
1 parent c12f30b commit 4320b81

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
*
88
* <p>Tested code against: https://uva.onlinejudge.org/external/124/p12489.pdf
99
*
10-
* <p>Time Complexity: O(Elog(E))
11-
*
1210
* @author William Fiset, william.alexandre.fiset@gmail.com
1311
*/
1412
package com.williamfiset.algorithms.graphtheory.treealgorithms;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/** Determines if two rooted trees are isomorphic. */
2+
package com.williamfiset.algorithms.graphtheory.treealgorithms;
3+
4+
import java.util.*;
5+
6+
public class TreeIsomorphismHash {
7+
public static class TreeNode {
8+
private int id;
9+
private List<TreeNode> children;
10+
11+
public TreeNode(int id) {
12+
this.id = id;
13+
children = new LinkedList<>();
14+
}
15+
16+
public void addChildren(TreeNode... nodes) {
17+
for (TreeNode node : nodes) {
18+
children.add(node);
19+
}
20+
}
21+
22+
public boolean isLeaf() {
23+
return children.size() == 0;
24+
}
25+
26+
public List<TreeNode> children() {
27+
return children;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return String.valueOf(id);
33+
}
34+
}
35+
36+
private static int MOD = 1_000_000_007;
37+
private static int PRIME = 131;
38+
private static int LEAF_VALUE = 37;
39+
40+
public static boolean areIsomorphic(TreeNode root1, TreeNode root2) {
41+
return treeHash(root1) == treeHash(root2);
42+
}
43+
44+
private static int treeHash(TreeNode node) {
45+
if (node == null || node.isLeaf()) {
46+
return LEAF_VALUE;
47+
}
48+
List<Integer> hashes = new LinkedList<>();
49+
for (TreeNode child : node.children()) {
50+
hashes.add(treeHash(child));
51+
}
52+
int hash = 1;
53+
Collections.sort(hashes);
54+
for (int h : hashes) {
55+
hash = ((PRIME * hash) ^ h) % MOD;
56+
}
57+
return hash;
58+
}
59+
60+
public static void main(String[] args) {
61+
TreeNode root1 = new TreeNode(0);
62+
TreeNode n1 = new TreeNode(1);
63+
TreeNode n2 = new TreeNode(2);
64+
TreeNode n3 = new TreeNode(3);
65+
TreeNode n4 = new TreeNode(4);
66+
TreeNode n5 = new TreeNode(5);
67+
TreeNode n6 = new TreeNode(6);
68+
TreeNode n7 = new TreeNode(7);
69+
TreeNode n8 = new TreeNode(8);
70+
TreeNode n9 = new TreeNode(9);
71+
TreeNode n10 = new TreeNode(10);
72+
root1.addChildren(n1, n4, n8);
73+
n1.addChildren(n2, n3);
74+
n4.addChildren(n5, n6, n7);
75+
n6.addChildren(n10);
76+
n8.addChildren(n9);
77+
78+
TreeNode root2 = new TreeNode(0);
79+
TreeNode x1 = new TreeNode(1);
80+
TreeNode x2 = new TreeNode(2);
81+
TreeNode x3 = new TreeNode(3);
82+
TreeNode x4 = new TreeNode(4);
83+
TreeNode x5 = new TreeNode(5);
84+
TreeNode x6 = new TreeNode(6);
85+
TreeNode x7 = new TreeNode(7);
86+
TreeNode x8 = new TreeNode(8);
87+
TreeNode x9 = new TreeNode(9);
88+
TreeNode x10 = new TreeNode(10);
89+
root2.addChildren(x4, x8, x1);
90+
x4.addChildren(x6, x5, x7);
91+
x7.addChildren(x10);
92+
x8.addChildren(x9);
93+
x1.addChildren(x3, x2);
94+
95+
System.out.println(areIsomorphic(root1, root2));
96+
}
97+
}

com/williamfiset/algorithms/graphtheory/treealgorithms/examples/TreeHeight.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public int getValue() {
3737
// root to the deepest leaf node, or -1 if the input is an empty tree.
3838
public static int treeHeight1(TreeNode node) {
3939
if (node == null) return -1;
40-
return Math.max(treeHeight1(node.left), treeHeight1(node.right)) + 1;
41-
}
40+
return Math.max(treeHeight1(node.left), treeHeight1(node.right)) + 1;
41+
}
4242

4343
// Returns the height of the binary tree which is the number of edges from the
4444
// root to the deepest leaf node, or -1 if the input is an empty tree.

slides/graphtheory/trees.key

37.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)