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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
import java.util.*;
import java.io.*;
public class HuffmanCode {
public HuffmanNode root;
public HuffmanCode(int[] frequencies) {
Queue<HuffmanNode> nodes = new PriorityQueue<>();
for (int i=0;i<frequencies.length;i++) {
nodes.add(new HuffmanNode((char) i, frequencies[i]));
}
while (nodes.size()>1) {
HuffmanNode left = nodes.remove();
HuffmanNode right = nodes.remove();
HuffmanNode branch = new HuffmanNode(null, left.freq + right.freq, left, right);
nodes.add(branch);
}
this.root = nodes.remove();
}
public HuffmanCode(Scanner input) {
assembleTree(input, new HuffmanNode(null, null));
}
private assembleTree(Scanner input, HuffmanNode head) {
if (input.hasNextLine()) {
char character = (char) input.nextLine();
}
if (input.hasNextLine()) {
String loc = input.nextLine();
}
if (head == null) {
head = new HuffmanNode(null, null);
}
}
public void save(PrintStream output) {
save(output, this.root)
}
private void save(PrintStream output, HuffmanNode head) {
output.println((int) head.data);
output.println() ;
save(head.left);
save(head.right);
}
public void translate(BitInputStream input, PrintStream output) {
}
private static class HuffmanNode implements Comparable<HuffmanNode> {
public final char data;
public final int freq;
public HuffmanNode left;
public HuffmanNode right;
public HuffmanNode(char data, int frequency) {
this(data, frequency, null, null);
}
public HuffmanNode(char data, int frequency, HuffmanNode left, HuffmanNode right) {
this.data = data;
this.freq = freq;
this.left = left;
this.right = right;
}
public int compareTo(HuffmanNode other) {
if (this.freq == other.freq) {
return 0;
}
else if (this.freq < other.freq) {
return -1;
}
return 1;
}
}
}
|