简体   繁体   中英

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.

    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.

Could somebody help me with what the bug in code is ?

Note : there is no error in insert and preorder functions . So , you can just focus on deleteTree() code

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;

C++ isn't garbage collected, but Java is. In Java, once an object no longer has any references to it, it will be automatically removed from memory. 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.

In your deleteTree() function, put head=null; //Nothing else.

Remove the other deleteTree(BTNode root) function, its of no use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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