创建叶节点如何?

huangapple go评论58阅读模式
英文:

How do I create a leaf node?

问题

这是一份作业。我已经卡在这里很久了...

我必须创建叶子节点。为此,我有LeafTreeNode类。

LeafTreeNode.java:

import java.util.Map;

public class LeafTreeNode implements TreeNode {

    private char symbol;
    private int symbolCount;

    public LeafTreeNode(char symbol, int symbolCount) {
        this.symbol = symbol;
        this.symbolCount = symbolCount;
    }

    public int symbolCount() {
        return symbolCount;
    }

    public void getSymbolCodes(String prefix, Map<Character, String> symbolCodes) {
        symbolCodes.put(symbol, prefix);
    }

}

现在我必须使用characterHistogram(String s)方法,并为字符串s中出现的每个字符创建一个叶子节点。这个方法位于新类Huffman中。

我必须在huffmanCode(String s)方法中执行这个操作。

Huffman.java:

import java.util.Map;

public class Huffman {

    private LeafTreeNode leafNode;

    public int[] characterHistogram(String s) {
        // 仅支持ASCII字符
        int[] histogram = new int[256];

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c < 256) {
                histogram[c]++;
            }
            System.out.println(s.charAt(i) + " <-> " + histogram[c]);
        }
        return histogram;
    }

    public void printHuffmanCoding(Map<Character, String> map) {
        // for (Character c : map.keySet()) {
        // System.out.println("'" + c + "': " + map.get(c));
        // }
    }

    /**
     * 计算字符串s的Huffman编码映射
     * 
     * @param s
     *          要编码的字符串
     * @return Huffman编码映射
     */
    public Map<Character, String> huffmanCode(String s) {

        int[] histogram = characterHistogram(s);
        System.out.println(1);

        // leafNode.symbolCount();
        // leafNode
        // leafNode.getSymbolCodes(s, null);

        // for( int i = 0; i < histogram.length;i++){
        // histogram[i];
        // }
        // LeafTreeNode.getSymbolCodes(s, null)
        // TODO 3.
        return null;
    }

    /**
     * @param s
     *            要编码的字符串
     * @param map
     *            Huffman编码映射
     * @return s的Huffman编码的长度
     */
    public long lengthHuffmanCoding(String s, Map<Character, String> map) {
        // TODO 4.
        return 42L;
    }

    /**
     * @param s
     *          要编码的字符串
     * @return s的8位编码的长度
     */
    public long length8BitCoding(String s) {
        // TODO 4.
        return 42L;
    }

    /**
     * 计算给定Huffman编码映射与符号的8位编码之间的压缩比
     * 
     * @param s
     *            要编码的字符串
     * @param map
     *            Huffman编码映射
     * @return 压缩比
     */
    public float computeCompressionRatio(String s, Map<Character, String> map) {
        // TODO 4.
        return 42.f;
    }

    public static void main(String[] args) {

        Huffman h = new Huffman();
        String s = "在此插入字符串";
        Map<Character, String> map = h.huffmanCode(s);
        h.printHuffmanCoding(map);
        float cr = h.computeCompressionRatio(s, map);

        System.out.println("压缩比率: " + (cr * 100) + "%");
    }
}

我尝试使用LeafTreeNode的方法,但这并没有起作用。我该如何创建一个叶子节点?

英文:

This is homework. I've been stuck on this a long time...

I have to create leaf nodes. For this I have the class LeafTreeNode.

> LeafTreeNode.java :

import java.util.Map;
public class LeafTreeNode implements TreeNode {
private char symbol;
private int symbolCount;
public LeafTreeNode(char symbol, int symbolCount) {
this.symbol = symbol;
this.symbolCount = symbolCount;
}
public int symbolCount() {
return symbolCount;
}
public void getSymbolCodes(String prefix, Map&lt;Character, String&gt; symbolCodes) {
symbolCodes.put(symbol, prefix);
}
}

Now I have to use the method characterHistogram(String s)
and create a leaf node for each character occurring in s.
This method is in a new class Huffman.

I have to do this in the method huffmanCode(String s).

> Huffman

import java.util.Map;
public class Huffman{
private LeafTreeNode Blattknoten;
public int[] characterHistogram(String s) {
// Only ASCII
int[] histogram = new int[256];
for (int i = 0; i &lt; s.length(); i++) {
char c = s.charAt(i);
if (c &lt; 256) {
histogram[c]++;
}
System.out.println(s.charAt(i) + &quot; &lt;-&gt; &quot; + histogram[c]);
}
return histogram;
}
public void printHuffmanCoding(Map&lt;Character, String&gt; map) {
// for (Character c : map.keySet()) {
// System.out.println(&quot;&#39;&quot; + c + &quot;&#39;: &quot; + map.get(c));
// }
}
/**
* Compute the Huffman coding map for s
* 
* @param s
*          String to encode
* @return Huffman coding map
*/
public Map&lt;Character, String&gt; huffmanCode(String s) {
int[] histogram = characterHistogram(s);
System.out.println(1);
// Blattknoten.symbolCount();
// Blattknoten
// Blattknoten.getSymbolCodes(s, null);
// for( int i = 0; i &lt; histogram.length;i++){
// histogram[i];
// }
// LeafTreeNode.getSymbolCodes(s, null)
// TODO 3.
return null;
}
/**
* @param s
*            String to encode
* @param map
*            Huffman coding map
* @return the length of the Huffman coding of s
*/
public long lengthHuffmanCoding(String s, Map&lt;Character, String&gt; map) {
// TODO 4.
return 42L;
}
/**
* @param s
*          String to encode
* @return the length of the 8-bit coding of s
*/
public long length8BitCoding(String s) {
// TODO 4.
return 42L;
}
/**
* Compute the compression ratio we can achieve for s given the Huffman
* encoding map as compared to 8-bit coding of the symbols.
* 
* @param s
*            String to encode
* @param map
*            Huffman coding map
* @return compression ratio
*/
public float computeCompressionRatio(String s, Map&lt;Character, String&gt; map) {
// TODO 4.
return 42.f;
}
public static void main(String[] args) {
Huffman h = new Huffman();
String s = &quot;Insert String here&quot;;
Map&lt;Character, String&gt; map = h.huffmanCode(s);
h.printHuffmanCoding(map);
float cr = h.computeCompressionRatio(s, map);
System.out.println(&quot;Compression ratio: &quot; + (cr * 100) + &quot;%&quot;);
}
}

I tried to use the methods from LeafTreeNode but this didn't work. How I can create a leaf node?

答案1

得分: 0

遍历所有256个直方图条目,对于每一个计数不为零的条目,使用 new LeafTreeNode(i, histogram[i])

接下来的问题是:一旦每个叶子节点创建好了,你要做什么?

英文:

Go through all 256 histogram entries, and for each one whose count is not zero, you use new LeafTreeNode(i, histogram[i]).

The next question will be: what do you do with each leaf once it's created?

huangapple
  • 本文由 发表于 2023年2月10日 03:50:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定