我正在尝试在RBTree的底部创建空节点,并希望将EMPTY实例化为空节点。
这行有错误:
final private Node<E> EMPTY = new Empty();
空洞的课程:
private class Empty extends Node<E> {
public Empty() {
red = false;
}
public Node<E> add(E data) {
count++;
return new Node<E>(data);
}
public Node<E> getNode(E data) {
return null;
}
}
我知道它与构造函数有关,但我无法对其进行归零。 我尝试过搜索,但我在这个网站上遇到的大多数内容都与android编程和/或其他一些我不熟悉的语言有关。我尝试过以下方法:
(Node)转换新的Empty();然后意识到这显然不是那样的。
并与班级合作,了解公众是否有效。
除了编程更改之外,我还尝试了此处提供的解决方案:
http://philip.yurchuk.com/software/eclipse-cannot-be-resolved-to-a-type-error/
但没有成功。
对不起,如果这个问题不合适,谢谢你的时间!
完整代码:
package data_structures;
public class RedBlackTree<E extends Comparable<E>> {
private Node<E> root;
private int count = 0;
final private Node<E> EMPTY = new Empty<E>();
public RedBlackTree() {
root = EMPTY;
}
public void add(E data) {
root = root.add(data);
count++;
root.red = false;
}
public boolean find(E data) {
return root.getNode(data) != null;
}
private class Node<E> {
public E data;
public boolean red;
public Node<E> leftChild;
public Node<E> rightChild;
/** Used by Empty */
protected Node() {
assert EMPTY == null;
}
/** Nodes always begin red */
public Node(E k) {
data = k;
red = true;
leftChild = (Node<E>) EMPTY;
rightChild = (Node<E>) EMPTY;
}
private boolean isRed() {
return red;
}
public int height(){
return 0; //returns the counts binary left most bit position to determine the height.
}
public Node<E> add(E newData) {
if(((Comparable<E>) newData).compareTo(data) == -1) {
count++;
leftChild = leftChild.add(newData);
return leftChild;
}
if(((Comparable<E>) newData).compareTo(data) == +1){
count++;
rightChild = rightChild.add(newData);
return rightChild;
}
if(((Comparable<E>) newData).compareTo(data) == 0){
return this;
}
if (leftChild.isRed() && leftChild.leftChild.isRed()) {
return balance(leftChild.leftChild, leftChild, this,
leftChild.leftChild.rightChild, leftChild.rightChild);
} else if (leftChild.isRed() && leftChild.rightChild.isRed()) {
return balance(leftChild, leftChild.rightChild, this,
leftChild.rightChild.leftChild, leftChild.rightChild.rightChild);
} else if (rightChild.isRed() && rightChild.leftChild.isRed()) {
return balance(this, rightChild.leftChild, rightChild,
rightChild.leftChild.leftChild, rightChild.leftChild.rightChild);
} else if (rightChild.isRed() && rightChild.rightChild.isRed()) {
return balance(this, rightChild, rightChild.rightChild,
rightChild.leftChild, rightChild.rightChild.leftChild);
}
return this;
}
/** Returns the node for this key, or null. */
public Node<E> getNode(E newData) {
if(((Comparable<E>) newData).compareTo(data) == -1){
return leftChild.getNode(newData);
}
if(((Comparable<E>) newData).compareTo(data) == +1){
return rightChild.getNode(newData);
}
else{
return this;
}
}
private class Empty<E> extends Node<E> {
public Empty() {
red = false;
}
public Node<E> add(E data) {
count++;
return new Node<E>(data);
}
public Node<E> getNode(E data) {
return null;
}
}
private Node<E> balance(Node<E> a, Node<E> b, Node<E> c, Node<E> d, Node<E> e) {
a.rightChild = d;
b.leftChild = a;
b.rightChild = c;
c.leftChild = e;
a.red = false;
b.red = true;
c.red = false;
return b;
}
}
}
答案 0 :(得分:0)
你不能写
Node<E> EMPTY = new Empty<E>();
因为E是通用类型。您只能使用实际类创建对象,例如:
Node<String> EMPTY = new Empty<String>()
如果你知道自己在做什么,你可以做一个应该使用警告的丑陋技巧
Node<E> EMPTY = new Empty();
另外,将Empty类移到Node之外。