【问题标题】:Binary Search Tree not deleting Node二叉搜索树不删除节点
【发布时间】:2018-10-04 10:18:36
【问题描述】:

我创建了一个Binary Search Tree 并尝试在添加特定节点后删除它们。我可以成功 delete 大约 5 Nodes 但是当我尝试删除 Node with id 109 它只是忽略它并且没有任何反应。我试过很多方法来删除它,但它不起作用。

myBinaryTree.deleteNode(myBinaryTree.root, 109);

这是我的二叉树中的delete 方法。

public Node deleteNode(Node root, int ID){

    if (root == null)  return root;
    if (ID < root.ID)
        root.leftChild = deleteNode(root.leftChild, ID);
    else if (ID > root.ID)
        root.rightChild = deleteNode(root.rightChild, ID);

    else
    {
        if (root.leftChild == null)
            return root.rightChild;
        else if (root.rightChild == null)
            return root.leftChild;

        root.ID = minValue(root.rightChild);
        root.rightChild = deleteNode(root.rightChild, root.ID);
    }

    return root;
}


int minValue(Node root)
{
    int minv = root.ID;
    while (root.leftChild != null)
    {
        minv = root.leftChild.ID;
        root = root.leftChild;
    }
    return minv;
}

还有我的Node

public class Node {
    int ID;
    Dancer.Gender gender;
    int height;

    Node leftChild;
    Node rightChild;

    Node(int ID, Dancer.Gender gender, int height) {

        this.ID = ID;
        this.gender = gender;
        this.height = ID;

    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

}

ID 按预期工作意味着deleteNode 方法获得了正确的 ID,它只是不删除它。

这是我要从中删除的一棵树的图片:

如果需要有关如何添加节点等的更多信息,那么我也可以提供。在我尝试使用ID = 109 删除节点之前,这一切都很奇怪。

【问题讨论】:

  • 你调试过吗?我会怀疑ID &gt; root.ID 不是真的,或者不会出现在右侧。
  • 你可能需要稍后修复的东西:this.height = ID; 它来自 Node 构造函数。
  • @achAmháin 它最终在 else if (root.rightChild == null) return root.leftChild
  • @dbl 目前是打算的,但以后会做
  • 这意味着它错误地超过了else if (ID &gt; root.ID)。您能否提供您传递给deleteNodeNode 代码?

标签: java binary-search-tree nodes


【解决方案1】:

您的代码看起来不错。

顺便问一下,你是如何验证节点没有被删除的? 我刚刚检查了您的代码并打印了中序遍历。而且效果很好。

// This is java code.
void inorder(Node root){
    if (root ==null)return;
    inorder(root.leftChild);
    System.out.print(root.ID + "  ");
    inorder(root.rightChild);
}

// verify deletion by printing inorder traversal before and after
public static void main(String[] args) {
    // creating the tree
    Node root = new Node(60);
    root.leftChild = new Node(40);
    root.rightChild = new Node(109);
    root.leftChild.leftChild = new Node(20);
    root.leftChild.rightChild = new Node(49);

    inorder(root); // Printing before deleting
    System.out.println();
    myBinaryTree.root = deleteNode(myBinaryTree.root, 109); // delete the node and collect the new reference of the root.
    inorder(root); // Printing after tree
    System.out.println();
}

【讨论】:

  • 通过打印剩余的节点并检查 109 是否仍在其中,我不知道为什么,但 waitingLineWithFemales.root =... 实际上将其从树中删除,即使之前的节点不需要它
  • 你能显示“打印出剩余节点”的代码吗?
  • 并且通过使用您的顺序,它仍然会留下 109,但参考部分有效
  • 参考部分有效,但仅删除不参考仍然无效
  • 不过没关系,我可以使用参考,如果你把答案改回参考部分我可以接受
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
相关资源
最近更新 更多