We have discussed recursive implementation to delete an entire binary tree here.
We strongly recommend you to minimize your browser and try this yourself first.
Now how to delete an entire tree without using recursion. This could easily be done with the help of Level Order Tree Traversal. The idea is for each dequeued node from the queue, delete it after queuing its left and right nodes (if any). The solution will work as we are traverse all the nodes of the tree level by level from top to bottom, and before deleting the parent node, we are storing its children into queue that will be deleted later.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
void _deleteTree(Node *root)
{
if (root == NULL)
return ;
queue<Node *> q;
q.push(root);
while (!q.empty())
{
Node *node = q.front();
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
free (node);
}
}
void deleteTree(Node** node_ref)
{
_deleteTree(*node_ref);
*node_ref = NULL;
}
Node* newNode( int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int main()
{
Node *root = newNode(15);
root->left = newNode(10);
root->right = newNode(20);
root->left->left = newNode(8);
root->left->right = newNode(12);
root->right->left = newNode(16);
root->right->right = newNode(25);
deleteTree(&root);
return 0;
}
|
Java
import java.util.*;
class Node
{
int data;
Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
}
class BinaryTree
{
Node root;
void _deleteTree()
{
if (root == null )
return ;
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while (!q.isEmpty())
{
Node node = q.peek();
q.poll();
if (node.left != null )
q.add(node.left);
if (node.right != null )
q.add(node.right);
}
}
void deleteTree()
{
_deleteTree();
root = null ;
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 15 );
tree.root.left = new Node( 10 );
tree.root.right = new Node( 20 );
tree.root.left.left = new Node( 8 );
tree.root.left.right = new Node( 12 );
tree.root.right.left = new Node( 16 );
tree.root.right.right = new Node( 25 );
tree.deleteTree();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def _deleteTree(root):
if root is None :
return
q = []
q.append(root)
while ( len (q)> 0 ):
node = q.pop( 0 )
if node.left is not None :
q.append(node.left)
if node.right is not None :
q.append(node.right)
node = None
return node
def deleteTree(node_ref):
node_ref = _deleteTree(node_ref)
return node_ref
root = Node( 15 )
root.left = Node( 10 )
root.right = Node( 20 )
root.left.left = Node( 8 )
root.left.right = Node( 12 )
root.right.left = Node( 16 )
root.right.right = Node( 25 )
root = deleteTree(root)
|
C#
using System;
using System.Collections.Generic;
public class Node
{
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
}
public class BinaryTree
{
Node root;
void _deleteTree()
{
if (root == null )
return ;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count != 0)
{
Node node = q.Peek();
q.Dequeue();
if (node.left != null )
q.Enqueue(node.left);
if (node.right != null )
q.Enqueue(node.right);
}
}
void deleteTree()
{
_deleteTree();
root = null ;
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(15);
tree.root.left = new Node(10);
tree.root.right = new Node(20);
tree.root.left.left = new Node(8);
tree.root.left.right = new Node(12);
tree.root.right.left = new Node(16);
tree.root.right.right = new Node(25);
tree.deleteTree();
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .left = this .right = null ;
}
}
let root;
function _deleteTree()
{
if (root == null )
return ;
let q = [];
q.push(root);
while (q.length != 0)
{
let node = q.shift();
if (node.left != null )
q.push(node.left);
if (node.right != null )
q.push(node.right);
}
}
function deleteTree()
{
_deleteTree();
root = null ;
}
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);
deleteTree();
</script>
|
Time Complexity: O(n)
As it is a normal level order traversal and we are visiting every node just once.
Auxiliary Space: O(b)
Here b is the breadth of the tree or the maximum number of elements at any level. The extra space is required to store the elements of a level in the queue.
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Jul, 2022
Like Article
Save Article