Java 删除二进制搜索树中的最小值?

Java 删除二进制搜索树中的最小值?,java,sorting,binary-search-tree,Java,Sorting,Binary Search Tree,我尝试使用removeMin()方法按排序顺序打印我的二叉搜索树,但不知何故,输出不正确 这是我的密码: public Node removeMin(Node insertNode){ Node parentNode =root; if (insertNode.left != null){ return removeMin(insertNode.left); } if (insertNode.right ==null){ parentNode.left = nu

我尝试使用
removeMin()
方法按排序顺序打印我的二叉搜索树,但不知何故,输出不正确

这是我的密码:

public Node removeMin(Node insertNode){

  Node parentNode =root;
  if (insertNode.left != null){
    return removeMin(insertNode.left);
  }
  if (insertNode.right ==null){
    parentNode.left = null;
  }else {
    parentNode.left = removeMin(insertNode.right);
  }
  return insertNode;
}
检查节点==NULL

public Node removeMin(Node root){
    if(root == NULL)
        return NULL;
    if (root.left != NULL){
        return removeMin(root.left);
    }
    if (insertNode.right == NULL){
        Node temp = root;
        root = NULL;
        return temp;
    }
    else {
       return removeMin(root.right);
    }

  }
没有检查

试试这个

private Node removeMin(Node node){
    if(node.left.left == null) {
        Node minNode = node.left;
        node.left = node.left.right;
        minNode.right = null;
        return minNode;
    } else {
        return removeMin(node.left);
    }
}

public Node removeMin() {
    if(root == null) {
        return null;
    } else if(root.left != null) {
        return removeMin(root);
    } else {
        Node minNode = root;
        root = root.right;
        minNode.right = null;
        return minNode;
    }
}

输出是什么?预期的输出是什么?首先,如果您没有打印任何内容,我看不出您如何期望输出是正确的可以显示更多的代码吗?这里重要的一点是removeMin无法删除作为参数传递的节点,因为它没有访问其父节点的权限。这就是为什么我编写了这样的代码,即永远不会使用没有剩余子节点的节点调用removeMin。因此,我需要第二个removeMin方法来检查根是否有左子级。我还假设返回节点的所有子节点都应该为null。我不确定这是否是你想要的。