-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanTree.java
More file actions
129 lines (119 loc) · 4.07 KB
/
HuffmanTree.java
File metadata and controls
129 lines (119 loc) · 4.07 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Mary Huibregtse
// CSE 143
// HW 8
// TA Shanti Camper Singh
// Class HuffmanTree manages Huffman coding
// to compress files
import java.util.*;
import java.io.*;
public class HuffmanTree {
private HuffmanNode overallRoot; // HuffmanTree reference
// post: constructs huffman tree given frequencies and
// corresponding character values
public HuffmanTree(int[] count) {
Queue<HuffmanNode> leafs = new PriorityQueue<HuffmanNode>();
int size = count.length;
for(int i = 0; i < size; i ++) {
if(count[i] > 0 ) {
leafs.add(new HuffmanNode(i, count[i]));
}
}
leafs.add(new HuffmanNode(size, 1));
overallRoot = buildTree(leafs);
}
// post: constructs huffman tree given frequencies and
// corresponding character values and node
private HuffmanNode buildTree(Queue<HuffmanNode> leafs) {
if (leafs.size() > 1) {
HuffmanNode left = leafs.remove();
HuffmanNode right = leafs.remove();
HuffmanNode root = new HuffmanNode(0, left.frequency +
right.frequency, left, right);
leafs.add(root);
return buildTree(leafs);
} else {
return leafs.remove();
}
}
// pre: file contains tree stored in standard from
// post: reconstructs a Huffman tree from a given
// file.
public HuffmanTree(Scanner input) {
while(input.hasNext()) {
int character = Integer.parseInt(input.nextLine());
String code = input.nextLine();
overallRoot = readTree(code, character, overallRoot);
}
}
// pre: file contains tree stored in standard from
// post: reconstructs a Huffman tree from a given
// file, character, and node.
private HuffmanNode readTreeRightNow(String code, int character,
HuffmanNode root) {
if (root == null) {
root = new HuffmanNode();
}
if(code.length()== 0) {
root.character = character;
} else {
String newCode = "";
if(code.length() > 1) {
newCode = code.substring(1);
}
if (code.charAt(0) == '0') {
root.left = readTree(newCode, character,
root.left);
}else {
root.right = readTree(newCode, character,
root.right);
}
}
return root;
}
// post: given an output stream, prints Huffman Tree in standard
// format
public void write(PrintStream output) {
String path = "";
writeHelper(output, overallRoot, path);
}
// post: given an output stream, HuffmanNode, and tree path,
// prints a HuffmanNode in standard format
private void writeHelper(PrintStream output, HuffmanNode root,
String path) {
if(root != null) {
if(root.left == null) {
output.println(root.character);
output.println(path);
}else {
writeHelper(output, root.left, path + "0");
writeHelper(output, root.right, path + "1");
}
}
}
// pre: input stream contains legal encoding of characters for
// tree, has ending eof
// post: given a BitInput stream, output stream, and end of file
// marker, writes corresponding characters to the output
public void decode(BitInputStream input, PrintStream output,
int eof) {
int value = input.readBit();
HuffmanNode root = overallRoot;
while(value != eof) {
if(root.left == null) {
if(root.character == eof) {
value = eof;
} else {
output.write(root.character);
root = overallRoot;
}
}else {
if(value == 0) {
root = root.left;
} else {
root = root.right;
}
value = input.readBit();
}
}
}
}