简体   繁体   English

删除二叉树-设计建议

[英]Delete Binary Tree - Suggestions on Design

I have written a code for deleting all elements of tree. 我已经编写了删除树的所有元素的代码。 Need suggestions for following: 需要以下建议:

  1. In reverseTreeStack method, Can I design without using stack method parameter? 在reverseTreeStack方法中,可以不使用堆栈方法参数进行设计吗?
  2. Can I design the entire code in 1 method with better design? 我可以用更好的设计以一种方法设计整个代码吗?

UPDATE : Changed return type of reverseTreeStack to void.Removed additional variable for stack. 更新:将reverseTreeStack的返回类型更改为void。移除了堆栈的其他变量。

    public class DeleteTree {

    public static void deleteTree(BinaryTreeNode root)
    {       
        Stack stack = new Stack();
        reverseTreeStack(stack, root);
        while (!stack.isEmpty())
        {
            BinaryTreeNode node = (BinaryTreeNode)stack.pop();
            System.out.println("---------Deleting----------->" + node.getData());
            node = null;
        }
    }

    public static void reverseTreeStack(Stack stack,BinaryTreeNode root)
    {
        if (root != null)
        {
            stack.push(root);   
            reverseTreeStack(stack,root.getLeft());
            reverseTreeStack(stack, root.getRight());
        }
    }
}

Why do you need to do this? 为什么需要这样做? If I recall correctly, the JVM can free resources once there are no available references to the resource, so just setting your root node to be null should free the whole tree. 如果我没记错的话,一旦没有可用的对资源的引用,JVM便可以释放资源,因此只需将根节点设置为null即可释放整个树。

I think, James is right, but if you want to practice the tree traversal, or if you want to implement this in a language where you need to free memory manually, then use recursion: 我认为,James是对的,但是如果您想进行树遍历,或者想要以需要手动释放内存的语言来实现它,则可以使用递归:

void deleteTree(TreeNode node)
{
    if(node==null)return;

    deleteTree(node.getLeft());
    deleteTree(node.getRight());

    System.out.printline("Deleting: "+node.getData())
    node = null;
}

Also take a look at Postorder Traversal (thats the only one, that works for deleting) 还请看一下Postorder Traversal (那是唯一的后删除遍历

Well your reverseTreeStack method can potentially give you a StackOverflowError if your tree is too large, so using a loop instead of recursion there might be a better choice (unless you know for a fact that your trees will never be that large). 好吧,如果树太大,您的reverseTreeStack方法可能会给您一个StackOverflowError ,因此使用循环而不是递归可能是更好的选择(除非您知道树永远不会那么大)。

Also, why are you "deleting" every node? 另外,为什么要“删除”每个节点? ( node = null actually just removes the reference you have just in that method...) Generally just forgetting the root ( root = null ) will delete your whole tree if you're structuring it in the classic way of Node(parent, leftChild, rightChild) and not storing pointers to nodes anywhere else. node = null实际上只是删除了该方法中的引用...)通常,如果您以经典的Node(parent,leftChild)方式构造树,则忘记根( root = null )将会删除整个树。 ,rightChild),并且不会在其他任何地方存储指向节点的指针。

1) I think you can kill the return value and make it a void method as you are directly manipulating the stack. 1)我认为您可以取消返回值并将其设置为void方法,因为您直接操作堆栈。 So just do 所以做

 Stack stack = new Stack();
 reverseTreeStack(stack, root);

 // Now just use stack

2) Don't condense things into one method. 2)不要将事物浓缩为一种方法。 Breaking things out into more methods will make your code easier to navigate and understand. 将事情分解为更多的方法将使您的代码更易于浏览和理解。 The less each function is responsible for, the more sense it will make to someone reading it. 每个函数负责的越少,它对阅读它的人的意义就越大。

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

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