/
Main.java
42 lines (37 loc) · 1.26 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package playground.stanford.huffman;
public class Main {
public static void main(String[] args) throws Exception {
//String fileName = "./data/test1.txt";
String fileName = "./data/huffman.txt";
Alphabet a = new Alphabet(fileName);
Node tree = new Huffman().computeTree(a);
computeMinAndMaxLeafDepth(tree);
System.out.println("max depth: " + maxLeafDepth + ", min depth: " + minLeafDepth);
}
private static int minLeafDepth;
private static int maxLeafDepth;
private static void computeMinAndMaxLeafDepth(Node n) {
minLeafDepth = Integer.MAX_VALUE;
maxLeafDepth = Integer.MIN_VALUE;
walk(n, 0);
}
private static void walk(Node n, int depth) {
if (n.s != null) {
// we're a leaf
assert n.leftChild == null;
assert n.rightChild == null;
if (depth < minLeafDepth) {
minLeafDepth = depth;
}
if (depth > maxLeafDepth) {
maxLeafDepth = depth;
}
return;
}
// we're an internal node
assert n.leftChild != null;
assert n.rightChild != null;
walk(n.leftChild, depth + 1);
walk(n.rightChild, depth + 1);
}
}