Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
63
Task/Huffman-coding/JavaScript/huffman-coding-1.js
Normal file
63
Task/Huffman-coding/JavaScript/huffman-coding-1.js
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
function HuffmanEncoding(str) {
|
||||
this.str = str;
|
||||
|
||||
var count_chars = {};
|
||||
for (var i = 0; i < str.length; i++)
|
||||
if (str[i] in count_chars)
|
||||
count_chars[str[i]] ++;
|
||||
else
|
||||
count_chars[str[i]] = 1;
|
||||
|
||||
var pq = new BinaryHeap(function(x){return x[0];});
|
||||
for (var ch in count_chars)
|
||||
pq.push([count_chars[ch], ch]);
|
||||
|
||||
while (pq.size() > 1) {
|
||||
var pair1 = pq.pop();
|
||||
var pair2 = pq.pop();
|
||||
pq.push([pair1[0]+pair2[0], [pair1[1], pair2[1]]]);
|
||||
}
|
||||
|
||||
var tree = pq.pop();
|
||||
this.encoding = {};
|
||||
this._generate_encoding(tree[1], "");
|
||||
|
||||
this.encoded_string = ""
|
||||
for (var i = 0; i < this.str.length; i++) {
|
||||
this.encoded_string += this.encoding[str[i]];
|
||||
}
|
||||
}
|
||||
|
||||
HuffmanEncoding.prototype._generate_encoding = function(ary, prefix) {
|
||||
if (ary instanceof Array) {
|
||||
this._generate_encoding(ary[0], prefix + "0");
|
||||
this._generate_encoding(ary[1], prefix + "1");
|
||||
}
|
||||
else {
|
||||
this.encoding[ary] = prefix;
|
||||
}
|
||||
}
|
||||
|
||||
HuffmanEncoding.prototype.inspect_encoding = function() {
|
||||
for (var ch in this.encoding) {
|
||||
print("'" + ch + "': " + this.encoding[ch])
|
||||
}
|
||||
}
|
||||
|
||||
HuffmanEncoding.prototype.decode = function(encoded) {
|
||||
var rev_enc = {};
|
||||
for (var ch in this.encoding)
|
||||
rev_enc[this.encoding[ch]] = ch;
|
||||
|
||||
var decoded = "";
|
||||
var pos = 0;
|
||||
while (pos < encoded.length) {
|
||||
var key = ""
|
||||
while (!(key in rev_enc)) {
|
||||
key += encoded[pos];
|
||||
pos++;
|
||||
}
|
||||
decoded += rev_enc[key];
|
||||
}
|
||||
return decoded;
|
||||
}
|
||||
13
Task/Huffman-coding/JavaScript/huffman-coding-2.js
Normal file
13
Task/Huffman-coding/JavaScript/huffman-coding-2.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
var s = "this is an example for huffman encoding";
|
||||
print(s);
|
||||
|
||||
var huff = new HuffmanEncoding(s);
|
||||
huff.inspect_encoding();
|
||||
|
||||
var e = huff.encoded_string;
|
||||
print(e);
|
||||
|
||||
var t = huff.decode(e);
|
||||
print(t);
|
||||
|
||||
print("is decoded string same as original? " + (s==t));
|
||||
113
Task/Huffman-coding/JavaScript/huffman-coding-3.js
Normal file
113
Task/Huffman-coding/JavaScript/huffman-coding-3.js
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
class node{
|
||||
constructor(freq, char, left, right){
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.freq = freq;
|
||||
this.c = char;
|
||||
}
|
||||
};
|
||||
|
||||
nodes = [];
|
||||
code = {};
|
||||
|
||||
function new_node(left, right){
|
||||
return new node(left.freq + right.freq, -1, left, right);;
|
||||
};
|
||||
|
||||
function qinsert(node){
|
||||
nodes.push(node);
|
||||
nodes.sort(compareFunction);
|
||||
};
|
||||
|
||||
function qremove(){
|
||||
return nodes.pop();
|
||||
};
|
||||
|
||||
function compareFunction(a, b){
|
||||
return b.freq - a.freq;
|
||||
};
|
||||
|
||||
function build_code(node, codeString, length){
|
||||
if (node.c != -1){
|
||||
code[node.c] = codeString;
|
||||
return;
|
||||
};
|
||||
/* Left Branch */
|
||||
leftCodeString = codeString + "0";
|
||||
build_code(node.left, leftCodeString, length + 1);
|
||||
/* Right Branch */
|
||||
rightCodeString = codeString + "1";
|
||||
build_code(node.right, rightCodeString, length + 1);
|
||||
};
|
||||
|
||||
function init(string){
|
||||
var i;
|
||||
var freq = [];
|
||||
var codeString = "";
|
||||
|
||||
for (var i = 0; i < string.length; i++){
|
||||
if (isNaN(freq[string.charCodeAt(i)])){
|
||||
freq[string.charCodeAt(i)] = 1;
|
||||
} else {
|
||||
freq[string.charCodeAt(i)] ++;
|
||||
};
|
||||
};
|
||||
|
||||
for (var i = 0; i < freq.length; i++){
|
||||
if (freq[i] > 0){
|
||||
qinsert(new node(freq[i], i, null, null));
|
||||
};
|
||||
};
|
||||
|
||||
while (nodes.length > 1){
|
||||
qinsert(new_node(qremove(), qremove()));
|
||||
};
|
||||
|
||||
build_code(nodes[0], codeString, 0);
|
||||
};
|
||||
|
||||
function encode(string){
|
||||
output = "";
|
||||
|
||||
for (var i = 0; i < string.length; i ++){
|
||||
output += code[string.charCodeAt(i)];
|
||||
};
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
function decode(input){
|
||||
output = "";
|
||||
node = nodes[0];
|
||||
|
||||
for (var i = 0; i < input.length; i++){
|
||||
if (input[i] == "0"){
|
||||
node = node.left;
|
||||
} else {
|
||||
node = node.right;
|
||||
};
|
||||
|
||||
if (node.c != -1){
|
||||
output += String.fromCharCode(node.c);
|
||||
node = nodes[0];
|
||||
};
|
||||
};
|
||||
|
||||
return output
|
||||
};
|
||||
|
||||
|
||||
string = "this is an example of huffman encoding";
|
||||
console.log("initial string: " + string);
|
||||
init(string);
|
||||
for (var i = 0; i < Object.keys(code).length; i++){
|
||||
if (isNaN(code[Object.keys(code)[i]])){
|
||||
} else {
|
||||
console.log("'" + String.fromCharCode(Object.keys(code)[i]) + "'" + ": " + code[Object.keys(code)[i]]);
|
||||
};
|
||||
};
|
||||
|
||||
huffman = encode(string);
|
||||
console.log("encoded: " + huffman + "\n");
|
||||
output = decode(huffman);
|
||||
console.log("decoded: " + output);
|
||||
Loading…
Add table
Add a link
Reference in a new issue