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++ program to delete the entire binary ` `// tree using the delete keyword ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `class` `BinaryTreeNode { ` ` ` ` ` `// Making data members public to ` ` ` `// avoid the usage of getter and setter functions ` `public` `: ` ` ` `int` `data; ` ` ` `BinaryTreeNode* left; ` ` ` `BinaryTreeNode* right; ` ` ` ` ` `// Constructor function to ` ` ` `// assign data to the node ` ` ` `BinaryTreeNode(` `int` `data) ` ` ` `{ ` ` ` `this` `->data = data; ` ` ` `this` `->left = NULL; ` ` ` `this` `->right = NULL; ` ` ` `} ` ` ` ` ` `// Destructor function to delete the tree ` ` ` `~BinaryTreeNode() ` ` ` `{ ` ` ` `// using keyword to delete the tree ` ` ` `delete` `left; ` ` ` `delete` `right; ` ` ` ` ` `// printing the node which has been deleted ` ` ` `cout << ` `"Deleting "` `<< ` `this` `->data << endl; ` ` ` `} ` `}; ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Creating the nodes dynamically ` ` ` `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); ` ` ` ` ` `// Creating the binary tree ` ` ` `root->left = node1; ` ` ` `root->right = node2; ` ` ` `node1->left = node3; ` ` ` `node1->right = node4; ` ` ` ` ` `// Calls the destructor function which actually deletes the tree entirely ` ` ` `delete` `root; ` ` ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

**Output:**

Deleting 4 Deleting 5 Deleting 2 Deleting 3 Deleting 1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Binary Tree to Binary Search Tree Conversion using STL set
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Non-recursive program to delete an entire binary tree
- Delete the last leaf node in a Binary Tree
- Binary Search Tree | Set 3 (Iterative Delete)
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a binary tree is a full binary tree or not
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search tree
- Binary Tree | Set 3 (Types of Binary Tree)
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
- Difference between Binary Tree and Binary Search Tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Write a program to Delete a Tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.