2012-11-15 111 views
2

我試圖建立一個原始類型BST,與Comparable<T>。問題是不知我的聲明做了錯事,因爲我在Node類類型Comparable<type>BST類是錯誤的使用與在Java SE 1.7原始類型二進制搜索樹問題

The method setParent(Node<Comparable<Comparable<type>>>) in the type 
Node<Comparable<type>> is not applicable for the arguments (Node<Comparable<type>>) 
BinarySearchTree.java /lab2/src line 22 Java Problem 

Node.java

public class Node <type> { 

    private Comparable<type> key; 
    private Node <Comparable<type>> parent; 
    private Node <Comparable<type>> leftChild; 
    private Node <Comparable<type>> rightChild; 

    public Node(Comparable<type> key, Node <Comparable<type>> leftChild, Node <Comparable<type>> rightChild) { 
     this.setKey(key); 
     this.setLeftChild(leftChild); 
     this.setRightChild(rightChild); 
    } 

    public void setKey(Comparable<type> key) { 
     this.key = key; 
    } 

    public Comparable<type> getKey() { 
     return key; 
    } 

    public void setParent(Node<Comparable<type>> y) { 
     this.parent = y; 
    } 

    public Node <Comparable<type>> getParent() { 
     return parent; 
    } 

    public void setLeftChild(Node <Comparable<type>> leftChild) { 
     this.leftChild = leftChild; 
    } 

    public Node <Comparable<type>> getLeftChild() { 
     return leftChild; 
    } 

    public void setRightChild(Node <Comparable<type>> rightChild) { 
     this.rightChild = rightChild; 
    } 

    public Node <Comparable<type>> getRightChild() { 
     return rightChild; 
    } 
} 

BinarySearchTree.java

import java.util.Iterator; 

public class BinarySearchTree<type> implements SortedSet<type> { 

    private Node <Comparable<type>> root; 

    public void insert(Node <Comparable<type>> z) { 

     Node <Comparable<type>> y = null; 
     Node <Comparable<type>> x = root; 

     while (x != null) { 
      y = x; 

      if (z.getKey() < x.getKey()) { // ERROR '<' is undefined for type... 
       x = x.getLeftChild();  // PARAM TYPE ERROR 
      } else { 
       x = x.getRightChild();  // PARAM TYPE ERROR 
      } 
     } 

     z.setParent(y); 

     if (y == null) { 
      root = z; 
     } else if (z.getKey() < y.getKey()) { 
      y.setLeftChild(z); 
     } else { 
      y.setRightChild(z); 
     } 
    } 
+3

你或許應該只需要在一個比較的:'公共類節點<類型擴展可比>'並從代碼中移除的可比其他所有出現次數......這也簡單地使用'T'作爲標準。 – assylias

+0

@assylias:這是一個答案,而不是評論! –

+1

@TomAnderson說實話,我沒有太多時間來闡述評論。隨意給出更詳細的答案! ;-) – assylias

回答

3

考慮到重構下面的代碼

import java.util.SortedSet; 

public abstract class BinarySearchTree<T extends Comparable<T>> implements SortedSet<T> { 
    private Node<T> root; 

    class Node<T extends Comparable<T>> { 

    private T key; 
    private Node<T> parent; 
    private Node<T> leftChild; 
    private Node<T> rightChild; 

    public Node(T key, Node<T> leftChild, Node<T> rightChild) { 
     this.setKey(key); 
     this.setLeftChild(leftChild); 
     this.setRightChild(rightChild); 
    } 

    public void setKey(T key) { 
     this.key = key; 
    } 

    public T getKey() { 
     return key; 
    } 

    public void setParent(Node<T> y) { 
     this.parent = y; 
    } 

    public Node <T> getParent() { 
     return parent; 
    } 

    public void setLeftChild(Node <T> leftChild) { 
     this.leftChild = leftChild; 
    } 

    public Node <T> getLeftChild() { 
     return leftChild; 
    } 

    public void setRightChild(Node <T> rightChild) { 
     this.rightChild = rightChild; 
    } 

    public Node <T> getRightChild() { 
     return rightChild; 
    } 
    } 

    public void insert(Node<T> z) { 

    Node<T> y = null; 
    Node<T> x = root; 

    while (x != null) { 
     y = x; 

     if (z.getKey().compareTo(x.getKey()) < 0) { 
     x = x.getLeftChild(); 
     } else { 
     x = x.getRightChild(); 
     } 
    } 

    z.setParent(y); 

    if (y == null) { 
     root = z; 
    } else if (z.getKey().compareTo((T) y.getKey()) <0) { 
     y.setLeftChild(z); 
    } else { 
     y.setRightChild(z); 
    } 
    } 
} 
+0

爲獲得最佳效果,請使用'>' – newacct

+0

(抱歉,因爲偏離主題)@RomanC能否請您停止對其他問題/答案進行小修改,如將單詞改爲斜體?編輯功能應該被用來**改善**並且不做不必要的改變!斜體風格不會改善任何東西。謝謝 – WarrenFaith