Mark As Completed Discussion

Decoding the Bit Sequence

Decode the Huffman-Encoded Bit Sequence Using the Tree

Use the tree to follow the 0s and 1s back to the original characters.

Here's how you can decode the encoded text:

1// JavaScript code for decoding
2function decode(encodedText, root) {
3    let node = root;
4    let decodedText = '';
5    for (let bit of encodedText) {
6        node = (bit === '0') ? node.left : node.right;
7        if (node.char != null) {
8            decodedText += node.char;
9            node = root;
10        }
11    }
12    return decodedText;
13}