Java:BST - 删除没有子节点的节点不起作用

时间:2018-05-03 02:08:06

标签: java data-structures binary-tree binary-search-tree nodes

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

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

我尝试删除“cat”节点,但发现remove方法不起作用 - “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));

}

2 个答案:

答案 0 :(得分:1)

race_header方法中,在确定remove(Node root, String s)包含root的值后,您只需将变量s更改为引用null。这不会影响root的左或右子项的父项,因为它从不引用它们。

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

root

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

答案 1 :(得分:1)

问题在于此部分中的删除方法

if (root.left == null && root.right == null) {
    System.out.println("----------------------------------");
    root = null;
    return true;
}

您希望删除root,但不会删除,因为 java is pass by value. 所以当您执行root = null;时,您将复制的变量设置为{{1而不是BST的null

这是您更新的root.方法。我已将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; } 设置为this.root

null
相关问题