Java通用二进制搜索树类型问题

Java通用二进制搜索树类型问题,java,generics,casting,binary-search-tree,Java,Generics,Casting,Binary Search Tree,我正在做作业,这让我有点困惑 我提供了以下BinarySearchTree类 import java.util.NoSuchElementException; /** * * @param <T> The type of data stored in the nodes of the tree, must implement Comparable<T> with the compareTo method. */ public class BinarySearch

我正在做作业,这让我有点困惑

我提供了以下BinarySearchTree类

import java.util.NoSuchElementException;

/**
 *
 * @param <T> The type of data stored in the nodes of the tree, must implement  Comparable<T> with the compareTo method.
 */
public class BinarySearchTree<T extends Comparable<T>> {


    BinaryTree<T> tree;

    int size;
    public BinarySearchTree() {
        tree = new BinaryTree<T>();
        size = 0;
    }

    public boolean isEmpty() {
        return tree.isEmpty();
    }

    protected BinaryTree<T> recursiveSearch(BinaryTree<T> root, T key) {
        if (root == null) {
            return null;
        }
        int c = key.compareTo(root.data);
        if (c == 0) {
            return root;
        }
        if (c < 0) {
            return recursiveSearch(root.left, key);
        } else {
            return recursiveSearch(root.right, key);
        }
    }

    public T search(T key) {
        if (tree.isEmpty()) { 
            return null;
        }
        return recursiveSearch(tree, key).data;
    }

    public void insert(T item) {

        if (tree.isEmpty()) { // insert here
            tree.makeRoot(item);
            size++;
            return;
        }

        // do an iterative descent
        BinaryTree<T> root = tree;
        boolean done=false;
        BinaryTree<T> newNode = null;
        while (!done) {
            int c = item.compareTo(root.data);
            if (c == 0) { // duplicate found, cannot be inserted
                throw new OrderViolationException();
            }
            if (c < 0) { // insert in left subtree
                if (root.left == null) { // insert here as left child
                    newNode = new BinaryTree<T>();
                    root.left = newNode;
                    done=true;
                } else { // go further down left subtree
                    root = root.left;
                }
            } else { // insert in right subtree
                if (root.right == null) { // insert here as right child 
                    newNode = new BinaryTree<T>();
                    root.right = newNode;
                    done=true;
                } else { // go further down right subtree
                    root = root.right;
                }
            }
        }
        // set fields of new node
        newNode.data = item;
        newNode.parent = root;
        size++;
    }

    /**
     * @param deleteNode Node whose parent will receive new node as right or left child,
     *                  depending on whether this node is its parent's right or left child. 
     * @param attach The node to be attached to parent of deleteNode.
     */
    protected void deleteHere(BinaryTree<T> deleteNode, BinaryTree<T> attach) {

        // deleteNode has only one subtree, attach
        BinaryTree<T> parent = deleteNode.parent;
        deleteNode.clear();  // clear the fields
        if (parent == null) {
            return;
        }
        if (deleteNode == parent.left) {
            // left child of parent, attach as left subtree
            parent.detachLeft();
            parent.attachLeft(attach);
            return;
        }
        // attach as right subtree
        parent.detachRight();
        parent.attachRight(attach);
    }


    protected BinaryTree<T> findPredecessor(BinaryTree<T> node) {
        if (node.left == null) {
            return null;
        }
        BinaryTree<T> pred = node.left; // turn left once
        while (pred.right != null) { // keep turning right
            pred = pred.right;
        }
        return pred;
    }


    public T delete(T key) {
        if (tree.isEmpty()) { // can't delete from an empty tree
            throw new NoSuchElementException();
        }

        // find node containing key 
        BinaryTree<T> deleteNode = recursiveSearch(tree, key);
        if (deleteNode == null) { // data not found, can't delete
            throw new NoSuchElementException();
        }

        BinaryTree<T> hold;

        // case c: deleteNode has exactly two subtrees
        if (deleteNode.right != null && deleteNode.left != null) {
            hold = findPredecessor(deleteNode);
            deleteNode.data = hold.data;
            deleteNode = hold; // fall through to case a or b
        }

        // case a: deleteNode is a leaf
        if (deleteNode.left == null && deleteNode.right == null) {
            deleteHere(deleteNode, null);
            size--;
            return deleteNode.data;
        }       

        // case b: deleteNode has exactly one subtree
        if (deleteNode.right != null) {
            hold = deleteNode.right;
            deleteNode.right = null;
        } else {
            hold = deleteNode.left;
            deleteNode.left = null;
        }

        deleteHere(deleteNode,hold);
        if (tree == deleteNode) { // root deleted
            tree = hold;
        }
        size--;
        return deleteNode.data;
    }


    public T minKey() {
        if (tree.data == null) { // tree empty, can't find min value
            throw new NoSuchElementException();
        }

        BinaryTree<T> root = tree;
        T min=root.data;
        root = root.left;  // turn left once
        while (root != null) {  // keep going left to leftmost node
            min = root.data;
            root = root.left;
        }
        return min;
    }


    public T maxKey() {
        if (tree.getData() == null) { // tree empty, can't find max value
            throw new NoSuchElementException();
        }

        BinaryTree<T> root=tree;
        T max=root.data;
        root = root.right;  // turn right once
        while (root != null) { // keep going to rightmost node
            max = root.data;
            root = root.right;
        }
        return max;
    }


    public int size() {
        return size;
    }


    protected void recursivePreOrder(BinaryTree<T> root, Visitor<T> visitor) {
        if (root != null) {
            visitor.visit(root);
            recursivePreOrder(root.left, visitor);
            recursivePreOrder(root.right, visitor);
        }
    }


    public void preOrder(Visitor<T> visitor) {
        if (tree.isEmpty()) {
            return;
        }
        recursivePreOrder(tree, visitor);
    }


    protected void recursiveInOrder(BinaryTree<T> root, Visitor<T> visitor) {
        if (root != null) {
            recursiveInOrder(root.left, visitor);
            visitor.visit(root);
            recursiveInOrder(root.right, visitor);
        }
    }


    public void inOrder(Visitor<T> visitor) {
        if (tree.isEmpty()) {   
            return;
        }
        recursiveInOrder(tree, visitor);
    }


    protected void recursivePostOrder(BinaryTree<T> root, Visitor<T> visitor) {
        if (root != null) {
            recursivePostOrder(root.left, visitor);
            recursivePostOrder(root.right, visitor);
            visitor.visit(root);
        }
    }

    public void postOrder(Visitor<T> visitor) {
        if (tree.isEmpty()) {
            return;
        }
        recursivePostOrder(tree, visitor);
    }
}
import java.util.NoSuchElementException;
/**
*
*@param存储在树节点中的数据类型,必须实现Comparable与compareTo方法。
*/
公共类二进制搜索树{
二叉树;
整数大小;
公共二进制搜索树(){
tree=新的二进制树();
尺寸=0;
}
公共布尔值为空(){
return tree.isEmpty();
}
受保护的二进制树递归搜索(二进制树根,T键){
if(root==null){
返回null;
}
int c=key.compareTo(root.data);
如果(c==0){
返回根;
}
if(c<0){
返回递归搜索(root.left,key);
}否则{
返回递归搜索(root.right,key);
}
}
公共T搜索(T键){
如果(tree.isEmpty()){
返回null;
}
返回递归搜索(树,键)。数据;
}
公共无效插入(T项){
如果(tree.isEmpty()){//插入此处
tree.makeRoot(item);
大小++;
返回;
}
//迭代下降
二叉树根=树;
布尔完成=假;
BinaryTree newNode=null;
而(!完成){
int c=item.compareTo(root.data);
如果(c==0){//找到重复项,则无法插入
抛出新的OrderViolationException();
}
如果(c<0){//插入左子树
如果(root.left==null){//在此处作为左子级插入
newNode=newbinarytree();
root.left=newNode;
完成=正确;
}else{//再往下走左子树
root=root.left;
}
}else{//插入到右子树中
如果(root.right==null){//在此处作为右子级插入
newNode=newbinarytree();
root.right=newNode;
完成=正确;
}else{//再往下走右子树
root=root.right;
}
}
}
//设置新节点的字段
newNode.data=项目;
newNode.parent=root;
大小++;
}
/**
*@param deleteNode其父节点将作为右或左子节点接收新节点,
*取决于此节点是其父节点的右子节点还是左子节点。
*@param附加要附加到deleteNode父节点的节点。
*/
受保护的void deleteHere(BinaryTree deleteNode,BinaryTree attach){
//deleteNode只有一个子树“附加”
BinaryTree parent=deleteNode.parent;
deleteNode.clear();//清除字段
如果(父项==null){
返回;
}
if(deleteNode==parent.left){
//父级的左子级,附加为左子树
parent.detachLeft();
父项。附件左(附件);
返回;
}
//附加为右子树
parent.detachRight();
父项。附件权限(附件);
}
受保护的BinaryTree findPredecessor(BinaryTree节点){
if(node.left==null){
返回null;
}
BinaryTree pred=node.left;//向左拐一次
而(pred.right!=null){//继续右转
pred=pred.right;
}
返回pred;
}
公共T删除(T键){
if(tree.isEmpty()){//无法从空树中删除
抛出新的NoTouchElementException();
}
//查找包含密钥的节点
BinaryTree deleteNode=递归搜索(树,键);
如果(deleteNode==null){//未找到数据,则无法删除
抛出新的NoTouchElementException();
}
二叉树控股;
//案例c:deleteNode正好有两个子树
if(deleteNode.right!=null&&deleteNode.left!=null){
hold=findPredecessor(删除节点);
deleteNode.data=hold.data;
deleteNode=hold;//适用于案例a或案例b
}
//案例a:deleteNode是一个叶子
if(deleteNode.left==null&&deleteNode.right==null){
deleteHere(deleteNode,null);
大小--;
返回deleteNode.data;
}       
//案例b:deleteNode正好有一个子树
if(deleteNode.right!=null){
hold=deleteNode.right;
deleteNode.right=null;
}否则{
hold=deleteNode.left;
deleteNode.left=null;
}
deleteHere(deleteNode,保持);
如果(tree==deleteNode){//root已删除
树=保持;
}
大小--;
返回deleteNode.data;
}
公共T minKey(){
如果(tree.data==null){//tree为空,则找不到最小值
抛出新的NoTouchElementException();
}
二叉树根=树;
T min=根数据;
root=root.left;//左转一次
而(root!=null){//继续向左移动到最左边的节点
min=根数据;
root=root.left;
}
返回最小值;
}
公共T maxKey(){
如果(tree.getData()==null){//tree为空,则找不到最大值
抛出新的NoTouchElementException();
}
二叉树根=树;
T max=根数据;
root=root.right;//向右转一次
而(root!=null){//继续转到最右边的节点
max=root.data;
root=root.right;
}
返回最大值;
}
公共整数大小(){
返回大小;
}
受保护的void recursivePreOrder(二进制树根,访问者){
if(root!=null){
visitor.visit(root);
记录
BinarySearchTree<Student> tree = new BinarySearchTree<Student>();
class Student implements Comparable<Student> {

    //...

    int compareTo(Student other) {
        // return some negative number if this object is less than other
        // return 0 if this object is equal to other
        // return some positive number if this object is greater than other
    }
}
 public class BinarySearchTree<T extends Comparable<T>> 
public class BinarySearchTree<Student extends Comparable<Student>>
   public class BinarySearchTree<T extends Comparable<? super T > >