-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman-decoding.js
177 lines (138 loc) · 4.46 KB
/
huffman-decoding.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function higherFrequency(nodeA, nodeB) {
return nodeA.data.frequency > nodeB.data.frequency;
}
function internalNode(node) {
return node.data === null;
}
function assigned(node) {
return node !== null;
}
function swap(nodeA, nodeB) {
const tmp = nodeA;
nodeA = nodeB;
nodeB = tmp;
}
class Tree {
constructor() {
this.root = new InternalNode();
}
insert(value) {
let node = new Node(value);
const queue = [this.root];
while(queue.length > 0) {
const prospectiveParent = queue.shift();
if (prospectiveParent[0] === null) {
prospectiveParent[0] = node;
break;
}
if (prospectiveParent[1] === null) {
prospectiveParent[1] = node;
break;
}
if (!internalNode(prospectiveParent[0])) {
if (!!prospectiveParent[0] && higherFrequency(node, prospectiveParent[0])) {
swap(node, prospectiveParent[0]);
}
} else {
queue.push(prospectiveParent[0]);
}
if (!internalNode(prospectiveParent[1])) {
if (!!prospectiveParent[1] && higherFrequency(node, prospectiveParent[1])) {
swap(node, prospectiveParent[1]);
}
} else {
queue.push(prospectiveParent[1]);
}
if (!internalNode(prospectiveParent[0]) && !internalNode(prospectiveParent[1])) {
let intNode = new InternalNode();
const leafNode = prospectiveParent[1];
intNode[1] = leafNode;
leafNode[1] = leafNode[0] = null;
prospectiveParent[1] = intNode;
queue.push(prospectiveParent[1]);
}
}
}
}
class Node {
constructor(data, zero = null, one = null) {
this.data = data;
this[0] = zero;
this[1] = one;
}
}
class InternalNode extends Node {
constructor() {
super(null);
}
}
function analyzeFrequencies(str) {
const frequencies = {};
for (const char of str) {
if (!frequencies[char]) {
frequencies[char] = 0;
}
frequencies[char] += 1;
}
return frequencies;
}
function constructHuffmanTree(frequencies) {
const tree = new Tree();
for (const [char, frequency] of Object.entries(frequencies)) {
tree.insert({char, frequency});
}
return tree;
}
function huffmanTree(str) {
const frequencies = analyzeFrequencies(str);
return constructHuffmanTree(frequencies);
}
function processData(input) {
//Enter your code here
const tree = huffmanTree(input);
const encodedString = huffmanEncode(input, tree);
process.stdout.write(huffmanDecode(encodedString, tree));
}
function findPath(char, node, path) {
if (node === null) {
return null;
} else if (node?.data?.char === char) {
return path;
} else {
const zeroPath = findPath(char, node[0], path.concat(0));
const onePath = findPath(char, node[1], path.concat(1));
return zeroPath ? zeroPath : onePath;
}
}
function encodeCharacter(char, huffmanTree) {
return findPath(char, huffmanTree.root, []);
}
function huffmanEncode(str, huffmanTree) {
let bits = [];
for (const char of str) {
bits = bits.concat(encodeCharacter(char, huffmanTree));
}
return bits;
}
function huffmanDecode(bits, huffmanTree) {
let str = "";
let currentVertex = huffmanTree.root;
while(bits.length > 0) {
const next = bits.shift();
currentVertex = currentVertex[next];
if (currentVertex.data !== null) {
str = str.concat(currentVertex.data.char);
currentVertex = huffmanTree.root;
}
}
return str;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});