eclipse上的语法错误

时间:2014-01-03 09:40:54

标签: java eclipse

我的代码中出现了X's红色}。它们对我来说都很好看,IDE坚持认为我错了。我不确定为什么这是不正确的。有人可以指导我,谢谢你!

错误发生在最后两次关闭}

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

5 个答案:

答案 0 :(得分:0)

    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);
                }
       -->      return (node);// I get an error here    #1
--->       }

你在外面写了回复声明。我把它搬进去了。请更新。

答案 1 :(得分:0)

错误是因为您有以下声明

return (node);

来自insert方法

答案 2 :(得分:0)

将插入更改为:

  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);
                     }
                 }
                 return (node);
        }    

在eclipse中:按Ctrl + a然后按Ctrl + shift + f格式化代码..您将轻松了解此类错误...

答案 3 :(得分:0)

完整的工作代码

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);
}      }  // 

答案 4 :(得分:0)

你在任何方法之外的课程结束时返回,这是一个错误。