简体   繁体   English

如何删除二叉树

[英]How to delete a binary tree

Logic is simple - traverse the root to end in post order and then null the nodes. 逻辑很简单-遍历根以后序结束,然后使节点为空。 Below is the code I have written for deleting all the nodes of a tree (ie deleting a binary tree). 下面是我编写的用于删除树的所有节点(即删除二叉树)的代码。

Problem: The actual tree is not deleted. 问题:实际的树没有被删除。 I mean the deleteTree(BTNode root) function only null all the values of root ref and not of head. 我的意思是deleteTree(BTNode root)函数仅将root ref的所有值都设为null,而不是head的所有值。

    tree.preorder();
    tree.deleteTree();
    tree.preorder();- this still prints all values of a tree

Even after doing tree.deleteTree(), it prints all nodes in a tree. 即使执行tree.deleteTree()之后,它也会打印树中的所有节点。

Could somebody help me with what the bug in code is ? 有人可以帮我解决代码中的错误吗?

Note : there is no error in insert and preorder functions . 注意 :insert和preorder函数中没有错误。 So , you can just focus on deleteTree() code 因此,您可以只关注deleteTree()代码

package com.practice;

import java.util.LinkedList;

public class BinaryTree {
    private BTNode head;

    public void insert(int data){
        BTNode n= new BTNode (data);
        BTNode temp=null;

        if(head==null){
            head=n;
            return;
        }
        else{
            LinkedList q= new LinkedList();
            q.addLast(head); //enque
            while(!q.isEmpty()){
                    temp=(BTNode) q.removeFirst();
                    if( temp.getLeft() ==null){

                            temp.setLeft(n);
                            return;
                    }
                    else{
                    //enque
                        q.addLast( temp.getLeft());
                    }

                    if( temp.getRight() ==null){
                        temp.setRight(n);
                        return;
                }
                else{
                //enque
                    q.addLast( temp.getRight());
                }
            }//while loop ends here
        }
    }//insert ends here

    public void preorder(){
        preorder(head);
    }

   private void preorder(BTNode head){
       if(head==null){
           return;
       }
       else{
           System.out.println(head.getData());\
                 preorder(head.getLeft());

           preorder(head.getRight());
       }
    }

 public void deleteTree(){
     deleteTreeInternal(this.head);
 }

 private void deleteTree(BTNode root){
     if(root == null){
         return ;
     }
     else{
         deleteTreeInternal(root.getLeft());
         deleteTreeInternal(root.getRight());

         root=null;
     }
 }
}//class ends here
package com.practice;

public class BTNode {
    private BTNode left;
    private BTNode right;
    private int data;

    public BTNode(){
    }

    public BTNode(int data){
        this.data=data;
        this.left=null;
        this.right=null;
    }

    public BTNode getLeft() {
        return left;
    }
    public void setLeft(BTNode left) {
        this.left = left;
    }
    public BTNode getRight() {
        return right;
    }
    public void setRight(BTNode right) {
        this.right = right;
    }
    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
}

Just set root=null; 只需设置root=null;

C++ isn't garbage collected, but Java is. C ++不是垃圾收集,而Java是。 In Java, once an object no longer has any references to it, it will be automatically removed from memory. 在Java中,一旦对象不再对其具有任何引用,它将自动从内存中删除。 All of the referenced objects of the garbage collected object will also be removed if they have no other references to them. 如果垃圾回收对象的所有引用对象没有其他引用,也将被删除。 That bit addresses your question: if the nodes under root don't have any external references, they will be automatically removed after root. 那一点解决了您的问题:如果root下的节点没有任何外部引用,则将在root之后自动将其删除。

In your deleteTree() function, put head=null; 在您的deleteTree()函数中,将head=null; //Nothing else. //没有其他的。

Remove the other deleteTree(BTNode root) function, its of no use. 删除另一个deleteTree(BTNode root)函数,该函数deleteTree(BTNode root)

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

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