简体   繁体   中英

Java implementation of BST delete node

I'm trying to make a method to delete a node in a BST and its not working..I'm using a Node class I made myself.. tried debugging but didnt get much help on addressing the error in the code.

Appreciate any help on how to make it work.

public boolean delete(Node z)
{
    if (z == null)
        return false;

    Node x,y;
    if( z.getLeft() == null || z.getRight()== null)
        y = z;
    else { 
        y = (Node) successor(z);
    }
    if (y.getLeft() != null)
        x = y.getLeft();
    else x = y.getRight();
    if(x != null)
        x.setParent(y.getParent());
    if(y.getParent() == null) {
        this.node=x;
    }
    else if (y == y.getParent().getLeft())
    {
            y.getParent().setLeft(x);
    }
        else y.getParent().setRight(x);

    if(y==z)
        z.setKey(y.getKey());
    return true;    
}


public Node treeMinimum(Node x) {
    while (x.getLeft() != null)
        x = x.getLeft();

    return x;
}

public Node successor(Node node) {
    Node x = node;

    if (x.getRight() != null)
        return treeMinimum(x.getRight());

    Node y = x.getParent();
    while (y != null && x == y.getRight()) {
        x = y;
        y = y.getParent();
    }

    return y;
}
// Here is the complete solution
public void delete(final Integer data) {
    root = delete(root, data);
}

public Node<OInfo> delete(Node<OInfo> node, final Integer data) {
    if (node == null) {
        return null;
    }

    int cmp = data.compareTo(new Integer(node.getData().data));
    if (cmp < 0) {
        node.setLeft(delete(node.getLeft(), data));
    } else if (cmp > 0) {
        node.setRight(delete(node.getRight(), data));
    } else {
        boolean isRoot = false;
        if (root == node) {
            isRoot = true;
        }
        if (node.getLeft() == null) {
            return node.getRight();
        }
        if (node.getRight() == null) {
            return node.getLeft();
        }
        Node<OInfo> t = node;
        node = findMinimum(t.getRight());
        node.setRight(deleteMin(t.getRight()));
        node.setLeft(t.getLeft());
        if (isRoot) {
            root = node;
        }
    }

    return node;
}

private Node<OInfo> deleteMin(Node<OInfo> node) {
if (node.getLeft() == null) {
    return node.getRight();
    }
node.setLeft(deleteMin(node.getLeft()));
return node;
}

private Node<OInfo> findMinimum(Node<OInfo> node) {
if (node == null) {
    throw new IllegalArgumentException("Null value of node provided.");
}

if (node.getLeft() == null) {
    return node;
} else {
    return findMinimum(node.getLeft());
}
}

Hope this helps

/**
 * Internal method to remove from a subtree.
 *
 * @param x the item to remove.
 * @param t the node that roots the tree.
 * @return the new root.
 * @throws ItemNotFoundException if x is not found.
 */
protected BinaryNode remove(Comparable x, BinaryNode t) {
    if (t == null) {
        throw new ItemNotFoundException(x.toString());
    }
    if (x.compareTo(t.element) < 0) {
        t.left = remove(x, t.left);
    } else if (x.compareTo(t.element) > 0) {
        t.right = remove(x, t.right);
    } else if (t.left != null && t.right != null) { // Two children
        t.element = findMin(t.right).element;
        t.right = removeMin(t.right);
    } else {
        t = (t.left != null) ? t.left : t.right;
    }
    return t;
}

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