简体   繁体   English

从Java中的二进制搜索树中删除节点

[英]Deleting a node from Binary Search Tree in Java

I've been trying to delete a node from a BST. 我一直在尝试从BST中删除节点。 I'm taking help of two functions where one (findInorderSuccesor) is being called when a node has two Children. 我正在接受两个函数的帮助,当一个节点有两个Child时,会调用一个函数(findInorderSuccesor)。 Problem is, the node that comes in as a replacement for the deleted node, is not getting deleted from it's original position. 问题是,作为已删除节点的替代而进入的节点不会从其原始位置删除。 As a result, I've two nodes with the same value. 结果,我有两个具有相同值的节点。

    obj.addNode(8);
    obj.addNode(2);
    obj.addNode(5);
    obj.addNode(1);
    obj.addNode(13);
    obj.addNode(10);
    obj.addNode(15);

    obj.deleteNode(obj.root,8);

    public void deleteNode(treeNode focusNode, int data)
    {
     if(data<focusNode.data)
        deleteNode(focusNode.left,data);
    else if (data>focusNode.data)
        deleteNode(focusNode.right,data);
    else
    {
        if(focusNode.right == null && focusNode.left == null)
            focusNode=null;
        else if(focusNode.left!=null && focusNode.right==null)
            focusNode = focusNode.left;
        else if (focusNode.right!=null && focusNode.left==null)
            focusNode = focusNode.right;
        else
        {
            //node has two children
            BSTDeletion obj = new BSTDeletion();
            treeNode replacement =obj.findInorderSuccessor(focusNode.right);
            focusNode.data = replacement.data;
            deleteNode(focusNode.right, replacement.data);

        }
    }
}



public treeNode findInorderSuccessor(treeNode focusNode)
{
treeNode preFocusNode = null;
 while(focusNode!=null)
 {
    preFocusNode = focusNode;
    focusNode = focusNode.left;
 }
return preFocusNode;
}

BFS遍历树

As Boola wrote, You need to know the parent of the node You are deleting. 正如Boola所写,您需要知道要删除的节点的父节点。

When you say focusNode = null; 当你说focusNode = null; you only make the reference null, you don't remove the object from the tree, because the parent is still referencing that node. 您只将引用设为null,不要从树中删除该对象,因为父级仍在引用该节点。 You need something like this: 你需要这样的东西:

public void deleteNode(treeNode focusNode, int data)
{
 if(data<focusNode.data)
    deleteNode(focusNode.left,data);
else if (data>focusNode.data)
    deleteNode(focusNode.right,data);
else
{
    treeNode parent = focusNode.getParent();  // get the parent.
if(focusNode.left==null && focusNode.right==null) 
    {
        if(parent.left.equals(focusNode))
             parent.left = null;                //Set the parents reference to null. 
        else
             parent.Right = null;
    }
 else if(focusNode.left!=null && focusNode.right==null)
    {
        if(parent.left.equals(focusNode))
             parent.left = focusNode.left;  //Reassign the parents reference to the correct node. 
        else
             parent.right = focusNode.left;
    }

and so on. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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