-
Notifications
You must be signed in to change notification settings - Fork 1
/
huffman.js
161 lines (132 loc) · 4.4 KB
/
huffman.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { BinaryHeap } from './heap.js';
export { HuffmanCoder }
class HuffmanCoder{
stringify(node){
if(typeof(node[1])==="string"){
return '\''+node[1];
}
return '0' + this.stringify(node[1][0]) + '1' + this.stringify(node[1][1]);
}
display(node, modify, index=1){
if(modify){
node = ['',node];
if(node[1].length===1)
node[1] = node[1][0];
}
if(typeof(node[1])==="string"){
return String(index) + " = " + node[1];
}
let left = this.display(node[1][0], modify, index*2);
let right = this.display(node[1][1], modify, index*2+1);
let res = String(index*2)+" <= "+index+" => "+String(index*2+1);
return res + '\n' + left + '\n' + right;
}
destringify(data){
let node = [];
if(data[this.ind]==='\''){
this.ind++;
node.push(data[this.ind]);
this.ind++;
return node;
}
this.ind++;
let left = this.destringify(data);
node.push(left);
this.ind++;
let right = this.destringify(data);
node.push(right);
return node;
}
getMappings(node, path){
if(typeof(node[1])==="string"){
this.mappings[node[1]] = path;
return;
}
this.getMappings(node[1][0], path+"0");
this.getMappings(node[1][1], path+"1");
}
encode(data){
this.heap = new BinaryHeap();
const mp = new Map();
for(let i=0;i<data.length;i++){
if(data[i] in mp){
mp[data[i]] = mp[data[i]] + 1;
} else{
mp[data[i]] = 1;
}
}
for(const key in mp){
this.heap.insert([-mp[key], key]);
}
while(this.heap.size() > 1){
const node1 = this.heap.extractMax();
const node2 = this.heap.extractMax();
const node = [node1[0]+node2[0],[node1,node2]];
this.heap.insert(node);
}
const huffman_encoder = this.heap.extractMax();
this.mappings = {};
this.getMappings(huffman_encoder, "");
let binary_string = "";
for(let i=0;i<data.length;i++) {
binary_string = binary_string + this.mappings[data[i]];
}
let rem = (8 - binary_string.length%8)%8;
let padding = "";
for(let i=0;i<rem;i++)
padding = padding + "0";
binary_string = binary_string + padding;
let result = "";
for(let i=0;i<binary_string.length;i+=8){
let num = 0;
for(let j=0;j<8;j++){
num = num*2 + (binary_string[i+j]-"0");
}
result = result + String.fromCharCode(num);
}
let final_res = this.stringify(huffman_encoder) + '\n' + rem + '\n' + result;
let info = "Compression Ratio : " + data.length/final_res.length;
info = "Compression complete and file sent for download" + '\n' + info;
return [final_res, this.display(huffman_encoder, false), info];
}
decode(data){
data = data.split('\n');
if(data.length===4){
// Handling new line
data[0] = data[0] + '\n' + data[1];
data[1] = data[2];
data[2] = data[3];
data.pop();
}
this.ind = 0;
const huffman_decoder = this.destringify(data[0]);
const text = data[2];
let binary_string = "";
for(let i=0;i<text.length;i++){
let num = text[i].charCodeAt(0);
let bin = "";
for(let j=0;j<8;j++){
bin = num%2 + bin;
num = Math.floor(num/2);
}
binary_string = binary_string + bin;
}
binary_string = binary_string.substring(0,binary_string.length-data[1]);
console.log(binary_string.length);
let res = "";
let node = huffman_decoder;
for(let i=0;i<binary_string.length;i++){
if(binary_string[i]==='0'){
node = node[0];
} else{
node = node[1];
}
if(typeof(node[0])==="string"){
res += node[0];
node = huffman_decoder;
}
}
let info = "Decompression complete and file sent for download";
return [res, this.display(huffman_decoder, true), info];
}
}