2014-01-03 3 views
-7

Я получаю красный X's в моем коде для закрытия } У меня есть. Они все хорошо смотрят на меня, IDE настаивает на том, что я ошибаюсь. Я не знаю, почему это неверно. Может ли кто-нибудь вести меня так, пожалуйста, спасибо!Ошибка синтаксиса на eclipse

Ошибки происходят в последних 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 
+6

'return (node);' не входит в метод. Вероятно, это должно быть на 2 строки выше ... – assylias

+3

, пожалуйста, прекратите отправку ответов на такие тривиальные проблемы. Я уверен, что ОП мог бы ответить на его вопрос сам с небольшой помощью. – dehlen

+0

и обычно затмение выигрывает .. – cytofu

ответ

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

В затмении: Нажмите Ctrl + A, а затем Ctrl + сдвиг + f в формат кода .. вы узнаете такие ошибки легко ...

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 
--->  } 

Вы написали заявление возврата снаружи. Я переместил его внутрь. Пожалуйста обновите.

0

Ошибка в том, что у вас есть следующее заявление

return (node); 

из вашего метода insert

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

Вы возвращаетесь в конце класса вне любого метода, что является ошибкой.

Смежные вопросы