Skip to content

Instantly share code, notes, and snippets.

@Recoskie
Last active August 23, 2021 07:07
Show Gist options
  • Save Recoskie/2f3aaadb49535652dc8f4e444431996d to your computer and use it in GitHub Desktop.
Save Recoskie/2f3aaadb49535652dc8f4e444431996d to your computer and use it in GitHub Desktop.
Huffman table expansion
//Huffman table expansion.
var bits = [ 0, 2, 3, 1, 1, 1, 1 ];
var codes = [ 3, 4, 1, 2, 5, 0, 6, 7, 8 ];
//The html table output.
var html = "<table border=\\\"1\\\">";
html += "<tr><td>Bits</td><td>Code</td><td>Length</td></tr>";
//Begin creating the bit expansion for each code.
var bit = 0, pos = 0;
for( var i = 0; i < bits.length; i++ )
{
for( var i2 = 0; i2 < bits[i]; i2++ )
{
html += "<tr><td>" + bit.toString(2).padStart( i + 1, "0" ) + "</td><td>" + codes[pos] + "</td><td>" + ( i + codes[pos] + 1 ) + "</td></tr>";
bit += 1; pos += 1;
}
bit <<= 1;
}
html += "</table>";
document.write( html );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment