Huffman tree with a given frequency Vaguely how to start? Java

I am trying to figure out what to do with my home problem. I am trying to create a Huffman tree that will encode and decode messages in Java. I have been given strings and frequency.

[a=10, b=15, c=12, e=3, nl=4, sp=13, t=1]. 

I know that with the Huffman Tree you take the two lowest frequencies and turn them into a tree with the sum of their frequencies as a parent. I understand that using the priority queue I can insert the entire frequency into it and use the method remove()to take out the 2 lower frequency. Then add them together to get the weight of both of them, then put that Weight back into the queue and repeat.

The final tree must contain weight

[58=root, root.left = 33, root.right = 25]   
[33.left = 18,  18.left = 8,  8.left = 4] 

I don’t know exactly how to even begin to implement the Huffman tree code, which can create a tree with a frequency and display the Tree. I look at other codes, and it seems that they all create from a Streaming Input Code or so.

Any help would be good at getting me going. Thanks in advance!

Suppose you can print it in a format such as: (preliminary traversal)

58  
- 33  
- - 18  
- - - 8  
- - - - 4  
- - - - - 1:t  
- - - - - 3:e  
- - - -  4:nl  
- - - 10:a  
- - 15:b  
- 25  
- - 12:c  
- - 13:sp  
+5
source share
1 answer
import java.util.PriorityQueue;

public class Node implements Comparable<Node> {
    Node left;
    Node right;
    Node parent;
    String text;
    int frequency;

    public Node(String textIn, int frequencyIn) {
        text = textIn;
        frequency = frequencyIn;
    }

    public Node(int frequencyIn) {
        text = "";
        frequency = frequencyIn;
    }

    public int compareTo(Node n) {
        if (frequency < n.frequency) {
            return -1;
        }
        else if(frequency > n.frequency) {
            return 1;
        }
        return 0;
    }

    public static void printFromPreOrder(Node n, String dashes) {
        // print with colon if leaf node
        if (n.text != "") {
            System.out.println(dashes + n.frequency + ":" + n.text);
        }
        else {
            System.out.println(dashes + n.frequency);
        }

        // Start recursive on left child then right
        if (n.left != null) {
            printFromPreOrder(n.left, dashes + "-");
        }
        if (n.right != null) {
            printFromPreOrder(n.right, dashes + "-");
        }
    }

    // Returns root node to pass to printFromPreOrder
    public static Node makeHuffmanTree(int frequencies[], String text[]) {
        PriorityQueue<Node> queue = new PriorityQueue<Node>();
        for (int i = 0; i < text.length; i++) {
            Node n = new Node(text[i], frequencies[i]);
            queue.add(n);
        }
        Node root = null;
        while (queue.size() > 1)  {
            Node least1 = queue.poll();
            Node least2 = queue.poll();
            Node combined = new Node(least1.frequency + least2.frequency);
            combined.right = least1;
            combined.left = least2;
            least1.parent = combined;
            least2.parent = combined;
            queue.add(combined);
            // Keep track until we actually find the root
            root = combined;
        }
        return root;
    }

    public static void main(String[] args) {
        int frequencies[] = {10, 15, 12, 3, 4, 13, 1};
        String text[] = {"a", "b", "c", "e", "nl", "sp", "t"};
        Node root = Node.makeHuffmanTree(frequencies, text);
        Node.printFromPreOrder(root, "");
    }
}

This will work for you. I included your example, but it should work on any number of frequencies and text. Just make sure the frequencies and text are the same size, because I did not check for errors.

+3
source

All Articles