繁体   English   中英

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

[英]Raw type Binary Search Tree issue in Java SE 1.7

我正在尝试使用Comparable<T>构建原始类型BST 事情是我的声明以某种方式做错了,因为我在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);
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM