-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffmantree.java
More file actions
142 lines (115 loc) · 3 KB
/
huffmantree.java
File metadata and controls
142 lines (115 loc) · 3 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
130
131
132
133
134
135
136
137
138
139
140
141
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class huffNode{
//class node that has right node, left node, and value
private class node{
String chr;
node left;
node right;
//constructor that sets value when given one
node(String value){
chr = value;
left = null;
right = null;
}
}
//instance variables
node root = new node(null);
node pointer = root;
//get input on one line
String getInput(Scanner scan) {
String inp = "";
while(scan.hasNextLine()) {
inp += scan.nextLine();
}
return inp+"0";
}
//builds huffman code tree by placing 0s on left if null and 1s on right if null
public void treeBuilder(Scanner scan) {
String placer = "";
while (scan.hasNextLine()) {
placer = scan.nextLine();
String[] arr = placer.split(":");
String code = arr[1];
pointer = root;
for(int i = 0; i < arr[1].length(); i++) {
if (code.charAt(i) == '0') {
if(pointer.left == null) {
node newNode = new node("0");
pointer.left = newNode;
pointer = pointer.left;
}
else {
pointer = pointer.left;
}
}
if (code.charAt(i) == '1') {
if(pointer.right == null) {
node newNode = new node("1");
pointer.right = newNode;
pointer = pointer.right;
}
else {
pointer = pointer.right;
}
}
}
char val = (char) Integer.parseInt(arr[0]);
String val2 = Character.toString(val);
pointer.chr = val2;
}
}
//decodes the 0s and 1s, going down the tree untill it hits a leaf (end)
String treeDecode(String line){
String dec = "";
pointer = root;
for(int i = 0; i < line.length(); i++) {
if(pointer.left == null && pointer.right == null) {
dec += pointer.chr;
pointer = root;
}
if(line.charAt(i) == '0'){
pointer = pointer.left;
}
else if(line.charAt(i) == '1') {
pointer = pointer.right;
}
}
return dec;
}
}
public class Decode {
public static void main(String[] args) {
try {
//create the files
File cB = new File("codebook.txt");
File input = new File(args[0]);
FileWriter writer = new FileWriter(args[1]);
//create the scanners
Scanner scan1 = new Scanner(cB);
Scanner scan2 = new Scanner(input);
huffNode huff = new huffNode();
//call the methods
huff.treeBuilder(scan1);
String encode = huff.getInput(scan2);
String decoded = huff.treeDecode(encode);
writer.write(decoded);
//close the scanners
scan1.close();
scan2.close();
writer.close();
}
catch (FileNotFoundException e) {
System.err.println("File not found " + e);
}
catch(IOException ioe) {
System.err.println("Error with files" + ioe);
}
catch(ArrayIndexOutOfBoundsException aiobe) {
System.err.println("Not enough args " + aiobe);
}
}
}