简体   繁体   中英

BST Delete Function does not Delete Intended Node (Java)

I am trying to implement a delete function for my BST class. I am trying to follow a few standard tutorials, and am good with everything besides my delete function. I don't get any errors, but it doesn't delete the intended node. I am recursively getting to the intended Node, returning the opposite side node if it only has one child, otherwise I am returning the smallest value from n.right.

Any help would be appreciated - Thanks!

public class BinaryTree {

    private Node root = null;

    private class Node {

        private Node left;
        private Node right;
        private int data;

        public Node(int data, Node left, Node right) {
            this.data = data;
            this.left = left;
            this.right = right;
        }
    }

    public boolean add(int data) {

        root = add(root, data);
        return true;

    }

    private Node add(Node node, int data) {

        if (node == null) {
            node = new Node(data, null, null);
        }

        else {
            if (data < node.data) {
                node.left = add(node.left, data);
            }
            else {
                node.right = add(node.right, data);
            }
        }
        return node;
    }

    // Helper function

    private Node findMin(Node node) {
        while (node.left != null) {
            node = node.left;
        }
        return node;
    }

    private Node findMax(Node node) {
        while (node.right != null) {
            node = node.right;
        }
        return node;
    }

    private void inOrderTraversal(Node n) {
        if (n == null) return;

        else {
            inOrderTraversal(n.left);
            System.out.println(n.data);
            inOrderTraversal(n.right);
        }
    }

    public void inOrderTraversal() {
        inOrderTraversal(root);
    }

    private Node deleteKey(Node n, int data) {

        if (n == null) return n;

        if (data < n.data) deleteKey(n.left, data);
        else if (data > n.data) deleteKey(n.right, data);

        else {
            if (n.left == null) return n.right;
            else if (n.right == null) return n.left;

            n.data =  findMin(n.right).data;
            n.right = deleteKey(n.right, n.data);
        }
        return n;
    }

    public void deleteKey(int data) {
        root = deleteKey(root, data);
    }

    public static void main(String[] args) {

        BinaryTree bst = new BinaryTree();

        bst.add(2);
        bst.add(8);
        bst.add(1);
        bst.add(7);
        bst.add(3);
        bst.add(11);
        bst.add(1);
        bst.add(21);
        bst.add(10);
        bst.add(12);

        bst.inOrderTraversal();

        System.out.println("----------------");

        bst.deleteKey(3);

        bst.inOrderTraversal();
    }
}

I think the problem is in your deleteKey function:

private Node deleteKey(Node n, int data) {

    if (n == null) return n;

    if (data < n.data)
        n.left = deleteKey(n.left, data); // must reassign child here
    else if (data > n.data)
        n.right = deleteKey(n.right, data); // must reassign child here
    else {
        if (n.left == null) return n.right;
        else if (n.right == null) return n.left;

        n.data = findMin(n.right).data;
        n.right = deleteKey(n.right, n.data); // this was correct
    }
    return n;
}

When data < n.data or data > n.data , you aren't updating the current node's leaves. So this was finding the appropriate node to delete, but it wasn't rebuilding the node branches properly on the way back up from the recursion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM