Delete entire nodes of a binary tree
Here given code implementation process.
/*
C Program
Delete entire nodes of a binary tree
Using queue
*/
#include <stdio.h>
#include <stdlib.h>
//Binary Tree node
struct Node
{
int data;
struct Node *left, *right;
};
//Define Custom queue
struct MyQueue
{
struct Node *element;
struct MyQueue *next;
};
//This is creating a binary tree node and return this
struct Node *add_node(int data)
{
// Create dynamic node
struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
if (new_node != NULL)
{
//Set data and pointer values
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return new_node;
}
//Recursive function
//Display preorder view of binary tree
void pre_order(struct Node *node)
{
if (node != NULL)
{
//Print node value
printf(" %d", node->data);
pre_order(node->left);
pre_order(node->right);
}
}
//Create a MyQueue element and return this reference
struct MyQueue *queue_node(struct Node *tree_node)
{
struct MyQueue *MyQueue_node = (struct MyQueue *) malloc(sizeof(struct MyQueue));
if (MyQueue_node != NULL)
{
//set pointer values
MyQueue_node->element = tree_node;
MyQueue_node->next = NULL;
}
else
{
printf("Memory Overflow\n");
exit(0); //Terminate program execution
}
return MyQueue_node;
}
//Remove a MyQueue elements
void dequeue(struct MyQueue **head)
{
if ( *head != NULL)
{
//Get a deleted node
struct MyQueue *remove = *head;
//Visit to next node of MyQueue
*head = remove->next;
//unlink tree element
remove->element = NULL;
//unlink next node
remove->next = NULL;
//free node
free(remove);
remove = NULL;
}
}
// Deleting all nodes in given tree in from top to bottom direction level by level
// Using queue
struct Node *delete_all_nodes(struct Node *root)
{
if (root == NULL)
{
printf("\nEmpty Tree\n");
return NULL;
}
else
{
// Before delete, display tree elements
printf("\n Tree Element \n");
pre_order(root);
//Make MyQueue variables
struct MyQueue *head = NULL;
struct MyQueue *tail = NULL;
//Get first node of tree
struct Node *auxiliary = root;
//Add first node into MyQueue
head = queue_node(root);
//initial tail is same to head of queue
tail = head;
//iterate the MyQueue elements
while (head != NULL)
{
//Get current head element of MyQueue
auxiliary = head->element;
if (auxiliary->left != NULL)
{
//Add new left child node
tail->next = queue_node(auxiliary->left);
tail = tail->next;
}
if (auxiliary->right != NULL)
{
//Add new right child node
tail->next = queue_node(auxiliary->right);
tail = tail->next;
}
//Unlink left subtree
auxiliary->left = NULL;
//Remove element of MyQueue
head->element = NULL;
dequeue( &head);
printf("\n Delete Node : %d", auxiliary->data);
//Deleted tree element
auxiliary->left = NULL;
auxiliary->right = NULL;
free(auxiliary);
auxiliary = NULL;
}
tail = NULL;
return NULL;
}
}
int main()
{
struct Node *root = NULL;
/*
Construct Binary Tree
-----------------------
10
/ \
6 8
/ / \
2 7 2
/ \ \
9 -1 9
*/
//Add nodes
root = add_node(10);
root->left = add_node(6);
root->left->left = add_node(2);
root->right = add_node(8);
root->right->right = add_node(2);
root->right->left = add_node(7);
root->left->left->left = add_node(9);
root->right->left->right = add_node(-1);
root->right->right->right = add_node(9);
//Test Process
root = delete_all_nodes(root);
return 0;
}
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
// Java Program
// Delete entire nodes of a binary tree
// Using queue
//Binary Tree node
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
//set node value
this.data = data;
this.left = null;
this.right = null;
}
}
//Define Custom queue
class MyQueue
{
public Node element;
public MyQueue next;
public MyQueue(Node element)
{
this.element = element;
this.next = null;
}
}
class BinaryTree
{
public Node root;
public BinaryTree()
{
//set initial tree root to null
this.root = null;
}
//Recursive function
//Display preorder view of binary tree
public void pre_order(Node node)
{
if (node != null)
{
//Print node value
System.out.print(" " + node.data);
pre_order(node.left);
pre_order(node.right);
}
}
//Remove a dequeue element
public MyQueue dequeue(MyQueue head)
{
if (head != null)
{
MyQueue temp = head.next;
//remove node
head.element = null;
head.next = null;
return temp;
}
return null;
}
// Deleting all nodes in given tree in from top to bottom direction level by level
// Using queue
public void delete_all_nodes()
{
if (this.root == null)
{
System.out.print("\nEmpty Tree\n");
}
else
{
// Before delete, display tree elements
System.out.print("\n Tree Element \n");
pre_order(this.root);
//Make MyQueue variables
//Add first node into MyQueue
MyQueue head = new MyQueue(this.root);
//initial tail is same to head of queue
MyQueue tail = head;
//Get first node of tree
Node auxiliary = this.root;
this.root = null;
//iterate the MyQueue elements
while (head != null)
{
//Get current head element of MyQueue
auxiliary = head.element;
if (auxiliary.left != null)
{
//Add new left child node
tail.next = new MyQueue(auxiliary.left);
tail = tail.next;
}
if (auxiliary.right != null)
{
//Add new right child node
tail.next = new MyQueue(auxiliary.right);
tail = tail.next;
}
//unlink left subtree
auxiliary.left = null;
//Remove element of MyQueue
head.element = null;
head = dequeue(head);
System.out.print("\n Delete Node : " + auxiliary.data);
//Deleted tree element
auxiliary.left = null;
auxiliary.right = null;
auxiliary = null;
}
tail = null;
}
}
public static void main(String[] args)
{
//Make object
BinaryTree obj = new BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/ \
6 8
/ / \
2 7 2
/ \ \
9 -1 9
*/
//Add nodes
obj.root = new Node(10);
obj.root.left = new Node(6);
obj.root.left.left = new Node(2);
obj.root.right = new Node(8);
obj.root.right.right = new Node(2);
obj.root.right.left = new Node(7);
obj.root.left.left.left = new Node(9);
obj.root.right.left.right = new Node(-1);
obj.root.right.right.right = new Node(9);
//Test Process
obj.delete_all_nodes();
}
}
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
//Include header file
#include <iostream>
using namespace std;
// C++ Program
// Delete entire nodes of a binary tree
// Using queue
//Binary Tree node
class Node
{
public: int data;
Node *left;
Node *right;
Node(int data)
{
//set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
//Define Custom queue
class MyQueue
{
public: Node *element;
MyQueue *next;
MyQueue(Node *element)
{
this->element = element;
this->next = NULL;
}
};
class BinaryTree
{
public: Node *root;
BinaryTree()
{
//set initial tree root to null
this->root = NULL;
}
//Recursive function
//Display preorder view of binary tree
void pre_order(Node *node)
{
if (node != NULL)
{
//Print node value
cout << " " << node->data;
this->pre_order(node->left);
this->pre_order(node->right);
}
}
//Remove a dequeue element
MyQueue *dequeue(MyQueue *head)
{
if (head != NULL)
{
MyQueue *temp = head->next;
//remove node
head->element = NULL;
head->next = NULL;
delete head;
head = NULL;
return temp;
}
return NULL;
}
// Deleting all nodes in given tree in from top to bottom direction level by level
// Using queue
void delete_all_nodes()
{
if (this->root == NULL)
{
cout << "\nEmpty Tree\n";
}
else
{
// Before delete, display tree elements
cout << "\n Tree Element \n";
this->pre_order(this->root);
//Make MyQueue variables
//Add first node into MyQueue
MyQueue *head = new MyQueue(this->root);
//initial tail is same to head of queue
MyQueue *tail = head;
//Get first node of tree
Node *auxiliary = this->root;
this->root = NULL;
//iterate the MyQueue elements
while (head != NULL)
{
//Get current head element of MyQueue
auxiliary = head->element;
if (auxiliary->left != NULL)
{
//Add new left child node
tail->next = new MyQueue(auxiliary->left);
tail = tail->next;
}
if (auxiliary->right != NULL)
{
//Add new right child node
tail->next = new MyQueue(auxiliary->right);
tail = tail->next;
}
//unlink left subtree
auxiliary->left = NULL;
//Remove element of MyQueue
head->element = NULL;
head = this->dequeue(head);
cout << "\n Delete Node : " << auxiliary->data;
//Deleted tree element
auxiliary->left = NULL;
auxiliary->right = NULL;
delete auxiliary;
auxiliary = NULL;
}
tail = NULL;
}
}
};
int main()
{
//Make object
BinaryTree obj = BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/ \
6 8
/ / \
2 7 2
/ \ \
9 -1 9
*/
//Add nodes
obj.root = new Node(10);
obj.root->left = new Node(6);
obj.root->left->left = new Node(2);
obj.root->right = new Node(8);
obj.root->right->right = new Node(2);
obj.root->right->left = new Node(7);
obj.root->left->left->left = new Node(9);
obj.root->right->left->right = new Node(-1);
obj.root->right->right->right = new Node(9);
//Test Process
obj.delete_all_nodes();
return 0;
}
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
//Include namespace system
using System;
// C# Program
// Delete entire nodes of a binary tree
// Using queue
//Binary Tree node
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
//set node value
this.data = data;
this.left = null;
this.right = null;
}
}
//Define Custom queue
class MyQueue
{
public Node element;
public MyQueue next;
public MyQueue(Node element)
{
this.element = element;
this.next = null;
}
}
class BinaryTree
{
public Node root;
public BinaryTree()
{
//set initial tree root to null
this.root = null;
}
//Recursive function
//Display preorder view of binary tree
public void pre_order(Node node)
{
if (node != null)
{
//Print node value
Console.Write(" " + node.data);
pre_order(node.left);
pre_order(node.right);
}
}
//Remove a dequeue element
public MyQueue dequeue(MyQueue head)
{
if (head != null)
{
MyQueue temp = head.next;
//remove node
head.element = null;
head.next = null;
return temp;
}
return null;
}
// Deleting all nodes in given tree in from top to bottom direction level by level
// Using queue
public void delete_all_nodes()
{
if (this.root == null)
{
Console.Write("\nEmpty Tree\n");
}
else
{
// Before delete, display tree elements
Console.Write("\n Tree Element \n");
pre_order(this.root);
//Make MyQueue variables
//Add first node into MyQueue
MyQueue head = new MyQueue(this.root);
//initial tail is same to head of queue
MyQueue tail = head;
//Get first node of tree
Node auxiliary = this.root;
this.root = null;
//iterate the MyQueue elements
while (head != null)
{
//Get current head element of MyQueue
auxiliary = head.element;
if (auxiliary.left != null)
{
//Add new left child node
tail.next = new MyQueue(auxiliary.left);
tail = tail.next;
}
if (auxiliary.right != null)
{
//Add new right child node
tail.next = new MyQueue(auxiliary.right);
tail = tail.next;
}
//unlink left subtree
auxiliary.left = null;
//Remove element of MyQueue
head.element = null;
head = dequeue(head);
Console.Write("\n Delete Node : " + auxiliary.data);
//Deleted tree element
auxiliary.left = null;
auxiliary.right = null;
auxiliary = null;
}
tail = null;
}
}
public static void Main(String[] args)
{
//Make object
BinaryTree obj = new BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/ \
6 8
/ / \
2 7 2
/ \ \
9 -1 9
*/
//Add nodes
obj.root = new Node(10);
obj.root.left = new Node(6);
obj.root.left.left = new Node(2);
obj.root.right = new Node(8);
obj.root.right.right = new Node(2);
obj.root.right.left = new Node(7);
obj.root.left.left.left = new Node(9);
obj.root.right.left.right = new Node(-1);
obj.root.right.right.right = new Node(9);
//Test Process
obj.delete_all_nodes();
}
}
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
<?php
// Php Program
// Delete entire nodes of a binary tree
// Using queue
//Binary Tree node
class Node
{
public $data;
public $left;
public $right;
function __construct($data)
{
//set node value
$this->data = $data;
$this->left = null;
$this->right = null;
}
}
//Define Custom queue
class MyQueue
{
public $element;
public $next;
function __construct($element)
{
$this->element = $element;
$this->next = null;
}
}
class BinaryTree
{
public $root;
function __construct()
{
//set initial tree root to null
$this->root = null;
}
//Recursive function
//Display preorder view of binary tree
public function pre_order($node)
{
if ($node != null)
{
//Print node value
echo " ". $node->data;
$this->pre_order($node->left);
$this->pre_order($node->right);
}
}
//Remove a dequeue element
public function dequeue($head)
{
if ($head != null)
{
$temp = $head->next;
//remove node
$head->element = null;
$head->next = null;
return $temp;
}
return null;
}
// Deleting all nodes in given tree in from top to bottom direction level by level
// Using queue
public function delete_all_nodes()
{
if ($this->root == null)
{
echo "\nEmpty Tree\n";
}
else
{
// Before delete, display tree elements
echo "\n Tree Element \n";
$this->pre_order($this->root);
//Make MyQueue variables
//Add first node into MyQueue
$head = new MyQueue($this->root);
//initial tail is same to head of queue
$tail = $head;
//Get first node of tree
$auxiliary = $this->root;
$this->root = null;
//iterate the MyQueue elements
while ($head != null)
{
//Get current head element of MyQueue
$auxiliary = $head->element;
if ($auxiliary->left != null)
{
//Add new left child node
$tail->next = new MyQueue($auxiliary->left);
$tail = $tail->next;
}
if ($auxiliary->right != null)
{
//Add new right child node
$tail->next = new MyQueue($auxiliary->right);
$tail = $tail->next;
}
//unlink left subtree
$auxiliary->left = null;
//Remove element of MyQueue
$head->element = null;
$head = $this->dequeue($head);
echo "\n Delete Node : ". $auxiliary->data;
//Deleted tree element
$auxiliary->left = null;
$auxiliary->right = null;
$auxiliary = null;
}
$tail = null;
}
}
}
function main()
{
//Make object
$obj = new BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/ \
6 8
/ / \
2 7 2
/ \ \
9 -1 9
*/
//Add nodes
$obj->root = new Node(10);
$obj->root->left = new Node(6);
$obj->root->left->left = new Node(2);
$obj->root->right = new Node(8);
$obj->root->right->right = new Node(2);
$obj->root->right->left = new Node(7);
$obj->root->left->left->left = new Node(9);
$obj->root->right->left->right = new Node(-1);
$obj->root->right->right->right = new Node(9);
//Test Process
$obj->delete_all_nodes();
}
main();
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
// Node Js Program
// Delete entire nodes of a binary tree
// Using queue
//Binary Tree node
class Node
{
constructor(data)
{
//set node value
this.data = data;
this.left = null;
this.right = null;
}
}
//Define Custom queue
class MyQueue
{
constructor(element)
{
this.element = element;
this.next = null;
}
}
class BinaryTree
{
constructor()
{
//set initial tree root to null
this.root = null;
}
//Recursive function
//Display preorder view of binary tree
pre_order(node)
{
if (node != null)
{
//Print node value
process.stdout.write(" " + node.data);
this.pre_order(node.left);
this.pre_order(node.right);
}
}
//Remove a dequeue element
dequeue(head)
{
if (head != null)
{
var temp = head.next;
//remove node
head.element = null;
head.next = null;
return temp;
}
return null;
}
// Deleting all nodes in given tree in from top to bottom direction level by level
// Using queue
delete_all_nodes()
{
if (this.root == null)
{
process.stdout.write("\nEmpty Tree\n");
}
else
{
// Before delete, display tree elements
process.stdout.write("\n Tree Element \n");
this.pre_order(this.root);
//Make MyQueue variables
//Add first node into MyQueue
var head = new MyQueue(this.root);
//initial tail is same to head of queue
var tail = head;
//Get first node of tree
var auxiliary = this.root;
this.root = null;
//iterate the MyQueue elements
while (head != null)
{
//Get current head element of MyQueue
auxiliary = head.element;
if (auxiliary.left != null)
{
//Add new left child node
tail.next = new MyQueue(auxiliary.left);
tail = tail.next;
}
if (auxiliary.right != null)
{
//Add new right child node
tail.next = new MyQueue(auxiliary.right);
tail = tail.next;
}
//unlink left subtree
auxiliary.left = null;
//Remove element of MyQueue
head.element = null;
head = this.dequeue(head);
process.stdout.write("\n Delete Node : " + auxiliary.data);
//Deleted tree element
auxiliary.left = null;
auxiliary.right = null;
auxiliary = null;
}
tail = null;
}
}
}
function main()
{
//Make object
var obj = new BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/ \
6 8
/ / \
2 7 2
/ \ \
9 -1 9
*/
//Add nodes
obj.root = new Node(10);
obj.root.left = new Node(6);
obj.root.left.left = new Node(2);
obj.root.right = new Node(8);
obj.root.right.right = new Node(2);
obj.root.right.left = new Node(7);
obj.root.left.left.left = new Node(9);
obj.root.right.left.right = new Node(-1);
obj.root.right.right.right = new Node(9);
//Test Process
obj.delete_all_nodes();
}
main();
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
# Python 3 Program
# Delete entire nodes of a binary tree
# Using queue
# Binary Tree node
class Node :
def __init__(self, data) :
# set node value
self.data = data
self.left = None
self.right = None
# Define Custom queue
class MyQueue :
def __init__(self, element) :
self.element = element
self.next = None
class BinaryTree :
def __init__(self) :
# set initial tree root to null
self.root = None
# Recursive function
# Display preorder view of binary tree
def pre_order(self, node) :
if (node != None) :
# Print node value
print(" ", node.data, end = "")
self.pre_order(node.left)
self.pre_order(node.right)
# Remove a dequeue element
def dequeue(self, head) :
if (head != None) :
temp = head.next
# remove node
head.element = None
head.next = None
return temp
return None
# Deleting all nodes in given tree in from top to bottom direction level by level
# Using queue
def delete_all_nodes(self) :
if (self.root == None) :
print("\nEmpty Tree\n", end = "")
else :
# Before delete, display tree elements
print("\n Tree Element \n", end = "")
self.pre_order(self.root)
# Make MyQueue variables
# Add first node into MyQueue
head = MyQueue(self.root)
# initial tail is same to head of queue
tail = head
# Get first node of tree
auxiliary = self.root
self.root = None
# iterate the MyQueue elements
while (head != None) :
# Get current head element of MyQueue
auxiliary = head.element
if (auxiliary.left != None) :
# Add new left child node
tail.next = MyQueue(auxiliary.left)
tail = tail.next
if (auxiliary.right != None) :
# Add new right child node
tail.next = MyQueue(auxiliary.right)
tail = tail.next
# unlink left subtree
auxiliary.left = None
# Remove element of MyQueue
head.element = None
head = self.dequeue(head)
print("\n Delete Node : ", auxiliary.data, end = "")
# Deleted tree element
auxiliary.left = None
auxiliary.right = None
auxiliary = None
tail = None
def main() :
# Make object
obj = BinaryTree()
#
# Construct Binary Tree
# -----------------------
# 10
# / \
# 6 8
# / / \
# 2 7 2
# / \ \
# 9 -1 9
#
# Add nodes
obj.root = Node(10)
obj.root.left = Node(6)
obj.root.left.left = Node(2)
obj.root.right = Node(8)
obj.root.right.right = Node(2)
obj.root.right.left = Node(7)
obj.root.left.left.left = Node(9)
obj.root.right.left.right = Node(-1)
obj.root.right.right.right = Node(9)
# Test Process
obj.delete_all_nodes()
if __name__ == "__main__": main()
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
# Ruby Program
# Delete entire nodes of a binary tree
# Using queue
# Binary Tree node
class Node
# Define the accessor and reader of class Node
attr_reader :data, :left, :right
attr_accessor :data, :left, :right
def initialize(data)
# set node value
self.data = data
self.left = nil
self.right = nil
end
end
# Define Custom queue
class MyQueue
# Define the accessor and reader of class MyQueue
attr_reader :element, :next
attr_accessor :element, :next
def initialize(element)
self.element = element
self.next = nil
end
end
class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_reader :root
attr_accessor :root
def initialize()
# set initial tree root to null
self.root = nil
end
# Recursive function
# Display preorder view of binary tree
def pre_order(node)
if (node != nil)
# Print node value
print(" ", node.data)
self.pre_order(node.left)
self.pre_order(node.right)
end
end
# Remove a dequeue element
def dequeue(head)
if (head != nil)
temp = head.next
# remove node
head.element = nil
head.next = nil
return temp
end
return nil
end
# Deleting all nodes in given tree in from top to bottom direction level by level
# Using queue
def delete_all_nodes()
if (self.root == nil)
print("\nEmpty Tree\n")
else
# Before delete, display tree elements
print("\n Tree Element \n")
self.pre_order(self.root)
# Make MyQueue variables
# Add first node into MyQueue
head = MyQueue.new(self.root)
# initial tail is same to head of queue
tail = head
# Get first node of tree
auxiliary = self.root
self.root = nil
# iterate the MyQueue elements
while (head != nil)
# Get current head element of MyQueue
auxiliary = head.element
if (auxiliary.left != nil)
# Add new left child node
tail.next = MyQueue.new(auxiliary.left)
tail = tail.next
end
if (auxiliary.right != nil)
# Add new right child node
tail.next = MyQueue.new(auxiliary.right)
tail = tail.next
end
# unlink left subtree
auxiliary.left = nil
# Remove element of MyQueue
head.element = nil
head = self.dequeue(head)
print("\n Delete Node : ", auxiliary.data)
# Deleted tree element
auxiliary.left = nil
auxiliary.right = nil
auxiliary = nil
end
tail = nil
end
end
end
def main()
# Make object
obj = BinaryTree.new()
#
# Construct Binary Tree
# -----------------------
# 10
# / \
# 6 8
# / / \
# 2 7 2
# / \ \
# 9 -1 9
#
# Add nodes
obj.root = Node.new(10)
obj.root.left = Node.new(6)
obj.root.left.left = Node.new(2)
obj.root.right = Node.new(8)
obj.root.right.right = Node.new(2)
obj.root.right.left = Node.new(7)
obj.root.left.left.left = Node.new(9)
obj.root.right.left.right = Node.new(-1)
obj.root.right.right.right = Node.new(9)
# Test Process
obj.delete_all_nodes()
end
main()
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
// Scala Program
// Delete entire nodes of a binary tree
// Using queue
//Binary Tree node
class Node(var data: Int,
var left: Node,
var right: Node)
{
def this(data: Int)
{
this(data, null, null);
}
}
//Define Custom queue
class MyQueue(var element: Node,
var next: MyQueue)
{
def this(element: Node)
{
this(element, null);
}
}
class BinaryTree(var root: Node)
{
def this()
{
this(null);
}
//Recursive function
//Display preorder view of binary tree
def pre_order(node: Node): Unit = {
if (node != null)
{
//Print node value
print(" " + node.data);
pre_order(node.left);
pre_order(node.right);
}
}
//Remove a dequeue element
def dequeue(head: MyQueue): MyQueue = {
if (head != null)
{
var temp: MyQueue = head.next;
//remove node
head.element = null;
head.next = null;
return temp;
}
return null;
}
// Deleting all nodes in given tree in from top to bottom direction level by level
// Using queue
def delete_all_nodes(): Unit = {
if (this.root == null)
{
print("\nEmpty Tree\n");
}
else
{
// Before delete, display tree elements
print("\n Tree Element \n");
pre_order(this.root);
//Make MyQueue variables
//Add first node into MyQueue
var head: MyQueue = new MyQueue(this.root);
//initial tail is same to head of queue
var tail: MyQueue = head;
//Get first node of tree
var auxiliary: Node = this.root;
this.root = null;
//iterate the MyQueue elements
while (head != null)
{
//Get current head element of MyQueue
auxiliary = head.element;
if (auxiliary.left != null)
{
//Add new left child node
tail.next = new MyQueue(auxiliary.left);
tail = tail.next;
}
if (auxiliary.right != null)
{
//Add new right child node
tail.next = new MyQueue(auxiliary.right);
tail = tail.next;
}
//unlink left subtree
auxiliary.left = null;
//Remove element of MyQueue
head.element = null;
head = dequeue(head);
print("\n Delete Node : " + auxiliary.data);
//Deleted tree element
auxiliary.left = null;
auxiliary.right = null;
auxiliary = null;
}
tail = null;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
//Make object
var obj: BinaryTree = new BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/ \
6 8
/ / \
2 7 2
/ \ \
9 -1 9
*/
//Add nodes
obj.root = new Node(10);
obj.root.left = new Node(6);
obj.root.left.left = new Node(2);
obj.root.right = new Node(8);
obj.root.right.right = new Node(2);
obj.root.right.left = new Node(7);
obj.root.left.left.left = new Node(9);
obj.root.right.left.right = new Node(-1);
obj.root.right.right.right = new Node(9);
//Test Process
obj.delete_all_nodes();
}
}
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
// Swift 4 Program
// Delete entire nodes of a binary tree
// Using queue
//Binary Tree node
class Node
{
var data: Int;
var left: Node? ;
var right: Node? ;
init(_ data: Int)
{
//set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
//Define Custom queue
class MyQueue
{
var element: Node? ;
var next: MyQueue? ;
init(_ element: Node? )
{
self.element = element;
self.next = nil;
}
}
class BinaryTree
{
var root: Node? ;
init()
{
//set initial tree root to null
self.root = nil;
}
//Recursive function
//Display preorder view of binary tree
func pre_order(_ node: Node? )
{
if (node != nil)
{
//Print node value
print(" ", node!.data, terminator: "");
self.pre_order(node!.left);
self.pre_order(node!.right);
}
}
//Remove a dequeue element
func dequeue(_ head: MyQueue? ) -> MyQueue?
{
if (head != nil)
{
let temp: MyQueue? = head!.next;
//remove node
head!.element = nil;
head!.next = nil;
return temp;
}
return nil;
}
// Deleting all nodes in given tree in from top to bottom direction level by level
// Using queue
func delete_all_nodes()
{
if (self.root == nil)
{
print("\nEmpty Tree\n", terminator: "");
}
else
{
// Before delete, display tree elements
print("\n Tree Element \n", terminator: "");
self.pre_order(self.root);
//Make MyQueue variables
//Add first node into MyQueue
var head: MyQueue? = MyQueue(self.root);
//initial tail is same to head of queue
var tail: MyQueue? = head;
//Get first node of tree
var auxiliary: Node? = self.root;
self.root = nil;
//iterate the MyQueue elements
while (head != nil)
{
//Get current head element of MyQueue
auxiliary = head!.element;
if (auxiliary!.left != nil)
{
//Add new left child node
tail!.next = MyQueue(auxiliary!.left);
tail = tail!.next;
}
if (auxiliary!.right != nil)
{
//Add new right child node
tail!.next = MyQueue(auxiliary!.right);
tail = tail!.next;
}
//unlink left subtree
auxiliary!.left = nil;
//Remove element of MyQueue
head!.element = nil;
head = self.dequeue(head);
print("\n Delete Node : ", auxiliary!.data, terminator: "");
//Deleted tree element
auxiliary!.left = nil;
auxiliary!.right = nil;
auxiliary = nil;
}
tail = nil;
}
}
}
func main()
{
//Make object
let obj: BinaryTree = BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/ \
6 8
/ / \
2 7 2
/ \ \
9 -1 9
*/
//Add nodes
obj.root = Node(10);
obj.root!.left = Node(6);
obj.root!.left!.left = Node(2);
obj.root!.right = Node(8);
obj.root!.right!.right = Node(2);
obj.root!.right!.left = Node(7);
obj.root!.left!.left!.left = Node(9);
obj.root!.right!.left!.right = Node(-1);
obj.root!.right!.right!.right = Node(9);
//Test Process
obj.delete_all_nodes();
}
main();
Output
Tree Element
10 6 2 9 8 7 -1 2 9
Delete Node : 10
Delete Node : 6
Delete Node : 8
Delete Node : 2
Delete Node : 7
Delete Node : 2
Delete Node : 9
Delete Node : -1
Delete Node : 9
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment