2013-02-17 8 views
0

자바에서 내 자신의 바이너리 검색 트리를 쓰려고합니다. 나는 모든 메서드를 작성했으며, 이제는 메서드를 테스트 할 프로그램을 작성하려고합니다.바이너리 검색 트리, 삽입 방법이 컴파일되지 않습니다

그러나 "삽입"방법을 구현하려고하면 컴파일되지 않으며 이유가 없습니다. 그것이

public class myTreeNode { 

    public myTreeNode() { 

    } 

    public Comparable data ; 

    public myTreeNode leftchild; 

    public myTreeNode rightchild; 

    public myTreeNode parent; 

    public void insert(Comparable d) { 
     //if less than 
     //does left exist? if it doesnt, make it, give it d 
     //if it exists call insertrecursive on rightchild 
     if(d.compareTo(data) <= 0) { 
      if(leftchild != null) { 
       leftchild.insert(d); 
      } else { 
       leftchild = new myTreeNode(); 
       leftchild.data = d; 
       leftchild.parent = this; 
      } 
     } else { 
      if(rightchild != null) { 
       rightchild.insert(d); 
      } else { 
       rightchild = new myTreeNode(); 
       rightchild.data = d; 
       rightchild.parent = this; 
      } 
     } 
    } 

...more code... 
} 

: 같은

public interface myBST { 
    public void insert(Comparable x); 
    public void delete(Comparable x); 
    public boolean lookup(Comparable x); 
    public void printPreOrder(); 
    public void printInOrder(); 
    public void printPostOrder(); 
} 

마지막으로, myTreeNode 보이는 : myBST의 모습,

public class BST implements myBST { 

    private myTreeNode root; 


    public BST() { 

    } 


    public void insert(Comparable x) { 
     if(root == null) { 
      root = new myTreeNode(); 
      root.data = x; 
     } else if (!lookup(x)) { 
      root.insert(x); 
     } 

    } 


    ...more code... 

} 

과 :

public class lab05driver { 
public static void main(String[] args) { 
    BST q = new BST(); 

    int a = 5; 
    String b = "jed"; 
    double c = 1.8; 
    char d = 'r'; 
    boolean e = false; 
    int f = 35; 
    String g = "yay"; 
    double h = 2.1; 
    char i = 'i'; 
    boolean j = true; 

    Integer k = 5; 
    q.insert(k);  
}} 

내 BST 클래스는 다음과 같습니다 lab05dr의 "q.insert (k)"에 오류가 발생했습니다. 나. 모든 도움/제안 크게 감사하겠습니다 ... ~~~~~ 수정 : 죄송합니다 내가 잘못 복사 ... 주 방법 및 정수 k입니다 정수 ... 오류 메신저 명령 행을 얻는 것은 다음과 같습니다 : 경고 : 원시 타입의 구성원으로서 compareTo (T)의 체크되지 않은 호출이 선택되었습니다. java.lang.Comparable

+0

숙제 확인

을해야 하는가? 컴파일러 출력이란 무엇입니까? – esej

+1

이 줄에서도 오류가 없습니다 -'Integer k = "test"'? – Dukeling

답변

0

q.insert(k);은 선언문입니다. 문은 메소드에 있어야하며 현재 메소드에 없습니다.

public class lab05driver 
{ 
    public static void main(String[] args) 
    { 
    BST q = new BST(); 
    int a = 5; 
    String b = "jed"; 
    double c = 1.8; 
    char d = 'r'; 
    boolean e = false; 
    int f = 35; 
    String g = "yay"; 
    double h = 2.1; 
    char i = 'i'; 
    boolean j = true; 

    Integer k = 1; // changed because "Integer k = "test";" doesn't compile 
    q.insert(k); 
    } 
} 

주 나는 방법에 사용되는 서명 :

그래서 그런 짓을. 이것은 Java가 엔트리 메소드 (프로그램이 시작될 곳)로 간주하는 서명입니다. 내가 볼 수

0

가장 눈에 띄는 문제는 :

Integer k = "test"; 

k는 어떤 종류의 정수 할 필요가 - 당신은 그것을 문자열을 할당했습니다. 이것은 유효한 할당이 아닙니다. 유효한 값은 -1, 0, 1 등 - 모든 정수 값입니다. 당신이 값 값을 할당 (또는 String 클래스로 K 변경)하면 코드는

+0

음 - 다른 사람의 말처럼 메인 엔트리 포인트는 "주"방법이어야합니다. –

관련 문제