Skip to content

Commit 2caaaf5

Browse files
authored
Merge pull request #1 from Vadym-Mitin/AnotherBranch
Another branch
2 parents fdf5efe + e2e50e4 commit 2caaaf5

File tree

9 files changed

+294
-238
lines changed

9 files changed

+294
-238
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,46 @@
11
package HuffmanCompression;
22

3+
import java.util.Map;
4+
5+
import static HuffmanCompression.Compressor.encode;
6+
import static HuffmanCompression.HuffmanTree.*;
7+
import static HuffmanCompression.SerializeHuffmanTable.*;
8+
9+
310
public class Application {
11+
12+
public static void main(String[] args) {
13+
14+
String s = "abracadabra";
15+
System.out.println("String :");
16+
System.out.println(s);
17+
Node tree = growTree(s);
18+
19+
Map<Character, String> tableOfCodes = HuffmanTree.createTable(tree);
20+
21+
System.out.println("create table from string");
22+
for (Map.Entry entry : tableOfCodes.entrySet()) {
23+
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
24+
}
25+
26+
System.out.println("Encoded string");
27+
String encoded = encode(s,tableOfCodes);
28+
System.out.println(encoded);
29+
30+
System.out.println("Serialize table ");
31+
String y = serialize(tableOfCodes);
32+
System.out.println(y);
33+
34+
System.out.println("deserealize table");
35+
Map<Character,String> deserialize = deserialize(y);
36+
37+
for (Map.Entry entry : deserialize.entrySet()) {
38+
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
39+
}
40+
41+
System.out.println("decoded string");
42+
String decode = Decompressor.decode(encoded,deserialize);
43+
System.out.println(decode);
44+
45+
}
446
}

src/HuffmanCompression/Compressor.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
11
package HuffmanCompression;
22

3-
import java.sql.SQLOutput;
43
import java.util.*;
54

65
public class Compressor {
76

8-
public static void main(String[] args) {
7+
public static String encode(String encodedString, Map<Character, String> table) {
8+
StringBuilder stringBuilder = new StringBuilder();
99

10-
String s = "abracadabra";
11-
12-
HuffmanTree tree = HuffmanTree.growTree(s);
13-
14-
Map<Character, String> tableOfCodes = tree.createTable(tree.getParentNode());
15-
16-
17-
for (Map.Entry entry : tableOfCodes.entrySet()) {
18-
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
10+
for (int i = 0; i <encodedString.length(); i++) {
11+
String code = table.get(encodedString.charAt(i));
12+
stringBuilder.append(code);
1913
}
2014

21-
// Byte[] bytes = new Byte[s.length()];
22-
StringBuffer sb = new StringBuffer();
23-
24-
for (int i = 0; i <s.length() ; i++) {
25-
sb.append(tableOfCodes.get(s.charAt(i)));
26-
}
27-
System.out.println(sb.toString());
28-
29-
String x = HuffmanTree.nodeSerialize(tree.getParentNode());
30-
System.out.println(x);
31-
32-
HuffmanTree.Node newParent = HuffmanTree.nodeDeserialize(x);
33-
15+
return stringBuilder.toString();
3416
}
3517

3618

src/HuffmanCompression/Decompressor.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
package HuffmanCompression;
22

3-
import java.util.Iterator;
43
import java.util.Map;
4+
import java.util.PriorityQueue;
5+
import java.util.Queue;
56

67
public class Decompressor {
78

89
public static void main(String[] args) {
910
String s = "abracadabra";
1011

11-
HuffmanTree tree = HuffmanTree.growTree(s);
12-
// System.out.println(HuffmanTree.createTable(tree.getParentNode()).toString());
12+
Node tree = HuffmanTree.growTree(s);
13+
14+
Map<Character, String> table = HuffmanTree.createTable(tree);
1315
String compressed = "01011001110011110101100";
1416

15-
// System.out.println(tree.getParentNode().getCode());
17+
for (Map.Entry entry : table.entrySet()) {
18+
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
19+
}
1620

17-
String swe = decode(compressed, tree);
21+
String swe = decode(compressed, table);
1822

1923
System.out.println(swe);
20-
// for (int j = maxCode.length()-1; j >=0 ; j--) {
21-
// System.out.println(j);
22-
// }
2324

2425
}
2526

26-
public static String decode(String s, HuffmanTree tree) {
27-
HuffmanTree.Node parentNode = tree.getParentNode();
28-
Map<Character, String> table = HuffmanTree.createTable(parentNode);
29-
// System.out.println(table.toString());
30-
Iterator<Map.Entry<Character, String>> iterator = table.entrySet().iterator();
31-
int maxCodeLength = iterator.next().getValue().length();
27+
public static String decode(String s, Map<Character, String> table) {
28+
29+
Queue<Map.Entry<Character, String>> queue = new PriorityQueue<>((Map.Entry<Character, String> o2,
30+
Map.Entry<Character, String> o1) ->
31+
o1.getValue().compareToIgnoreCase(o2.getValue()));
32+
33+
queue.addAll(table.entrySet());
34+
35+
String maxValue = queue.poll().getValue();
36+
// System.out.println(maxValue);
37+
int maxCodeLength = maxValue.length();
3238

3339
StringBuilder sb = new StringBuilder();
3440

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package HuffmanCompression;
22

3-
public class FileReader {
3+
public class FileWork {
44
}

src/HuffmanCompression/HuffmanTree.java

Lines changed: 20 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class HuffmanTree {
88

99
public static void main(String[] args) {
1010
String s = "abracadabra";
11+
System.out.println(s);
12+
1113
Map<Character, Integer> map = ListOfFrequencies(s);
1214

1315
for (Map.Entry entry : map.entrySet()) {
@@ -17,10 +19,8 @@ public static void main(String[] args) {
1719
System.out.println();
1820
System.out.println();
1921

20-
21-
HuffmanTree tree = HuffmanTree.growTree(s);
22-
Map<Character, String> table = tree.createTable(tree.parent);
23-
22+
Node tree = HuffmanTree.growTree(s);
23+
Map<Character, String> table = createTable(tree);
2424

2525
for (Map.Entry entry : table.entrySet()) {
2626
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
@@ -36,34 +36,28 @@ public Node getParentNode() {
3636
}
3737

3838
private static Map<Character, Integer> ListOfFrequencies(String s) {
39-
Map<Character, Integer> map = new HashMap<>();
39+
Map<Character, Integer> listOfRepetition = new HashMap<>();
4040
char[] characters = s.toCharArray();
41-
for (int i = 0; i < characters.length; i++) {
42-
char aChar = characters[i];
43-
if (map.containsKey(aChar)) {
44-
Integer count = map.get(aChar) + 1;
45-
map.put(aChar, count);
41+
for (char aChar : characters) {
42+
if (listOfRepetition.containsKey(aChar)) {
43+
Integer count = listOfRepetition.get(aChar) + 1;
44+
listOfRepetition.put(aChar, count);
4645
} else {
47-
map.put(aChar, 1);
46+
listOfRepetition.put(aChar, 1);
4847
}
4948
}
5049

51-
List<Map.Entry<Character, Integer>> list = new LinkedList<>(map.entrySet());
52-
53-
//Sorting witch Comparator
54-
//LamBadAss
50+
List<Map.Entry<Character, Integer>> list = new LinkedList<>(listOfRepetition.entrySet());
5551
Collections.sort(list, (Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) ->
5652
o1.getValue().compareTo(o2.getValue()));
57-
58-
map = new LinkedHashMap<>();
53+
listOfRepetition = new LinkedHashMap<>();
5954
for (Map.Entry<Character, Integer> entry : list) {
60-
map.put(entry.getKey(), entry.getValue());
55+
listOfRepetition.put(entry.getKey(), entry.getValue());
6156
}
62-
63-
return map;
57+
return listOfRepetition;
6458
}
6559

66-
public static HuffmanTree growTree(String s) {
60+
public static Node growTree(String s) {
6761

6862
Map<Character, Integer> nodeSet = HuffmanTree.ListOfFrequencies(s);
6963

@@ -92,116 +86,26 @@ public static HuffmanTree growTree(String s) {
9286
}
9387

9488
Node root = queue.poll();
95-
return new HuffmanTree(root);
89+
return root;
9690
}
9791

9892
public static Map<Character, String> createTable(Node node) {
9993
Map<Character, String> table = new LinkedHashMap<>();
100-
treeTraversalforCreateTable(table, node, "");
94+
treeTraversalForCreateTable(table, node, "");
10195
return table;
10296
}
10397

104-
private static void treeTraversalforCreateTable(Map<Character, String> table, Node node, String s) {
98+
private static void treeTraversalForCreateTable(Map<Character, String> table, Node node, String s) {
10599

106100
if (!node.isLeaf()) {
107101
// System.out.println(node.getChars());
108-
treeTraversalforCreateTable(table, node.getLeftNode(), s+ node.getLeftNode().getCode());
109-
treeTraversalforCreateTable(table, node.getRightNode(), s+ node.getRightNode().getCode());
102+
treeTraversalForCreateTable(table, node.getLeftNode(), s+ node.getLeftNode().getCode());
103+
treeTraversalForCreateTable(table, node.getRightNode(), s+ node.getRightNode().getCode());
110104
} else {
111105
node.setLeafCode(s);
112106
table.put(node.getSymbol(), node.getCode());
113107
}
114108
}
115109

116110

117-
public static class Node {
118-
119-
private Node leftNode;
120-
private Node rightNode;
121-
private boolean isRoot;
122-
private boolean isFirst;
123-
private int freq;
124-
private String code;
125-
private String chars;
126-
private Character symbol;
127-
128-
private Node(Node left, int freq, Node right) {
129-
this.leftNode = left;
130-
this.rightNode = right;
131-
this.freq = freq;
132-
}
133-
134-
private Node(Node left, int freq, Node right, Character symbol) {
135-
this.leftNode = left;
136-
this.rightNode = right;
137-
this.freq = freq;
138-
this.chars = symbol.toString();
139-
this.symbol = symbol;
140-
141-
}
142-
143-
public Character getSymbol() {
144-
return symbol;
145-
}
146-
147-
public void setChars(String s) {
148-
this.chars = s;
149-
}
150-
151-
public String getChars() {
152-
return chars;
153-
}
154-
155-
public static Node createLeaf(Map.Entry<Character, Integer> symbol) {
156-
return new Node(null, symbol.getValue(), null, symbol.getKey());
157-
}
158-
159-
public static Node createNode(Node left, int freq, Node right) {
160-
return new Node(left, freq, right);
161-
}
162-
163-
public String getCode() {
164-
return code;
165-
}
166-
167-
public void setCode(String code) {
168-
if (code.equals("1") || code.equals("0")) {
169-
this.code = code;
170-
// return this;
171-
} else throw new IllegalStateException("this wrong code type 0 or 1");
172-
}
173-
174-
public void setLeafCode(String code) {
175-
if (isLeaf()) {
176-
this.code = code;
177-
}
178-
}
179-
180-
public boolean isLeaf() {
181-
return leftNode == null && rightNode == null && !(chars.isEmpty());
182-
}
183-
184-
public Node setLeftNode(Node leftNode) {
185-
this.leftNode = leftNode;
186-
return this;
187-
}
188-
189-
public Node setRightNode(Node rightNode) {
190-
this.rightNode = rightNode;
191-
return this;
192-
}
193-
194-
public Node getLeftNode() {
195-
return leftNode;
196-
}
197-
198-
public Node getRightNode() {
199-
return rightNode;
200-
}
201-
202-
public Integer getFreq() {
203-
return freq;
204-
}
205-
206-
}
207111
}

0 commit comments

Comments
 (0)