Java 用泛型实现二叉搜索树的困难

Java 用泛型实现二叉搜索树的困难,java,Java,我在实现下面列出的BinarySearchTree时遇到问题。在某些情况下,我正在基于一个需要泛型和可比较键的接口创建一个二元搜索树。我认为代码中有一个逻辑错误难住了我,它在BinarySearchTree中的insert方法中,但我不能100%确定 下面是我的节点的类,它在我的BST中使用 public class MyNodeClass<Key extends Comparable<Key>, Value>{ private Key key; private Valu

我在实现下面列出的BinarySearchTree时遇到问题。在某些情况下,我正在基于一个需要泛型和可比较键的接口创建一个二元搜索树。我认为代码中有一个逻辑错误难住了我,它在BinarySearchTree中的insert方法中,但我不能100%确定

下面是我的节点的类,它在我的BST中使用

public class MyNodeClass<Key extends Comparable<Key>, Value>{
private Key key;
private Value value;
private MyNodeClass<Key,Value> left = null;
private MyNodeClass<Key,Value> right = null;

public MyNodeClass(Key key, Value val)
{
    this.key = key;
    this.value = val;
}

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

public void setValue(Value value){
    this.value = value;
}

public Key getKey(){
    return this.key;
}

public Value getValue(){
    return this.value;
}

public void setLeft(MyNodeClass<Key, Value> l){
    this.left = l;
}

public void setRight(MyNodeClass<Key, Value> r){
    this.right = r;
}

public MyNodeClass<Key,Value> getLeft(){return this.left;}

public MyNodeClass<Key,Value> getRight(){return this.right;}

public int compareTo(Key that){
    return(this.getKey().compareTo(that));
}
公共类MyNodeClass{
私钥;
私人价值;
私有MyNodeClass left=null;
私有MyNodeClass right=null;
公共MyNodeClass(Key,Value val)
{
this.key=key;
this.value=val;
}
公共无效设置密钥(密钥){
this.key=key;
}
公共无效设置值(值){
这个值=值;
}
公钥getKey(){
返回此.key;
}
公共值getValue(){
返回此.value;
}
公共void setLeft(MyNodeClass l){
这个左=l;
}
公共无效设置权限(MyNodeClass r){
这个。右=r;
}
公共MyNodeClass getLeft(){返回this.left;}
公共MyNodeClass getRight(){返回this.right;}
public int compareTo(输入该键){
返回(this.getKey().compareTo(that));
}
}


公共类MyBinarySearchTree实现BinarySearchTree{
紫薇根;
公共MyBinarySearchTree(){
root=null;
}
@凌驾
公共布尔值为空(){
返回root==null;
}
@凌驾
公共值插入(键、值val){
MyNodeClass newNode=新建MyNodeClass(键,val);
newNode.setKey(key);
newNode.setValue(val);
if(root==null){
根=新节点;
return(newNode.getValue());
}
否则{
MyNodeClass当前=新节点;
MyNodeClass亲本;
while(true){
{
父项=当前;
如果(当前比较(键)==1)
{
current=current.getLeft();
如果(当前==null)
{
setLeft(newNode);
返回parent.getLeft().getValue();
}
}
else if(当前比较(键)=-1){
current=current.getRight();
如果(当前==null)
{
parent.setRight(newNode);
返回parent.getRight().getValue();
}
}
否则{
if(当前比较(键)==0){
当前设置键(key);
当前设置值(val);
返回current.getValue();
}
}
}
}
}
}
@凌驾
公共值查找(键){
MyNodeClass电流=根;
while(当前比较(键)!=0)
{
如果(当前比较(键)==1)
{
current=current.getLeft();
}否则{
current=current.getRight();
}
如果(当前==null)
返回null;
}
返回current.getValue();
}
@凌驾
公共值删除(键){
MyNodeClass电流=根;
MyNodeClass父类=根;
布尔值isLeftChild=true;
while(当前比较(键)!=0){
父项=当前;
如果(当前比较(键)==1){
isLeftChild=true;
current=current.getLeft();
}否则{
isLeftChild=false;
current=current.getRight();
}
如果(当前==null)
返回null;
}
if(current.getLeft()==null&¤t.getRight()==null){
如果(当前==根){
root=null;
}else if(isLeftChild){
parent.setLeft(null);
}否则{
parent.setRight(null);
}
返回current.getValue();
}
else if(current.getRight()==null)
{
如果(当前==根){
root=current.getLeft();
}
else if(isLeftChild){
setLeft(current.getLeft());
}
否则{
parent.setRight(current.getLeft());
}
返回current.getValue();
}
else if(current.getLeft()==null)
{
如果(当前==根)
root=current.getRight();
else if(isLeftChild)
setLeft(current.getRight());
其他的
parent.setRight(current.getRight());
返回current.getValue();
}
其他的
{
MyNodeClass Succession=GetSuccession(当前);
如果(当前==根)
根=继承者;
else if(isLeftChild)
父.setLeft(继任者);
其他的
母公司设定权利(继承人);
setLeft(current.getLeft());
返回current.getValue();
}
}
@凌驾
公共字符串stringLevelOrder(){
返回(LevelOrder(root));
}
私有MyNodeClass GetSuccession(MyNodeClass deleteNode)
{
MyNodeClass successorParent=deleteNode;
MyNodeClass后继者=删除节点;
MyNodeClass current=deleteNode.getRight();
while(当前!=null)
{
successorParent=继任者;
后继=当前;
current=current.getLeft();
}
if(继任者!=deleteNode.getRight())
{
successorParent.setLeft(successorParent.getRight());
setRight(deleteNode.getRight());
}
返还继承人;
}
公共静态void main(字符串[]args)
{
MyBinarySearchTree BST=新建MyBinarySearchTree();
MyStudent myStud1=新MyStudent();
MyStudent myStud2=新MyStudent();
MyStudent myStud3=新MyStudent();
MyStudent myStud4=新MyStudent();
MyStudent myStud5=新MyStudent();
myStud1.init(“Clarise”,1.1);
myStud2.init(“克里斯托弗”,1.2);
myStud3.init(“约翰”,1.3);
myStud4.init(“克洛伊”,1.4);
myStud5.init(“Goo”,1.5);
System.out.println(BST.insert(myStud1.getGPA(),myStud1));
系统输出打印LN(BST.i
public class MyBinarySearchTree<Key extends Comparable<Key>, Value> implements BinarySearchTree<Key,Value>  {

private MyNodeClass<Key, Value> root;

public MyBinarySearchTree(){
    root = null;
}

@Override
public boolean isEmpty() {
    return root == null;
}

@Override
public Value insert(Key key, Value val) {
    MyNodeClass<Key,Value> newNode = new MyNodeClass<Key,Value>(key,val);
    newNode.setKey(key);
    newNode.setValue(val);
    if(root==null){
        root = newNode;
        return(newNode.getValue());
    }
    else{
        MyNodeClass<Key,Value> current = newNode;
        MyNodeClass<Key,Value> parent;
        while(true){
            {
                parent = current;
                if(current.compareTo(key) == 1)
                {
                    current = current.getLeft();
                    if(current == null)
                    {
                        parent.setLeft(newNode);
                        return parent.getLeft().getValue();
                    }
                }

                else if(current.compareTo(key) == -1){
                    current = current.getRight();
                    if(current == null)
                    {
                        parent.setRight(newNode);
                        return parent.getRight().getValue();
                    }
                }

               else{
                    if(current.compareTo(key) == 0){
                        current.setKey(key);
                        current.setValue(val);
                        return current.getValue();
                    }
                }
            }
        }
    }
}

@Override
public Value find(Key key) {
    MyNodeClass<Key, Value> current = root;
    while (current.compareTo(key) != 0)
    {
        if (current.compareTo(key) == 1)
        {
            current = current.getLeft();
        } else {
            current = current.getRight();
        }
        if(current == null)
            return null;
    }
    return current.getValue();
}

@Override
public Value delete(Key key) {
    MyNodeClass<Key,Value> current = root;
    MyNodeClass<Key,Value> parent = root;
    boolean isLeftChild = true;

    while(current.compareTo(key) != 0) {
        parent = current;
        if (current.compareTo(key) == 1) {
            isLeftChild = true;
            current = current.getLeft();
        } else {
            isLeftChild = false;
            current = current.getRight();
        }
        if(current == null)
            return null;
    }

    if(current.getLeft() == null && current.getRight() == null) {
        if (current == root) {
            root = null;
        } else if (isLeftChild) {
            parent.setLeft(null);
        } else{
            parent.setRight(null);
        }
        return current.getValue();
    }

    else if(current.getRight() == null)
    {
        if(current == root) {
            root = current.getLeft();
        }
        else if(isLeftChild) {
            parent.setLeft(current.getLeft());
        }
        else{
            parent.setRight(current.getLeft());
        }
        return current.getValue();
    }

    else if(current.getLeft() == null)
    {
        if(current == root)
            root = current.getRight();
        else if(isLeftChild)
            parent.setLeft(current.getRight());
        else
            parent.setRight(current.getRight());
        return current.getValue();
    }

    else
    {
        MyNodeClass<Key,Value> successor = getSuccessor(current);

        if(current == root)
            root = successor;
        else if(isLeftChild)
            parent.setLeft(successor);
        else
            parent.setRight(successor);
        successor.setLeft(current.getLeft());
        return current.getValue();
    }
}


@Override
public String stringLevelOrder() {
    return(LevelOrder(root));
}


private MyNodeClass<Key,Value> getSuccessor(MyNodeClass<Key,Value> deleteNode)
{
    MyNodeClass<Key,Value> successorParent = deleteNode;
    MyNodeClass<Key,Value> successor = deleteNode;
    MyNodeClass<Key,Value> current = deleteNode.getRight();

    while(current != null)
    {
        successorParent = successor;
        successor = current;
        current = current.getLeft();
    }

    if(successor != deleteNode.getRight())
    {
        successorParent.setLeft(successor.getRight());
        successor.setRight(deleteNode.getRight());
    }
    return successor;
}
public static void main(String[] args)
{
    MyBinarySearchTree<Double, MyStudent> BST = new MyBinarySearchTree<Double, MyStudent>();
    MyStudent myStud1 = new MyStudent();
    MyStudent myStud2 = new MyStudent();
    MyStudent myStud3 = new MyStudent();
    MyStudent myStud4 = new MyStudent();
    MyStudent myStud5 = new MyStudent();

    myStud1.init("Clarise", 1.1);
    myStud2.init("Christopher", 1.2);
    myStud3.init("John", 1.3);
    myStud4.init("Chloe", 1.4);
    myStud5.init("Goo", 1.5);

    System.out.println(BST.insert(myStud1.getGPA(), myStud1));
    System.out.println(BST.insert(myStud2.getGPA(), myStud2));
    System.out.println(BST.insert(myStud3.getGPA(), myStud3));
    System.out.println(BST.insert(myStud4.getGPA(), myStud4));
    System.out.println(BST.insert(myStud5.getGPA(), myStud5));

    System.out.println("Delete Key 1.0: " +BST.delete(1.3));
    System.out.println("Delete Key 1.4: " +BST.delete(1.4));
    System.out.println("Is Empty?: " +BST.isEmpty());
    System.out.print("Find 3.9: "+  BST.find(3.9));
}
MyNodeClass<Key,Value> current = newNode;
MyNodeClass<Key,Value> parent;
MyNodeClass<Key,Value> current = root;
MyNodeClass<Key,Value> parent;
MyNodeClass<Key,Value> current = newNode;
MyNodeClass<Key,Value> current = root;