BST删除节点的Java实现

时间:2013-05-15 11:33:40

标签: java binary-search-tree

我正在尝试创建一个方法来删除BST中的节点并且它无法工作..我正在使用我自己制作的Node类...尝试调试但是没有在解决代码中的错误方面获得太多帮助

感谢任何有关如何使其发挥作用的帮助。

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;
}

2 个答案:

答案 0 :(得分:1)

// 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());
}
}

答案 1 :(得分:0)

希望这有帮助

/**
 * 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;
}