m0_65279354 2021-12-20 00:17 采纳率: 50%
浏览 13

用eclipse弄的,想问问怎么解决那个Heap无法解析为类型的错误,代码如下,🙏!

import java.util.Scanner;
import java.ion.*;

public class HuffmanCode {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a text: ");
String text = input.nextLine();

int[] counts = getCharacterFrequency(text); // Count frequency

System.out.printf("%-15s%-15s%-15s%-15s\n",
  "ASCII Code", "Character", "Frequency", "Code");  

Tree tree = getHuffmanTree(counts); // Create a Huffman tree
String[] codes = getCode(tree.root); // Get codes
    
for (int i = 0; i < codes.length; i++)
  if (counts[i] != 0) // (char)i is not in text if counts[i] is 0
    System.out.printf("%-15d%-15s%-15d%-15s\n", 
      i, (char)i + "", counts[i], codes[i]);

}

/** Get Huffman codes for the characters

  • This method is called once after a Huffman tree is built
  • /
    public static String[] getCode(Tree.Node root) {
    if (root == null) return null;
    String[] codes = new String[2 * 128];
    assignCode(root, codes);
    return codes;
    }

/* Recursively get codes to the leaf node */
private static void assignCode(Tree.Node root, String[] codes) {
if (root.left != null) {
root.left.code = root.code + "0";
assignCode(root.left, codes);

  root.right.code = root.code + "1";
  assignCode(root.right, codes);
}
else {
  codes[(int)root.element] = root.code;
}

}

/** Get a Huffman tree from the codes */
public static Tree getHuffmanTree(int[] counts) {
// Create a heap to hold trees
Heap heap = new Heap<>(); // Defined in Listing 24.10
for (int i = 0; i < counts.length; i++) {
if (counts[i] > 0)
heap.add(new Tree(counts[i], (char)i)); // A leaf node tree
}

while (heap.getSize() > 1) { 
  Tree t1 = heap.remove(); // Remove the smallest weight tree
  Tree t2 = heap.remove(); // Remove the next smallest weight 
  heap.add(new Tree(t1, t2)); // Combine two trees
}

return heap.remove(); // The final tree

}

/** Get the frequency of the characters */
public static int[] getCharacterFrequency(String text) {
int[] counts = new int[256]; // 256 ASCII characters

for (int i = 0; i < text.length(); i++)
  counts[(int)text.charAt(i)]++; // Count the character in text

return counts;

}

/** Define a Huffman coding tree */
public static class Tree implements Comparable {
Node root; // The root of the tree

/** Create a tree with two subtrees */
public Tree(Tree t1, Tree t2) {
  root = new Node();
  root.left = t1.root;
  root.right = t2.root;
  root.weight = t1.root.weight + t2.root.weight;
}

/** Create a tree containing a leaf node */
public Tree(int weight, char element) {
  root = new Node(weight, element);
}

@Override /** Compare trees based on their weights */
public int compareTo(Tree t) {
  if (root.weight < t.root.weight) // Purposely reverse the order
    return 1;
  else if (root.weight == t.root.weight)
    return 0;
  else
    return -1;
}

public class Node {
  char element; // Stores the character for a leaf node
  int weight; // weight of the subtree rooted at this node
  Node left; // Reference to the left subtree
  Node right; // Reference to the right subtree
  String code = ""; // The code of this node from the root

  /** Create an empty node */
  public Node() {
  }
  
  /** Create a node with the specified weight and character */
  public Node(int weight, char element) {
    this.weight = weight;
    this.element = element;
  }
}

}
}

img

  • 写回答

1条回答 默认 最新

  • 逗神。 2021-12-20 14:00
    关注

    Heap如果是你自己定义的,那你就要把它定义为一个泛型,可以看下https://www.cnblogs.com/lwbqqyumidi/p/3837629.html
    如果是引入的第三方包,就需要根据第三方包进行使用。

    评论

报告相同问题?

问题事件

  • 创建了问题 12月20日

悬赏问题

  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀
  • ¥50 我在一个购物网站的排队系统排队,这个排队到号后重新定向到目标网站进行购物,但是有技术牛通过技术方法直接跳过排队系统进入目标网址购物,有没有什么软件或者脚本可以用
  • ¥15 ios可以实现ymodem-1k协议 1024字节传输吗?
  • ¥300 寻抓云闪付tn组成网页付款链接
  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥15 服务端控制goose报文控制块的发布问题