BST:在 Java 中删除具有 2 个子节点的节点

Posted

技术标签:

【中文标题】BST:在 Java 中删除具有 2 个子节点的节点【英文标题】:BST: deleting node with 2 children in Java 【发布时间】:2016-05-04 14:25:32 【问题描述】:

我在 BST 中实现了用于删除节点的递归算法,但如果要删除的节点有两个子节点,它似乎无法正常工作。这是用于删除节点的方法的代码:

public boolean delete(int val)



    Node nodeToBeDeleted = find(val);
    if(nodeToBeDeleted != null)
    
        //case 1: node has no children
        if(nodeToBeDeleted.leftChild == null && nodeToBeDeleted.rightChild == null)
            deleteCase1(nodeToBeDeleted);

        //case 3: node has two children
        else if(nodeToBeDeleted.leftChild != null && nodeToBeDeleted.rightChild != null)
        
            deleteCase3(nodeToBeDeleted);
        

        //case 2: node has one child
        else if(nodeToBeDeleted.leftChild != null)
        
            //case 2 where left child should be deleted
            deleteCase2(nodeToBeDeleted);
        

        else if(nodeToBeDeleted.rightChild != null)
        
            //case 2 where right child should be deleted
            deleteCase2(nodeToBeDeleted);
        

        return true;
    
    else
        return false;

这里还有deleteCase1、deleteCase2和deleteCase3方法:

private void deleteCase1(Node nodeToBeDeleted)

        //check if node to be deleted is a left or a right child of the parent of the node to be deleted
        if(nodeToBeDeleted.parent.leftChild == nodeToBeDeleted)
        
            nodeToBeDeleted.parent.leftChild = null;
        
        else if(nodeToBeDeleted.parent.rightChild == nodeToBeDeleted)
        
            nodeToBeDeleted.parent.rightChild = null;
        

这里找方法:

public Node find(int val)

    if(root != null)
    
        return findNode(root, new Node(val));
    

    return null;


private Node findNode(Node search, Node node)

    if(search == null)
        return null;

    if(search.data == node.data)
    
        return search;
    
    else
    
        Node returnNode = findNode(search.leftChild, node);

        if(returnNode == null)
        
            returnNode = findNode(search.rightChild, node);
        

        return returnNode;
    

minLeftTreversal 方法:

private Node minLeftTreversal(Node node)

    if(node.leftChild == null)
        return node;

    return minLeftTreversal(node.leftChild);

树的结构如下所示: enter image description here

如果我删除 75,算法会起作用,但如果我尝试删除 25,它就会搞砸。

提前谢谢你!

【问题讨论】:

【参考方案1】:

public boolean delete(int val) 中的第一个 if 语句缺少

        //case 1: node has no children
        if(nodeToBeDeleted.leftChild == null && nodeToBeDeleted.rightChild == null)
         // <---- ADD THIS
            deleteCase1(nodeToBeDeleted);
         // <---- AND ADD THIS
        //case 3: node has two children
        else if(nodeToBeDeleted.leftChild != null && nodeToBeDeleted.rightChild != null)
        
            deleteCase3(nodeToBeDeleted);
        

【讨论】:

以上是关于BST:在 Java 中删除具有 2 个子节点的节点的主要内容,如果未能解决你的问题,请参考以下文章

B+树相对于BST的优势?

java 450.删除BST中的节点(#1).java

java 450.删除BST中的节点(#1).java

java 450.删除BST中的节点(#1).java

java 450.删除BST中的节点(#1).java

java 450.删除BST中的节点(#1).java