How to confuse a Huffman tree with a given frequency and how to start? Java

I want to know how to deal with my homework I'm trying to create a Huffman tree that will encode and decode messages in Java I have strings and frequencies

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

I know that for a Hoffman tree, you can choose the two lowest frequencies and turn them into a tree whose frequencies are the sum I know that I can insert all frequencies into the priority queue and use the remove () method to get the two lowest frequencies They are then added together to obtain their weight, which is then inserted into the queue and repeated

The final tree should maintain its weight

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

I'm not sure how to start implementing a Huffman tree code that can create and display trees with frequency Let me look at other codes. It seems that they are all created from streaming input code

Any help to let me go would be great Thank you in advance!

I want to print it out in the following format: (reservation traversal)

58  
- 33  
- - 18  
- - - 8  
- - - - 4  
- - - - - 1:t  
- - - - - 3:e  
- - - -  4:nl  
- - - 10:a  
- - 15:b  
- 25  
- - 12:c  
- - 13:sp

Solution

import java.util.PriorityQueue;
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 help you I've included your example, but it should apply to any number of frequencies and text Just make sure the frequency and text size are the same because I didn't check for errors

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>