Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:BST-删除没有子节点的节点不会';行不通_Java_Data Structures_Binary Tree_Binary Search Tree_Nodes - Fatal编程技术网

Java:BST-删除没有子节点的节点不会';行不通

Java:BST-删除没有子节点的节点不会';行不通,java,data-structures,binary-tree,binary-search-tree,nodes,Java,Data Structures,Binary Tree,Binary Search Tree,Nodes,我使用链接节点来表示BST。 我可以找到一个没有子节点的节点,但此节点的删除方法不起作用: 在我添加了一个值为“cat”的节点之后,我的BST只有一个没有子节点的节点 我试图删除“cat”节点,但发现删除方法不起作用,“cat”节点仍在BST中 有人知道如何解决这个问题吗?谢谢 public class BinarySearchTree { Node root; public BinarySearchTree() { root = null; } /* * Adds the sp

我使用链接节点来表示BST。 我可以找到一个没有子节点的节点,但此节点的删除方法不起作用:

在我添加了一个值为“cat”的节点之后,我的BST只有一个没有子节点的节点

我试图删除“cat”节点,但发现删除方法不起作用,“cat”节点仍在BST中

有人知道如何解决这个问题吗?谢谢

public class BinarySearchTree {

Node root;

public BinarySearchTree() {
    root = null;
}

/*
 * Adds the specified node to the BST
 */
public String add(String value) {
    if (root == null) {
        root = new Node(value);
        return value;
    }
    return add(root, value);
}

public String add(Node root, String value) {
    int comparision = value.compareTo(root.data);

    if (comparision < 0) {
        if (root.left != null)
            return add(root.left, value);
        root.left = new Node(value);
        return value;
    }

    if (comparision > 0) {
        if (root.right != null)
            return add(root.right, value);
        root.right = new Node(value);
        return value;
    }
    return value;// not allow duplicate
}

/*
 * Returns true if the string is found in the BST
 */
public boolean contains(String value) {
    return contains(root, value);
}

private boolean contains(Node root, String value) {
    if (root == null) {
        return false;
    }

    int comparison = value.compareTo(root.data);
    if (comparison == 0) {
        return true;
    }
    if (comparison < 0) {
        return contains(root.left, value);
    } else {
        return contains(root.right, value);
    }
}

/*
 * Checks whether the tree is empty or not
 */
public boolean isEmpty() {
    return root == null;
}

/*
 * Removes the specified string from the BST
 */
public boolean remove(String s) {
    if (contains(s) == false) {
        return false;
    }
    return remove(root, s);
}

public boolean remove(Node root, String s) {
    if (root == null) {
        return false;
    }

    int comparision = s.compareTo(root.data);

    if (comparision == 0) {
        if (root.left == null && root.right == null) {
            System.out.println("----------------------------------");
            root = null;
            return true;
        } else if (root.left != null && root.right != null) {
            Node temp = root;
            String min = minValue(temp.right).data;
            root.data = min;
            removemin(root.right);
            return true;
        }
    }

    if (comparision < 0) {
        if (root.left.data.equals(s)) {
            if (root.left.left == null || root.left.right == null) {
                root.left = root.left.right;
                return true;
            }
        }
        return remove(root.left, s);
    }

    if (comparision > 0) {
        if (root.right.data.equals(s)) {
            if (root.right.right == null || root.right.left == null) {
                root.right = root.right.left;
                return true;
            }
        }
        return remove(root.right, s);
    }
    return false;
}

public Node minValue(Node root) {
    if (root.left == null) {
        return root;
    } else
        return minValue(root.left);
}

public static void removemin(Node root) {
    if (root.left == null) {
        root = null;
    } else
        removemin(root.left);
}

/**
 * Prints the inorder traversal of this tree
 */
public void inorderTraversal() {
    inorderTraversal(root);
}

private void inorderTraversal(Node root) {
    if (root == null)
        return;
    inorderTraversal(root.left);
    System.out.print(root.data + " ");
    inorderTraversal(root.right);
}

private class Node {
    String data;
    Node left;
    Node right;

    public Node(String data) {
        this.data = data;
    }
}

/*
 * Returns the height of the tree
 */
public int getHeight() {
    return getHeight(root);
}

private int getHeight(Node root) {
    if (root == null)
        return 0;
    return 1 + Math.max(getHeight(root.left), getHeight(root.right));

}
公共类二进制搜索树{
节根;
公共二进制搜索树(){
root=null;
}
/*
*将指定的节点添加到BST
*/
公共字符串添加(字符串值){
if(root==null){
根=新节点(值);
返回值;
}
返回add(根,值);
}
公共字符串添加(节点根,字符串值){
int comparison=value.comparieto(root.data);
如果(比较<0){
if(root.left!=null)
返回add(root.left,value);
root.left=新节点(值);
返回值;
}
如果(比较>0){
if(root.right!=null)
返回add(root.right,value);
root.right=新节点(值);
返回值;
}
返回值;//不允许重复
}
/*
*如果在BST中找到字符串,则返回true
*/
公共布尔包含(字符串值){
返回包含(根、值);
}
私有布尔包含(节点根、字符串值){
if(root==null){
返回false;
}
int comparison=value.comparieto(root.data);
如果(比较==0){
返回true;
}
如果(比较<0){
返回包含(root.left,value);
}否则{
返回包含(root.right,value);
}
}
/*
*检查树是否为空
*/
公共布尔值为空(){
返回root==null;
}
/*
*从BST中删除指定的字符串
*/
公共布尔删除(字符串s){
如果(包含)=false{
返回false;
}
返回移除(根,s);
}
公共布尔删除(节点根,字符串s){
if(root==null){
返回false;
}
int comparison=s.comparieto(root.data);
如果(比较==0){
if(root.left==null&&root.right==null){
System.out.println(“---------------------------------------------”);
root=null;
返回true;
}else if(root.left!=null&&root.right!=null){
节点温度=根;
字符串min=minValue(右临时值).data;
root.data=min;
removemin(根,右);
返回true;
}
}
如果(比较<0){
if(root.left.data.equals){
if(root.left.left==null | | root.left.right==null){
root.left=root.left.right;
返回true;
}
}
返回移除(根、左、s);
}
如果(比较>0){
if(root.right.data.equals){
if(root.right.right==null | | root.right.left==null){
root.right=root.right.left;
返回true;
}
}
返回移除(root.right,s);
}
返回false;
}
公共节点最小值(节点根){
if(root.left==null){
返回根;
}否则
返回最小值(root.left);
}
公共静态void removemin(节点根){
if(root.left==null){
root=null;
}否则
removemin(根,左);
}
/**
*打印此树的顺序遍历
*/
OrderTraversal()中的公共无效{
inorderTraversal(根);
}
OrderTraversal中的私有void(节点根){
if(root==null)
返回;
inorderTraversal(root.left);
System.out.print(root.data+“”);
inorderTraversal(root.right);
}
私有类节点{
字符串数据;
左淋巴结;
节点权;
公共节点(字符串数据){
这个数据=数据;
}
}
/*
*返回树的高度
*/
公共整数getHeight(){
返回getHeight(根);
}
私有int getHeight(节点根){
if(root==null)
返回0;
返回1+Math.max(getHeight(root.left)、getHeight(root.right));
}
remove(Node root,String s)
方法中,在确定
root
包含
s
的值后,只需将变量
root
更改为reference null。这不会影响root的左或右子级的父级,因为它从不引用它们

典型的BST delete方法将返回一个节点,您可以执行以下操作:

//...
if(valueToDelete.compareTo(root.value) == 0){
  if(root.left == null && root.right == null){
    return null;
  }
  // Otherwise some juggling of children into a new shape
  // ... actual code here
  return someNodeThatWasDescendantOfRoot
}else if(valueToDelete.compareTo(root.value) < 0){
  root.left = delete(root.left, valueToDelete)
  return root;
}else{
  root.right = delete(root.right, valueToDelete)
  return root;
}
//...
/。。。
if(valueToDelete.compareTo(root.value)==0){
if(root.left==null&&root.right==null){
返回null;
}
//否则,一些杂耍儿童进入一个新的形状
//…这里是实际代码
返回某个从上到下的节点
}else if(valueToDelete.compareTo(root.value)<0){
root.left=删除(root.left,valueToDelete)
返回根;
}否则{
root.right=delete(root.right,valueToDelete)
返回根;
}
//...

对可能受影响的子节点的分配允许删除的结果在必要时更新其父节点,而无需子节点引用其父节点。

本部分中的删除方法存在问题:

if (root.left == null && root.right == null) {
    System.out.println("----------------------------------");
    root = null;
    return true;
}
您希望删除根,但它不会删除,因为
java是按值传递的。
因此,当您执行
root=null;
时,您将复制的变量设置为
null
,而不是BST的
根。

这是您更新的
remove
方法。为了减少混淆,我将
root
重命名为
node

public boolean remove(Node node, String s) {
    if (node == null) {
        return false;
    }

    int comparision = s.compareTo(node.data);

    if (comparision == 0) {
        if (node.left == null && node.right == null) {
            System.out.println("----------------------------------");
            if (node.equals(root))
                this.root = null;
            node = null;
            return true;
        } else if (node.left != null && node.right != null) {
            Node temp = node;
            String min = minValue(temp.right).data;
            node.data = min;
            removemin(node.right);
            return true;
        }
    }

    if (comparision < 0) {
        if (node.left.data.equals(s)) {
            if (node.left.left == null || node.left.right == null) {
                node.left = node.left.right;
                return true;
            }
        }
        return remove(node.left, s);
    }

    if (comparision > 0) {
        if (node.right.data.equals(s)) {
            if (node.right.right == null || node.right.left == null) {
                node.right = node.right.left;
                return true;
            }
        }
        return remove(node.right, s);
    }
    return false;
}

java有BST类型吗?它适用于字符串吗?(我在谷歌搜索后在BinarySearchTree上找不到oracle文档)在java 8中找不到它。你能在哪里找到BST代码(即导入/自行编写的代码)吗如果你自己工作,发布你的BST类?使用链接节点,父节点可以有左节点和右节点,我自己在你的r中写BST
if (node.equals(root))
    this.root = null;