-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanCode.js
113 lines (102 loc) · 2.88 KB
/
HuffmanCode.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
let fs = require ('fs');
let inputStr = fs.readFileSync('input.txt').toString();
let alphabet = new Array();
let huffmanTree = new Array();
function Node(symbol, freq, used, parent, code){
this.parent = parent;
this.letter = symbol;
this.used = used;
this.freq = freq;
this.code = code;
}
for(let i = 0; i < inputStr.length; i++)
{
alphabet [inputStr.charAt(i)] = 0;
}
for(let i = 0; i < inputStr.length; i++)
{
alphabet [inputStr.charAt(i)]++;
}
for(i in alphabet){
let newNode = new Node(i, alphabet[i], false, null, '');
huffmanTree.push(newNode);
}
let treeLength = huffmanTree.length;
for (let i = 0; i < treeLength - 1; i++){
let firstMinInd = -1;
let firstMinFrequency = inputStr.length;
let secondMinInd = -1;
let secondMinFrequency = inputStr.length;
for(let j = 0; j < huffmanTree.length; j++){
if((huffmanTree[j].used == 0) && (huffmanTree[j].freq <= firstMinFrequency))
{
secondMinInd = firstMinInd;
firstMinInd = j;
secondMinFrequency = firstMinFrequency;
firstMinFrequency = huffmanTree[j].freq;
}
else if ((huffmanTree[j].used == 0) && (huffmanTree[j].freq <= secondMinFrequency))
{
secondMinInd = j;
secondMinFrequency = huffmanTree[j].freq;
}
}
huffmanTree.push(new Node(huffmanTree[secondMinInd].letter + huffmanTree[firstMinInd].letter, firstMinFrequency + secondMinFrequency, false, null, ''));
huffmanTree[firstMinInd].used = true;
huffmanTree[secondMinInd].used = true;
huffmanTree[firstMinInd].parent = huffmanTree.length - 1;
huffmanTree[secondMinInd].parent = huffmanTree.length - 1;
huffmanTree[firstMinInd].code = '0';
huffmanTree[secondMinInd].code = '1';
}
if (treeLength > 1)
{
for (let i = 0; i < treeLength; i++)
{
let par = huffmanTree[huffmanTree[i].parent];
while (par.parent != null)
{
huffmanTree[i].code = par.code + huffmanTree[i].code;
par = huffmanTree[par.parent];
}
}
}
else
{
huffmanTree[0].code = '0';
}
for (let i = 0; i < treeLength; i++)
{
console.log(huffmanTree[i].letter + " : " + huffmanTree[i].code);
}
let codeString = "";
for (let i = 0; i < inputStr.length; i++)
{
for (let j = 0; j < treeLength; j++)
{
if (inputStr[i] == huffmanTree[j].letter)
{
codeString += huffmanTree[j].code;
break;
}
}
}
console.log("Encoded string:")
console.log(codeString);
let decodeString = "";
let codes = "";
for (let i = 0; i < codeString.length; i++)
{
codes += codeString[i];
for (let j = 0; j < treeLength; j++)
{
if (codes == huffmanTree[j].code)
{
decodeString += huffmanTree[j].letter;
codes = "";
break;
}
}
}
console.log("Decoded string:")
console.log(decodeString);