Java SE 1.7 中的原始类型二叉搜索树问题



我正在尝试构建一个原始类型BSTComparable<T> .问题是我的声明不知何故做错了,因为我在类中使用Node类型 Comparable<type> 并且在BST类中使用了错误

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);
        }
    }
考虑重构

为以下代码

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

相关内容

  • 没有找到相关文章

最新更新