Posts Huffman Decoding-1 (geeksforgeeks - SDE Sheet)
Post
Cancel

Huffman Decoding-1 (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given a string S, implement Huffman Encoding and Decoding.

geeksforgeeks

SOLUTION

  1. Start with an empty string ans to store the decoded characters.
  2. Initialize another node curr, which is set to the root of the Huffman tree.
  3. Loop through each bit in the string s.
    • If the bit is ‘0’, move curr to its left child.
    • If the bit is ‘1’, move curr to its right child.
    • If curr is at a leaf node (it has no children), this means you have reached a decoded character.
      • Add the character to ans.
      • Set curr back to the root of the Huffman tree.
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
class GfG {

    String decode_file(minHeapNode root, String encodedStr){

        int n = encodedStr.length();

        StringBuilder sb = new StringBuilder();

        minHeapNode ptr = root;

        for(int i=0; i<n; i++){

            char ch = encodedStr.charAt(i);

            if(ch == '0'){
                ptr = ptr.left;
            }else
                ptr = ptr.right;

            if(ptr.left == null && ptr.right == null){
                sb.append(ptr.data);
                ptr = root;
            }

        }

        return sb.toString();

    }

}
This post is licensed under CC BY 4.0 by the author.