-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.java
136 lines (117 loc) · 3.77 KB
/
Huffman.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
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
public class Huffman {
static HashMap<String, Integer> frequecyTable = new HashMap<>();
static HashMap<String, String> huffmanCode = new HashMap<>();
static MinHeap minHeap = new MinHeap();
static Node[] nodes;
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(Paths.get("./data06_huffman.txt").toString()));
String[] strings = bufferedReader.readLine().split("");
countCharacters(strings);
makeHuffmanTree();
makeHuffmanCode(nodes[1], "");
printHuffmanCode();
}
private static void printHuffmanCode() {
for(String key : huffmanCode.keySet()){
System.out.println(key+","+ huffmanCode.get(key));
}
}
private static void makeHuffmanCode(Node node, String str){
if(!node.character.equals("")){
huffmanCode.put(node.character, str);
}
else {
makeHuffmanCode(node.leftNode, str+"0");
makeHuffmanCode(node.rightNode, str+"1");
}
}
private static void makeHuffmanTree() {
nodes = new Node[frequecyTable.keySet().size()+1];
for(String key : frequecyTable.keySet()){
minHeap.insert(nodes, new Node(frequecyTable.get(key), key));
}
int n = minHeap.count;
for(int i=0; i<n-1; i++){
Node z = new Node(0,"");
z.leftNode = minHeap.extract_min(nodes);
z.rightNode = minHeap.extract_min(nodes);
z.count = z.leftNode.count + z.rightNode.count;
minHeap.insert(nodes, z);
}
}
private static void countCharacters(String[] strings) {
for(int i=0; i<strings.length; i++){
String temp = strings[i];
if(frequecyTable.containsKey(temp)){
frequecyTable.replace(temp, frequecyTable.get(temp)+1);
}
else{
frequecyTable.put(temp,1);
}
}
}
}
class MinHeap{
static int count = 0;
public static void insert(Node[] nodes, Node x){
nodes[++count] = x;
reverseHeappy(nodes, x, count);
}
private static void reverseHeappy(Node[] nodes, Node x, int index) {
int parent = index/2;
int child = index;
while(parent > 0 && nodes[parent].count > x.count){
nodes[child] = nodes[parent];
child = parent;
parent /=2;
}
nodes[child] = x;
}
public static Node extract_min(Node[] nodes){
Node maxNode = count>=1 ? nodes[1] : null;
if(maxNode != null){
swap(nodes, 1, count);
nodes[count--] = null;
heappfy(nodes, 1);
}
return maxNode;
}
private static void heappfy(Node[] nodes, int index){
int left = 2*index;
int right= 2*index+1;
int largest = index;
if(left <= count && nodes[left].count < nodes[largest].count){
largest = left;
}
else{
largest = index;
}
if(right <= count && nodes[right].count < nodes[largest].count){
largest = right;
}
if(largest != index){
swap(nodes, largest, index);
heappfy(nodes, largest);
}
}
private static void swap(Node[] nodes, int i, int j){
Node temp = nodes[i];
nodes[i] = nodes[j];
nodes[j] = temp;
}
}
class Node{
int count = 0;
String character = "";
Node leftNode = null;
Node rightNode = null;
public Node(int count, String character){
this.count = count;
this.character = character;
}
}