Delete a binary tree – Iterative and Recursive
Given a binary tree, write an efficient algorithm to delete the entire binary tree. The algorithm should deallocate every single node present in the tree, not just change the root node’s reference to null.
Recursive Solution
The idea is to traverse the tree in a postorder fashion and delete the left and right subtree of a node before deleting the node itself. Note that we cannot traverse a tree in preorder or inorder fashion as we can’t delete a parent before deleting its children.
Following is the C++ program that demonstrates it:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> using namespace std; // Data structure to store a binary tree node struct Node { int key; Node *left, *right; Node(int key) { this->key = key; this->left = this->right = nullptr; } }; // Recursive function to delete a given binary tree void deleteBinaryTree(Node* &root) { // Base case: empty tree if (root == nullptr) { return; } // delete left and right subtree first (Postorder) deleteBinaryTree(root->left); deleteBinaryTree(root->right); // delete the current node after deleting its left and right subtree delete root; // set root as null before returning root = nullptr; } int main() { Node* root = new Node(15); root->left = new Node(10); root->right = new Node(20); root->left->left = new Node(8); root->left->right = new Node(12); root->right->left = new Node(16); root->right->right = new Node(25); // delete the entire tree deleteBinaryTree(root); if (root == nullptr) { cout << "Tree Successfully Deleted"; } return 0; } |
The time complexity of the above recursive solution is O(n), where n
is the total number of nodes in the binary tree. The auxiliary space required by the program is O(h) for the call stack, where h
is the height of the tree.
Iterative Solution
In the iterative version, perform a level order traversal on the tree. The idea is to delete each node in the queue, one by one, after enqueuing their children. Note that the parent is deleted before deleting its children as we are enqueuing them, and they will be processed and deleted afterward.
This is demonstrated below in C++:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <iostream> #include <queue> using namespace std; // Data structure to store a binary tree node struct Node { int key; Node *left, *right; Node(int key) { this->key = key; this->left = this->right = nullptr; } }; // Iterative function to delete a given binary tree void deleteBinaryTree(Node* &root) { // empty tree if (root == nullptr) { return; } // create an empty queue and enqueue the root node queue<Node*> queue; queue.push(root); Node* front = nullptr; // loop till queue is empty while (!queue.empty()) { // delete each node in the queue one by one after pushing their // non-empty left and right child to the queue front = queue.front(); queue.pop(); if (front->left) { queue.push(front->left); } if (front->right) { queue.push(front->right); } // it is important to delete the front node ONLY after enqueuing its children delete front; } // set root as null before returning root = nullptr; } int main() { Node* root = new Node(15); root->left = new Node(10); root->right = new Node(20); root->left->left = new Node(8); root->left->right = new Node(12); root->right->left = new Node(16); root->right->right = new Node(25); // delete the entire tree deleteBinaryTree(root); if (root == nullptr) { cout << "Tree Successfully Deleted"; } return 0; } |
The time complexity of the above iterative solution is O(n), where n
is the total number of nodes in the binary tree. The auxiliary space required by the program is O(n) for the queue data structure.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)