2013-01-01 1 views
0

나는 다양한 트리 구조 (예 : 표준 이진 트리, 빨강 - 검정 트리 또는 B- 트리)를 구현하는 Java 기반 템플릿 기반 클래스를 개발 중입니다. 내 생각은 Java Collections에서 다양한 목록처럼 처리하도록하는 것이다. 이것은 지정된 트리에 의해 확장되는 하나의 인터페이스 클래스입니다. 그 문제를 해결하는 방법 ... 그것은 자바처럼 보이는동등한 일반 확장

BSTree.java:12: error: BSTree is not abstract and does not override abstract method  search(Comparable) in Tree 
public class BSTree<T extends Comparable<T>> extends Tree { 
    ^

BSTree.java:20: error: name clash: add(T#1) in BSTree and add(T#2) in Tree have the same erasure, yet neither overrides the other 
    public void add(T key) throws NullPointerException { 
       ^
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Comparable<T#2> declared in class Tree 

BSTree.java:28: error: method compareTo in interface Comparable<T#2> cannot be applied  to given types; 
       if (key.compareTo(ptr.key) == -1) { 
       ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:42: error: name clash: remove(T#1) in BSTree and remove(T#2) in Tree have the same erasure, yet neither overrides the other 
    public void remove(T key) throws NullPointerException, TreeException { 
       ^
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Comparable<T#2> declared in class Tree 

BSTree.java:49: error: method compareTo in interface Comparable<T#2> cannot be applied  to given types; 
      if (key.compareTo(ptr.key) == 0) { 
       ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:81: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      else if (key.compareTo(ptr.key) < 0) ptr = ptr.left; 
         ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:89: error: name clash: search(T#1) in BSTree and search(T#2) in Tree have  the same erasure, yet neither overrides the other 
    public Node<T> search(T key) throws NullPointerException, KeyNotStoredException { 
       ^
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Comparable<T#2> declared in class Tree 

BSTree.java:94: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      if (key.compareTo(ptr.key) == 0) return ptr; 
       ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:95: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      else if (key.compareTo(ptr.key) < 0) ptr = ptr.left; 
        ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

개체가 다른 유형의 것을 생각한다 : 그러나, 나는 이상한 문제가 벽에 충돌?

여기에 내 코드 조각입니다 :

Tree.java

class Node<T extends Comparable<T>> { 

    protected T key; 
    protected Node parent, left, right; 

    public Node(T key, Node parent) { 
     this.key = key; 
     this.parent = parent; 
     this.left = null; 
     this.right = null; 
    } 

} 

public abstract class Tree<T extends Comparable<T>> { 
    protected Node<T> root; 
protected Integer nodesCount; 

    public abstract void add(T key) throws NullPointerException; 

    public abstract void remove(T key) throws NullPointerException, TreeException; 

    public abstract Node<T> search(T key) throws NullPointerException, KeyNotStoredException; 
} 

BSTree.java는

public class BSTree<T extends Comparable<T>> extends Tree { 

    public BSTree() { 
     root = null; 
     nodesCount = new Integer(0); 
    } 

    @Override 
    public void add(T key) throws NullPointerException { 
     if (root == null) root = new Node<T>(key, null);  
     else {  
      boolean left = false; 
      Node ptr = root, parent = ptr.parent; 
      while (ptr != null) { 
       parent = ptr; 
       left = false; 
       if (key.compareTo(ptr.key) == -1) { 
        ptr = ptr.left; 
        left = true; 
       } else ptr = ptr.right; 
      } 

      if (left) parent.left = new Node<T>(key, parent); 
      else parent.right = new Node<T>(key, parent); 
     } 

     nodesCount++; 
    } 

    @Override 
    public void remove(T key) throws NullPointerException, TreeException { 
     /* implementation */ 
    } 

    @Override 
    public Node<T> search(T key) throws NullPointerException, KeyNotStoredException { 
     /* implementation */ 
    } 

} 

편집 : 조언 당신의 조각에 덕분에 내가 할 수 있었다 오류 수를 5로 줄입니다. 여기에 있습니다 :,210 javac의는 ../bin -d *

BSTree.java:28: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
       if (key.compareTo(ptr.key) == -1) { 
        ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:49: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      if (key.compareTo(ptr.key) == 0) { 
      ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:81: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      else if (key.compareTo(ptr.key) < 0) ptr = ptr.left; 
         ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation  conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:94: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      if (key.compareTo(ptr.key) == 0) return ptr; 
       ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

BSTree.java:95: error: method compareTo in interface Comparable<T#2> cannot be applied to given types; 
      else if (key.compareTo(ptr.key) < 0) ptr = ptr.left; 
         ^
    required: T#1 
    found: Comparable 
    reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion 
    where T#1,T#2 are type-variables: 
    T#1 extends Comparable<T#1> declared in class BSTree 
    T#2 extends Object declared in interface Comparable 

지금, 내 코드 Node<T>하고 그것을 부족 Tree<T>가 된 .java. 그러나 아직도 잘못된 무엇입니까?

+0

아마도 'Tree'가 아닌'Tree '을 확장해야합니다. –

+0

새로운 에러 메시지로 수정 한 코드는 일치하지 않으므로 알기가 어렵지만'ptr'은'Node ' 대신에'Node'로 여전히 선언되어 있습니다. –

답변

7

, 당신은 몇 가지 아이디어를 얻을 수있는 코드를 읽어야합니다.

노드와 트리를 사용하기 위해 코드에서 필요한 것이 하나 있습니다.

public class BSTree<T extends Comparable<T>> extends Tree<T> { 

protected Node<T> parent, left, right; 

BTW : 당신은 원시적를 사용할 수있을 때 당신은 래퍼를 사용하지 말아야합니다.

protected int nodesCount; 
+2

또한'BSTree # add (T)'메소드에서'Node ptr = root, parent = ptr.parent;'를 사용하십시오. –

+0

감사합니다. – Robin92

0

당신은 BSTree 선언에 Tree에 일반 매개 변수를 놓치고있어 : 그것이 그들이 가지고 있지 않기 때문에 BSTreeadd(T) 방법은 Tree에서 하나를 오버라이드 (override)하지 않는 것을 의미

public class BSTree<T ...> extends Tree<T> 

동일한 매개 변수 유형. 클래스로 T때문에

는에서 확인 된 잠재적으로 호환되지 않는 유형 (더불어,이 (우리는 그것이 Comparable인터페이스 구현 알고), 두 방법 모두 add(Object)와 같은 삭제가 Object보다 더 정확하지 않습니다 컴파일러의 오류 출력은 T#1T#2입니다.

0

시도 :

당신은 JDK의 기능을 복제
public class BSTree<T extends Comparable<T>> extends Tree<T> {