2016-12-01 2 views
0

저는 초보 자바 학생이며 재귀를 사용하여 간단한 BST를 작성해야하는 숙제가 있습니다. 간단해야하지만, 그것은 나에게 약간의 문제를주고있다.BST에 삽입, "메소드 삽입 (myclass.TreeNode, int)이 유형 myclass에 대해 정의되지 않았습니다."

public static class TreeNode 
{ 
    TreeNode LeftLink; 
    int  Content; 
    TreeNode RightLink; 

} 
// end class TreeNode 

public static void main(String[] args) throws IOException 
{ 
    TreeNode T = null; 
    int  H, i; 

    T = BuildSearchTree(); 

    H = Height(T); 
    System.out.println("The number of nodes on the tree is " + NodeCount(T)); 
    System.out.println("The height of the tree is " + H); 
    for (i = 0; i <= H; i++) 
     System.out.println("The number of nodes on level " + i + " is " + CountLevel(T,i)); 
    // end for 
} 

내 작업이 이미 나에게 제공 아무것도 수정하지 않고 BinarySearchTree 방법을 만들 채우는 것입니다 다음과 같이

생성자 클래스와 main() 메소드가 나에게 제공됩니다.

BinarySearch Tree 메서드는 정수로 채워진 파일을 사용자에게 요청하고 BST를 재귀 적으로 작성합니다.

내 컴파일러 (이클립스) 내 삽입 방법의 제목으로 나에게 오류를주고있다

내가이 할 수있는 뭔가가 생각 "메소드 삽입 (myclass.TreeNode, INT 것은) 타입 MyClass에 대한 정의되지" 길은 T = BuildSearchTree(); 주요 방법으로 선언하지만, 솔직히, 나는 무슨 일이 일어나는지 전혀 모른다. 내가 가진 무엇

지금까지 삽입에 대한 코드를 시작하기 전에()

public static TreeNode BuildSearchTree() throws IOException { 

    Scanner Keyboard = new Scanner(System.in); 

    System.out.println("enter the name of your word file (please include ''.data'' or ''.txt'' when inputting file name) (number of words in the word file must be less than 30.)"); 

    String datafile = Keyboard.next(); 

    Scanner InputFile = new Scanner(new File(datafile)); 

    int[] data = new int[1000]; 

    while(InputFile.hasNext()){ 
     int i; 
     data[i]=InputFile.nextInt(); 
       i++; 
     } //end while 

    for(int i=0; i<1000; i++){ 
    TreeNode T = insert(T, data);} //end for 




    private static TreeNode insert(TreeNode node, int content) 
    { 


     if (node == null) 
     { 
      node.Content=content; 
     } 

     else if (content <= node.Content) 
      { 
       insert(node.LeftLink, content); 
      } 

     else 
      { 
       insert(node.RightLink, content); 
      }          
     } 
    return node; 
}//end insert method 

return T; 

}//end BuildSearchTree 
+0

왜'이다 : 당신은 무언가 같이해야 1000

귀하의 while 루프의 리터럴 값보다는 삽입 실제로 호출 주변의 루프에서 파일에서 읽은 항목 수를 사용할 필요가 insert'는'BuildSearchTree' 안에 선언 되었습니까? 내 교수님에 따르면 – 4castle

+0

은 BuildSearchTree 내부의 코드 만 편집 할 수 있습니다. 필자가 삽입 메소드를 외부에 배치 할 수 있다면이 모든 것을 스스로 쉽게 실행할 수 있습니다. –

+0

BuildSearchTree 메서드 끝에 중괄호가 누락되었습니다. 자동 들여 쓰기가 엉망이되기 때문에 줄 끝 부분에 중괄호를 넣어 두지 않는 것이 좋습니다. 그 줄을 자동 줄 들여 쓰면 자동으로 들여 쓰기가 쉽습니다. – templatetypedef

답변

0

자바 중첩 된 기능을 허용하지 않으므로 코드 BuildSearchTree을 완료되지 않습니다()이다. 당신은 루프 후 사용하려는 경우 루프 외부 T를 선언 할 필요가 뭔가

T = insert(T, data[i]);처럼

호출

는해야 T = insert(T, data);에 삽입합니다.

데이터 파일의 수가 1000 개 미만이라고 가정 할 수 없으므로 배열보다는 java.util.ArrayList를 사용하려고합니다.

ArrayList<Integer> data = new ArrayList<Integer>; 
    while(InputFile.hasNext()){ 
     data.add(InputFile.nextInt()); 
    } //end while 
    TreeNode T = new TreeNode(); 
    for(i=0; i<data.length(); i++){ 
     T = insert(T, data[i]); 
    } //end for 
    return T;