Algorithm Visualizations
Huffman prefix decoding: Rebuild the deterministic tree from frequencies and walk one edge for each bit, emitting symbols at leaves. Explore a worked example,…
javascript
function algorithm(frequencies, bits, n, output) {
if (n < 0 || n > 64) { return -1; }
let freq = Array(256).fill(0); let observed = Array(256).fill(0); let total = 0; let count = 0; let state = 0;
for (let i = 0; i < 26; i++) { if (frequencies[i] < 0 || frequencies[i] > 16) { return -1; } freq[i] = frequencies[i]; total += freq[i]; }
if (total > 16) { return -1; }
if (total == 0) { if (n == 0) { return 0; } return -1; }
let left = Array(256).fill(0); let right = Array(256).fill(0); let parent = Array(256).fill(0); let smallest = Array(256).fill(0); let nodes = 26; let active = 0; let root = -1; let a = -1; let b = -1;
for (let i = 0; i < 256; i++) { left[i] = -1; right[i] = -1; parent[i] = -1; smallest[i] = i; }
for (let i = 0; i < 26; i++) { if (freq[i] > 0) { active++; root = i; } }
while (active > 1) { a = -1; for (let k = 0; k < nodes; k++) { if (freq[k] > 0 && parent[k] == -1) { if (a == -1 || freq[k] < freq[a] || (freq[k] == freq[a] && smallest[k] < smallest[a])) { a = k; } } } parent[a] = nodes; b = -1; for (let k = 0; k < nodes; k++) { if (freq[k] > 0 && parent[k] == -1) { if (b == -1 || freq[k] < freq[b] || (freq[k] == freq[b] && smallest[k] < smallest[b])) { b = k; } } } parent[b] = nodes; left[nodes] = a; right[nodes] = b; freq[nodes] = freq[a]+freq[b]; smallest[nodes] = smallest[a]; if (smallest[b] < smallest[nodes]) { smallest[nodes] = smallest[b]; } root = nodes; nodes++; active--; }
state = root;
Nov 16, 2026
javascript
Algorithm Visualizations
Huffman prefix encoding: Repeatedly merge the two least frequent trees, then use left/right paths as prefix-free binary codes. Explore a worked example,…
javascript
function algorithm(text, n, output) {
let freq = Array(256).fill(0); let code = Array(256).fill(0); let length = Array(256).fill(0); let node = 0; let count = 0;
for (let i = 0; i < n; i++) { freq[text[i]-97]++; }
for (let i = 0; i < 26; i++) { output[i] = freq[i]; }
let left = Array(256).fill(0); let right = Array(256).fill(0); let parent = Array(256).fill(0); let smallest = Array(256).fill(0); let nodes = 26; let active = 0; let root = -1; let a = -1; let b = -1;
for (let i = 0; i < 256; i++) { left[i] = -1; right[i] = -1; parent[i] = -1; smallest[i] = i; }
for (let i = 0; i < 26; i++) { if (freq[i] > 0) { active++; root = i; } }
while (active > 1) { a = -1; for (let k = 0; k < nodes; k++) { if (freq[k] > 0 && parent[k] == -1) { if (a == -1 || freq[k] < freq[a] || (freq[k] == freq[a] && smallest[k] < smallest[a])) { a = k; } } } parent[a] = nodes; b = -1; for (let k = 0; k < nodes; k++) { if (freq[k] > 0 && parent[k] == -1) { if (b == -1 || freq[k] < freq[b] || (freq[k] == freq[b] && smallest[k] < smallest[b])) { b = k; } } } parent[b] = nodes; left[nodes] = a; right[nodes] = b; freq[nodes] = freq[a]+freq[b]; smallest[nodes] = smallest[a]; if (smallest[b] < smallest[nodes]) { smallest[nodes] = smallest[b]; } root = nodes; nodes++; active--; }
if (root == -1) { output[26] = 0; return 0; }
for (let c = 0; c < 26; c++) { if (freq[c] > 0) { node = c; while (parent[node] != -1) { if (right[parent[node]] == node) { code[c] = code[c] | (1 << length[c]); } length[c]++; node = parent[node]; } if (length[c] == 0) { length[c] = 1; } } }
for (let i = 0; i < n; i++) { for (let k = length[text[i]-97]-1; k >= 0; k--) { output[27+count] = (code[text[i]-97] >> k) & 1; count++; } }
Nov 16, 2026
javascript
Algorithm Visualizations
LZW dictionary decoding: Rebuild the dictionary from previous phrases, including the code-equals-next-entry special case. Explore a worked example, complexity…
javascript
function algorithm(codes, n, output) {
if (n < 0 || n > 24) { return -1; }
if (n == 0) { return 0; }
if (codes[0] < 0 || codes[0] >= 26) { return -1; }
let prefix = Array(256).fill(0); let suffix = Array(256).fill(0); let stack = Array(256).fill(0); let size = 26; let previous = codes[0]; let first = previous+97; let count = 1; let code = 0; let original = 0; let top = 0;
output[0] = first;
for (let i = 1; i < n; i++) { code = codes[i]; original = code; top = 0; if (code < 0 || code > size) { return -1; } if (code == size) { stack[top] = first; top++; code = previous; }
while (code >= 26) { stack[top] = suffix[code]; top++; code = prefix[code]; }
stack[top] = code+97; top++; first = stack[top-1]; if (count+top > 24) { return -1; }
while (top > 0) { top--; output[count] = stack[top]; count++; }
prefix[size] = previous; suffix[size] = first; size++; previous = original;
}
Nov 16, 2026
javascript
Algorithm Visualizations
LZW dictionary encoding: Extend the longest known phrase; emit its code and add the phrase plus the next character to the dictionary. Explore a worked…
javascript
function algorithm(text, n, output) {
if (n == 0) { return 0; }
let prefix = Array(256).fill(0); let suffix = Array(256).fill(0); let size = 26; let phrase = text[0]-97; let found = -1; let count = 0;
for (let i = 1; i < n; i++) { found = -1; for (let code = 26; code < size; code++) { if (prefix[code] == phrase && suffix[code] == text[i]) { found = code; break; } } if (found != -1) { phrase = found; } else { output[count] = phrase; count++; prefix[size] = phrase; suffix[size] = text[i]; size++; phrase = text[i]-97; } }
output[count] = phrase; count++; return count;
}
Nov 16, 2026
javascript
Algorithm Visualizations
Inverse Burrows-Wheeler transform: Match the rank of each final-column character to its occurrence in the sorted first column, then follow LF links backward.…
javascript
function algorithm(last, n, primary, output) {
if (n == 0) { if (primary == 0) { return 0; } return -1; }
if (n < 0 || n > 12 || primary < 0 || primary >= n) { return -1; }
let dollars = 0; let before = 0; let rank = 0; let row = primary; let lf = Array(256).fill(0); let seen = Array(256).fill(0);
for (let i = 0; i < n; i++) { if (last[i] == 36) { dollars++; } else { if (last[i] < 97 || last[i] > 122) { return -1; } } }
if (dollars != 1 || last[primary] != 36) { return -1; }
for (let i = 0; i < n; i++) { before = 0; rank = 0; for (let j = 0; j < n; j++) { if (last[j] < last[i]) { before++; } if (j < i && last[j] == last[i]) { rank++; } } lf[i] = before+rank; }
for (let i = n-1; i >= 0; i--) { if (seen[row] != 0) { return -1; } seen[row] = 1; output[i] = last[row]; row = lf[row]; }
if (row != primary || output[n-1] != 36) { return -1; }
return n;
}
Nov 16, 2026
javascript