[英]Implementing comparable for Tree class
My assignment deals with Huffman encoding and I am using a priority queue of trees to create it. 我的作业涉及霍夫曼编码,我使用树的优先级队列来创建它。 I am trying to implement comparable for my Tree class and then have a compare To method so that the Trees can be sorted in the priority queue by frequency.
我正在尝试为我的Tree类实现可比对象,然后使用compare To方法,以便可以按频率在优先级队列中对Trees进行排序。 I am getting some error messages when trying to do this and I am not sure why.
尝试执行此操作时收到一些错误消息,但我不确定为什么。
n00832607.java:249: error: Tree is not abstract and does not override abstract method
compareTo(Object) in Comparable
class Tree implements Comparable
^
n00832607.java:423: error: method does not override or implement a method from a supertype
@Override
^
Here is the code that is giving me trouble. 这是给我麻烦的代码。
//Begin tree class
class Tree implements Comparable
{
private Node root; // first node of tree
// -------------------------------------------------------------
public Tree(char data, int frequency) // constructor
{
root = new Node();
root.iData = frequency;
root.dData = data;
}
public Tree(Tree leftChild, Tree rightChild)
{
root = new Node();
root.leftChild = leftChild.root;
root.rightChild = rightChild.root;
root.iData = leftChild.root.iData + rightChild.root.iData;
}
protected Tree(Node root)
{
this.root = root;
}
//end constructors
//Misc tree methods inbetween the constructors and compareTo, I can post them if that would help
@Override
public int compareTo(Tree arg0)
{
Integer freq1 = new Integer(this.root.iData);
Integer freq2 = new Integer(arg0.root.iData);
return freq1.compareTo(freq2);
}
} // end class Tree
////////////////////////////////////////////////////////////////
Also here is my Node class if that is of any help 这也是我的Node类,如果有帮助的话
//Begin node class
////////////////////////////////////////////////////////////////
class Node
{
public int iData; // data item (frequency/key)
public char dData; // data item (character)
public Node leftChild; // this node's left child
public Node rightChild; // this node's right child
public void displayNode() // display ourself
{
System.out.print('{');
System.out.print(iData);
System.out.print(", ");
System.out.print(dData);
System.out.print("} ");
}
} // end class Node
////////////////////////////////////////////////////////////////
You're using the raw Comparable
type, instead of using the generic Comparable<Tree>
type. 您使用的是原始
Comparable
类型,而不是使用通用的Comparable<Tree>
类型。 To compile, as is, your compareTo() method should thus take an Object as argument, and not a Tree. 因此,按原样进行编译,您的compareTo()方法应采用Object作为参数,而不是Tree。 But of course, the correct way to fix it is to make your class implement
Comparable<Tree>
. 但是,当然,解决该问题的正确方法是使您的类实现
Comparable<Tree>
。
Also, note that instead of creating two new Integer instances at each comparison, you could simply use (since Java 7): 另外,请注意,您无需使用每个比较就创建两个新的Integer实例,而只需使用(因为Java 7):
return Integer.compare(this.root.iData, arg0.root.iData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.