A recursive and a non-recursive program to delete an entire binary tree has already been discussed in the previous posts. In this post, deleting the entire binary tree using the delete keyword in C++ is discussed.
Declare a destructor function in the ‘BinaryTreeNode’ class which has been defined to create a tree node. Using ‘delete’ keyword on an object of a class deletes the entire binary tree., it’s destructor is called within the destructor.
Use the ‘delete’ keyword for the children; so the destructors for the children will be called one by one, and the process will go on recursively until the entire binary tree is deleted. Consider the tree shown below, as soon as the destructor is called for the root i.e., ‘1’, it will call the destructors for ‘2’ and ‘3’, and 2 will then call the same for its left and right child with data ‘4’ and ‘5’ respectively. Eventually, the tree will be deleted in the order: 4->5->2->3->1 (Post-order)

Below is the C++ implementation of the above approach:
C++
#include <iostream>
using namespace std;
class BinaryTreeNode {
public :
int data;
BinaryTreeNode* left;
BinaryTreeNode* right;
BinaryTreeNode( int data)
{
this ->data = data;
this ->left = NULL;
this ->right = NULL;
}
~BinaryTreeNode()
{
delete left;
delete right;
cout << "Deleting " << this ->data << endl;
}
};
int main()
{
BinaryTreeNode* root = new BinaryTreeNode(1);
BinaryTreeNode* node1 = new BinaryTreeNode(2);
BinaryTreeNode* node2 = new BinaryTreeNode(3);
BinaryTreeNode* node3 = new BinaryTreeNode(4);
BinaryTreeNode* node4 = new BinaryTreeNode(5);
root->left = node1;
root->right = node2;
node1->left = node3;
node1->right = node4;
delete root;
return 0;
}
|
Python3
class BinaryTreeNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def __del__( self ):
del self .left
del self .right
print ( "Deleting" , self .data)
root = BinaryTreeNode( 1 )
root.left = BinaryTreeNode( 2 )
root.right = BinaryTreeNode( 3 )
root.left.left = BinaryTreeNode( 4 )
root.left.right = BinaryTreeNode( 5 )
del root
|
C#
using System;
public class BinaryTreeNode
{
public int data;
public BinaryTreeNode left;
public BinaryTreeNode right;
public BinaryTreeNode( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
~BinaryTreeNode()
{
if (left != null )
left = null ;
if (right != null )
right = null ;
Console.WriteLine( "Deleting " + this .data);
}
}
public class Program
{
static void Main( string [] args)
{
BinaryTreeNode root = new BinaryTreeNode(1);
BinaryTreeNode node1 = new BinaryTreeNode(2);
BinaryTreeNode node2 = new BinaryTreeNode(3);
BinaryTreeNode node3 = new BinaryTreeNode(4);
BinaryTreeNode node4 = new BinaryTreeNode(5);
root.left = node1;
root.right = node2;
node1.left = node3;
node1.right = node4;
root = null ;
GC.Collect();
Console.ReadKey();
}
}
|
Javascript
class BinaryTreeNode {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
destructor() {
if ( this .left) {
this .left.destructor();
}
if ( this .right) {
this .right.destructor();
}
console.log( "Deleting " + this .data);
}
}
let root = new BinaryTreeNode(1);
let node1 = new BinaryTreeNode(2);
let node2 = new BinaryTreeNode(3);
let node3 = new BinaryTreeNode(4);
let node4 = new BinaryTreeNode(5);
root.left = node1;
root.right = node2;
node1.left = node3;
node1.right = node4;
root.destructor();
|
Java
class BinaryTreeNode {
public int data;
public BinaryTreeNode left;
public BinaryTreeNode right;
BinaryTreeNode( int data) {
this .data = data;
this .left = null ;
this .right = null ;
}
public void deleteTree() {
if (left != null ) {
left.deleteTree();
}
if (right != null ) {
right.deleteTree();
}
System.out.println( "Deleting " + data);
}
}
public class Gfg {
public static void main(String[] args) {
BinaryTreeNode root = new BinaryTreeNode( 1 );
BinaryTreeNode node1 = new BinaryTreeNode( 2 );
BinaryTreeNode node2 = new BinaryTreeNode( 3 );
BinaryTreeNode node3 = new BinaryTreeNode( 4 );
BinaryTreeNode node4 = new BinaryTreeNode( 5 );
root.left = node1;
root.right = node2;
node1.left = node3;
node1.right = node4;
root.deleteTree();
}
}
|
OutputDeleting 4
Deleting 5
Deleting 2
Deleting 3
Deleting 1