查找级别lev(Java、BST)上具有最大值的节点

查找级别lev(Java、BST)上具有最大值的节点,java,binary-search-tree,Java,Binary Search Tree,我需要编写一个方法T bigOnLevel(int-lev)来查找lev级别上具有最大值的节点。由于我不熟悉java泛型,我对T有一些问题。我的想法是将rightChild/leftChild子树的高度与lev进行比较,以便知道子树中该级别上是否存在节点: if(!rightChild.isEmpty() && rightChild.bigOnLevel(lev) >= lev) 我得到的错误是二进制oeprator>=first type T second type i

我需要编写一个方法T bigOnLevel(int-lev)来查找lev级别上具有最大值的节点。由于我不熟悉java泛型,我对T有一些问题。我的想法是将rightChild/leftChild子树的高度与lev进行比较,以便知道子树中该级别上是否存在节点:

if(!rightChild.isEmpty() && rightChild.bigOnLevel(lev) >= lev)
我得到的错误是二进制oeprator>=first type T second type int的错误操作数类型。 但是如果不允许相互比较,我想不出另一种方法来找到具有最大值的节点

整个代码(末尾的方法):

公共类二进制搜索树{
私有内容;
私有二进制搜索树leftChild、righchild;
公共二进制搜索树(){
内容=空;
leftChild=null;
rightChild=null;
}
公共T getContent(){
如果(!isEmpty()){
返回内容;
}否则{
抛出新的RuntimeException();
}
}
公共布尔值为空(){
返回内容==null;
}
公共布尔isLeaf(){
return!isEmpty()&&leftChild.isEmpty()&&righchild.isEmpty();
}
公共无效添加(T){
if(isEmpty()){
内容=t;
leftChild=新的BinarySearchTree();
rightChild=新的BinarySearchTree();
}否则{
如果(内容比较(t)>0){
leftChild.add(t);
}else if(content.compareTo(t)<0){
右子女。添加(t);
}
}
}
公共布尔包含(T){
if(isEmpty()){
返回false;
}否则{
如果(内容比较(t)>0){
返回leftChild.contains(t);
}else if(content.compareTo(t)<0){
返回rightChild.contains(t);
}
返回true;
}
}
公共整数大小(){
if(isEmpty()){
返回0;
}否则{
返回1+leftChild.size()+rightChild.size();
}
}
公开展览({
如果(!isEmpty()){
leftChild.show();
系统输出打印项次(内容);
show();
}
}
//节目:
公共T BIONLEVEL(国际级)
{
int currentlevel=0;
如果(isEmpty()| | lev=lev)
{
返回rightChild.bigOnLevel(lev);
}
其他的
{
如果(!leftChild.isEmpty()&&leftChild.bigOnLevel(lev)>=lev)
{
返回leftChild.bigOnLevel(lev);
}
其他的
{
返回null;
}
}
}
其他的
{
返回内容;
}
}
}
}

public class BinarySearchTree<T extends Comparable<T>> {
private T content;
private BinarySearchTree<T> leftChild, rightChild;

public BinarySearchTree() {
    content = null;
    leftChild = null;
    rightChild = null;
}

public T getContent() {
    if (!isEmpty()) {
        return content;
    } else {
        throw new RuntimeException();
    }
}

public boolean isEmpty() {
    return content == null;
}

public boolean isLeaf() {
    return !isEmpty() && leftChild.isEmpty() && rightChild.isEmpty();
}

public void add(T t) {
    if (isEmpty()) {
        content = t;
        leftChild = new BinarySearchTree<T>();
        rightChild = new BinarySearchTree<T>();
    } else {
        if (content.compareTo(t) > 0) {
            leftChild.add(t);
        } else if (content.compareTo(t) < 0) {
            rightChild.add(t);
        }
    }
}

public boolean contains(T t) {
    if (isEmpty()) {
        return false;
    } else {
        if (content.compareTo(t) > 0) {
            return leftChild.contains(t);
        } else if (content.compareTo(t) < 0) {
            return rightChild.contains(t);
        }
        return true;
    }
}

public int size() {
    if (isEmpty()) {
        return 0;
    } else {
        return 1 + leftChild.size() + rightChild.size();
    }
}

public void show() {
    if (!isEmpty()) {
        leftChild.show();
        System.out.println(content);
        rightChild.show();
    }
}


//Program:

public T bigOnLevel (int lev)
{
    int currentlevel = 0;
    if(isEmpty()  || lev<0 )
    {
        return null;
    }
    else
    {
        if(currentlevel < lev)
        {
            currentlevel++;
            if(!rightChild.isEmpty() && rightChild.bigOnLevel(lev) >= lev)
            {
                return rightChild.bigOnLevel(lev);
            }
            else
            {
                if(!leftChild.isEmpty() && leftChild.bigOnLevel(lev) >= lev)
                {
                    return leftChild.bigOnLevel(lev);
                }
                else
                {
                    return null;
                }
            }
        }
        else
        {
            return content;
        }
    }
}