Tree: Huffman Decoding

Sort by

recency

|

71 Discussions

|

  • + 1 comment

    Notice: In python, The null of node data judgement condition should be "\0" rather than "", " " or None.('\0' is treated simply as a character within a string.)

    def decodeHuff(root, s):
        pointer = root
        decode_s = ""
        for c in s:
            if c=="1":
                pointer=pointer.right
            else:
                pointer = pointer.left
            if pointer.data!='\0':
                decode_s += pointer.data
                pointer = root
        print(decode_s)
    
  • + 0 comments

    simple python solution

    def decodeHuff(root, s):
        node = root
        ans = ""
        for i in s:
            if node is None:
                return
            if i == "1":
                node = node.right
            elif i == "0":
                node = node.left
            if node.left is None and node.right is None:
                ans += node.data
                node = root
        print(ans)
    
  • + 0 comments

    JS version is not working. To get an idea of the structure of the 'input' variable I logged it to the console and passed all tests. The background process mentioned in the explanation that is supposed to encode the string is not present.

  • + 0 comments

    Heya there is a problem with the question when set to JS, simply console.logging the input will work and pass all tests

  • + 0 comments
    void decode(String s, Node root) {
        StringBuilder res = new StringBuilder("");
        Node move = root;
        for(int i = 0; i < s.length(); i++ ) {
            if(Objects.equals(s.charAt(i), '0')){
    
                move = move.left;
    
                if(move != null && move.left == null && move.right == null ) {
                    res.append(move.data);
                    move = root;
                }
    
            } else if(Objects.equals(s.charAt(i), '1')){
    
                move = move.right;
    
                if(move != null && move.left == null && move.right == null ) {
                    res.append(move.data);
                    move = root;
                }
    
            } 
        }
    
       System.out.println(res.toString());
    }