2 years ago

#24717

test-img

Oğuzhan Atalay

Method to encode a given text using an already constructed Huffman coding tree

Write a method that encodes a given text using an already constructed Huffman coding tree. The fields in the Node class are; String value, Float frequency, Node left, Node right.

Huffman Node class:

public class HuffmanNode {
    String value;
    float frequency;
    HuffmanNode left;
    HuffmanNode right;

    public HuffmanNode(String value, float frequency) {
        this.value = value;
        this.frequency = frequency;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public float getFrequency() {
        return frequency;
    }

    public void setFrequency(float frequency) {
        this.frequency = frequency;
    }

    public HuffmanNode getLeft() {
        return left;
    }

    public void setLeft(HuffmanNode left) {
        this.left = left;
    }

    public HuffmanNode getRight() {
        return right;
    }

    public void setRight(HuffmanNode right) {
        this.right = right;
    }

    ...
}

Test with encode and decode functions:

class NodeTest {
    public static String encode(String text, HuffmanNode root) {
        String sb = "";
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            HuffmanNode node = root;
            while (node.left != null && node.right != null) {
                if (node.value.charAt(0) == c) {
                    sb += "0";
                    break;
                } else if (node.left.value.charAt(0) == c) {
                    sb += "0";
                    node = node.left;
                } else {
                    sb += "1";
                    node = node.right;
                }
            }
        }
        return sb;
    }

    public static String decode(String encodedText, HuffmanNode root) {
        String sb = "";
        HuffmanNode node = root;
        for (int i = 0; i < encodedText.length(); i++) {
            char c = encodedText.charAt(i);
            if (c == '0') {
                node = node.left;
            } else {
                node = node.right;
            }
            if (node.left == null && node.right == null) {
                sb += node.value;
                node = root;
            }
        }
        return sb;
    }

    public static void main(String[] args) {
        HuffmanNode root = new HuffmanNode("ABCDE", 1);
        root.left = new HuffmanNode("A", (float) 0.4);
        root.right = new HuffmanNode("BDEC", (float) 0.6);
        root.right.left = new HuffmanNode("BD", (float) 0.25);
        root.right.right = new HuffmanNode("EC", (float) 0.35);
        root.right.left.left = new HuffmanNode("B", (float) 0.1);
        root.right.left.right = new HuffmanNode("D", (float) 0.15);
        root.right.right.left = new HuffmanNode("E", (float) 0.15);
        root.right.right.right = new HuffmanNode("C", (float) 0.2);
        System.out.println(encode("BADEADA", root));
        System.out.println(decode("100010111001010", root));
    }
}

There is no error in decoding, but the expected encode result - specifically D - is different than the calculated. Is there another way to encode the given string?

Output:

10011111001110
BADEADA

Expected:

100010111001010
BADEADA

java

data-structures

encoding

decoding

huffman-code

0 Answers

Your Answer

Accepted video resources