【发布时间】:2014-01-20 22:39:20
【问题描述】:
我的代码中的X's 变红了,因为我有关闭}。他们对我来说都很好,IDE坚持认为我错了。我不确定为什么这是不正确的。有人可以指导我吗,谢谢!
错误发生在最后2个关闭}
1 错误:
Syntax error on token "}", { expected after this
token
2 错误:
Syntax error, insert "}" to complete
ClassBody
这是我的代码
public class BinaryTree {
// root node pointer. Will be null for an empty tree
private Node root;
/*
-- Node --
The binary tree is built using this nested node class.
Each node stores on data element, and has left and right
sub-tree pointer which may be null.
The node is a "dumb" nested class -- we just use it for storage;
*/
private static class Node { // Node class
Node left;
Node right;
int data;
Node(int newData) { // create Node
left = null;
right = null;
data = newData;
}
}
/*
Creates an empty binary tree == null root pointer
*/
public void BinaryTree() {
root = null;
}
/*
Returns true if the given target is in the binary tree
*/
public boolean lookup(int data) { // look up a number
return(lookup(root, data)); // use this to parse through a tree to search for element
}
/*
recursive lookup -- given a node, recur
down searching for the given data.
*/
private boolean lookup(Node node, int data) {
if (node == null) {
return(false);
}
if (data == node.data) {
return(true);
}
else if (data < node.data) {
return(lookup(node.left, data));
}
else {
return(lookup(node.right, data));
}
}
public void insert(int data) {
root = insert(root, data);
}
/*
Recursive insert -- given a pointer, recur down
and insert the given data into the tree. Returns the new
node pointer (the standard way to communicate
a changed pointer back to the caller).
*/
private Node insert(Node node, int data) {
if (node == null) {
node = new Node(data);
}
else {
if (data <= node.data) {
node.left = insert(node.left, data);
}
else {
node.right = insert(node.right, data);
}
}
} // I get an error here #1
return (node);
} // I also get an error here #2
【问题讨论】:
-
return (node);不在方法内。它可能应该高 2 行... -
请不要再为这样一个微不足道的问题发布答案。我相信 OP 可以在一点点帮助下自己回答他的问题。
-
通常日食会获胜..