所以我有一个红黑树的java代码。
public class RedBlackBST<Key extends Comparable<Key>, Value>
{
private Node root;
private class Node // BST node with color bit (see page 433)
private boolean isRed(Node h); // See page 433.
private Node rotateLeft(Node h); // See page 434.
private Node rotateRight(Node h); // See page 434.
private void flipColors(Node h); // See page 436.
private int size(); // See page 398.
public void put(Key key, Value val){ // Search for key. Update value if found; grow table if new.
root = put(root, key, val);
root.color = BLACK;
}
private Node put(Node h, Key key, Value val) {
//inserts the value into the tree
}
}
这是我的节点类:
private static final boolean RED = true;
private static final boolean BLACK = false;
private class Node {
Key key; // key
Value val; // associated data
Node left, right; // subtrees
int N; // # nodes in this subtree
boolean color; // color of link from
// parent to this node
Node(Key key, Value val, int N, boolean color) {
this.key = key;
this.val = val;
this.N = N;
this.color = color;
}
}
奇怪的是,每次运行它时,我都会收到一个错误,指出 RedBlackBST 代码的最后一行上的 error:reached end of file while parsing
。问题是我检查过并且我的括号已关闭,所以它不应该给我这个错误,但事实就是如此,所以我只是想知道为什么它这么说。
最佳答案
Node
类的语法错误。 private static final boolean
值应该位于类内部,然后必须可以从其他类访问它们:
private class Node {
public static final boolean RED = true;
public static final boolean BLACK = false;
Key key; // key
Value val; // associated data
Node left, right; // subtrees
int N; // # nodes in this subtree
boolean color; // color of link from
// parent to this node
Node(Key key, Value val, int N, boolean color) {
this.key = key;
this.val = val;
this.N = N;
this.color = color;
}
}
关于java - 错误: reached end of file while parsing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116302/