java - 从 BST 中删除节点

标签 java binary-search-tree

我认为除了情况 2(删除节点只有一个子树)之外,大多数情况都可以工作。我的测试用例是下面没有 1、2、3 和 4 节点的树:enter image description here

这个程序告诉我 6 已从树中删除,但是当我尝试打印树时,它仍然显示 6 在树中。

public class RandomNode {

static int size;

public static class Node {

    int data;
    Node left;
    Node right;

    public Node(int data) {

        this.data = data;
        left = null;
        right = null;
        size++;
    }

}

// delete node in right subtree
public Node delete(Node root, int data) {
    // base case - if tree is empty
    if (root == null)
        return root;

    // search for deletion node 
    else if (data < root.data)
        root.left = delete(root.left, data);
    else if (data > root.data) {
        root.right = delete(root.right, data);

    } else {

        // case 1: deletion node has no subtrees
        if (root.left == null && root.right == null) {
            root = null;
            size--;
            System.out.println(data + " successfully deleted from tree (case1)");

            // case 2: deletion node has only one subtree
        } else if (root.left != null && root.right == null) {
            root = root.left;
            size--;
            System.out.println(data + " successfully deleted from tree (case2a)");
        } else if (root.left == null && root.right != null) {
            root = root.left;
            size--;
            System.out.println(data + " successfully deleted from tree (case2b)");

            // case 3: deletion node has two subtrees
            // *find min value in right subtree
            // *replace deletion node with min value
            // *remove the min value from right subtree or else there'll be
            // a duplicate
        } else if (root.left != null && root.right != null) {

            Node temp;
            if (root.right.right == null && root.right.left == null)
                temp = findMinNode(root.left);
            else
                temp = findMinNode(root.right);

            System.out.println(root.data + " replaced with " + temp.data);
            root.data = temp.data;

            if (root.right.right == null || root.left.left == null)
                root.left = delete(root.left, root.data);
            else
                root.right = delete(root.right, root.data);

            size--;
            System.out.println(temp.data + " succesfuly deleted from tree (case3)");

        }
    }

    return root;

}

// find min value in tree
public Node findMinNode(Node root) {

    while (root.left != null)
        root = root.left;
    System.out.println("min value: " + root.data);
    return root;

}

public void preOrderTraversal(Node root) {

    if (root == null)
        return;

    preOrderTraversal(root.left);
    System.out.println(root.data);
    preOrderTraversal(root.right);

}

public static void main(String[] args) {

    RandomNode r = new RandomNode();

    Node root = new Node(6);
    //root.left = new Node(2);
    root.right = new Node(9);
    //root.left.left = new Node(1);
    //root.left.right = new Node(5);
    //root.left.right.left = new Node(4);
    //root.left.right.left.left = new Node(3);
    root.right.left = new Node(8);
    root.right.right = new Node(13);
    root.right.left.left = new Node(7);
    root.right.right.left = new Node(11);
    root.right.right.right = new Node(18);

    r.delete(root, 6);
    r.preOrderTraversal(root);

}

}

最佳答案

当你执行 root = root.left; 时和大小 --,还必须使 root.left = null。

在这里,您只是将 root 设置为 root.left,但没有将 root.left 设置为 null。

关于java - 从 BST 中删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38339274/

相关文章:

java - 线程 “main”中的ExException java.lang.IllegalArgumentException:/home不能是目录

c - 尝试使用扫描值在 C 中创建二叉搜索树

c - BST 插入不工作

java - Android:如何设置 MenuButton 的监听器?

java - IntelliJ IDEA 社区版 SDK

java - 如何从 Parcelable 中正确读取可序列化?

java - 如何在jsp中格式化字符串?

python - 有效地找到二叉树中最重的路径 - python

c++ - 在二叉搜索树中搜索一个值

java - 从Java中的展平列表重建二叉搜索树?