简体   繁体   English

删除二叉树中的节点

[英]Deleting a node in Binary Tree

Below is the code I wrote for the case when the deleteItem is at the leaf node. 下面是我为deleteItem在叶节点上的情况编写的代码。 Even if I am equating the found leaf node to "null" then also when I print the inorder traversing order if the tree, that element is not deleted and comes on the screen. 即使我将找到的叶节点等同于“ null”,那么当我打印树的有序遍历顺序时,该元素也不会被删除并出现在屏幕上。 What am I missing? 我想念什么?

public void deleteNode(T deleteItem)
    {
        TreeNode<T> node = root;
        if(root == null)
        {
            System.out.print("Binary Tree does not exist");
            System.exit(0);
        }
        while((node.data != deleteItem) && (node.leftNode != null || node.rightNode != null))
        {
            if(deleteItem.compareTo(node.data)<0)
                node = node.leftNode;
            else
                node = node.rightNode;
        }
        //System.out.printf("deleting item is: %s\n", node.data);
        if(node.data != deleteItem)
        {   
            System.out.println("\ndeleteItem not found in the tree");
            System.exit(0);
        }
        else
        {
            if(node.leftNode == null && node.rightNode == null)
            {
                node = null;
            }
        }
    }

Java is "Pass-by-value" , so TreeNode<T> tmp = root here means assigning tmp 's references to root which means simply you have two different references to the same instance. Java是“按值传递” ,因此TreeNode<T> tmp = root表示将tmp的引用分配给root ,这意味着您对同一实例具有两个不同的引用。 According to that when you say node = null you set your local references to null which means that the node's parent is still reference to it. 据此,当您说node = null ,请将您的本地引用设置为null ,这意味着该节点的父级仍在引用它。 So to remove leaf node from the tree you need to keep track of its parent and then removing the reference to the node (removed the reference in the tree not you local copy) which will be something like that: 因此,要从树中删除叶节点,您需要跟踪其父节点,然后删除对该节点的引用(删除树中的引用而不是本地副本) ,这将是这样的:

public void deleteNode(T deleteItem) {
        TreeNode<T> node = root;
        TreeNode<T> parent = null; // parent of the deleted node.
        if(root == null) {
            System.out.print("Binary Tree does not exist");
            System.exit(0);
        }
        while((node.data != deleteItem) && 
              (node.leftNode != null || node.rightNode != null)) {
            parent = node; // keep track of the parent of the deleted node
            if(deleteItem.compareTo(node.data) < 0)
                node = node.leftNode;
            else
                node = node.rightNode;
        }
        if(node.data != deleteItem) {   
            System.out.println("\ndeleteItem not found in the tree");
            System.exit(0);
        }
        else {
            if(parent.leftNode == node)
                parent.leftNode = null;
            else
                parent.rightNode = null;
        }
    }

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

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