Remove all leaf nodes from the binary search tree
Last Updated :
28 Nov, 2022
We have given a binary search tree and we want to delete the leaf nodes from the binary search tree.
Examples:
Input : 20 10 5 15 30 25 35
Output : Inorder before Deleting the leaf node
5 10 15 20 25 30 35
Inorder after Deleting the leaf node
10 20 30
This is the binary search tree where we
want to delete the leaf node.
20
/ \
10 30
/ \ / \
5 15 25 35
After deleting the leaf node the binary
search tree looks like
20
/ \
10 30
We traverse given Binary Search Tree in inorder way. During traversal, we check if current node is leaf, if yes, we delete it. Else we recur for left and right children. An important thing to remember is, we must assign new left and right children if there is any modification in roots of subtrees.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode( int data)
{
struct Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
struct Node* insert( struct Node* root, int data)
{
if (root == NULL)
return newNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
}
void inorder( struct Node* root)
{
if (root != NULL) {
inorder(root->left);
cout << root->data << " " ;
inorder(root->right);
}
}
struct Node* leafDelete( struct Node* root)
{
if (root == NULL)
return NULL;
if (root->left == NULL && root->right == NULL) {
free (root);
return NULL;
}
root->left = leafDelete(root->left);
root->right = leafDelete(root->right);
return root;
}
int main()
{
struct Node* root = NULL;
root = insert(root, 20);
insert(root, 10);
insert(root, 5);
insert(root, 15);
insert(root, 30);
insert(root, 25);
insert(root, 35);
cout << "Inorder before Deleting the leaf Node." << endl;
inorder(root);
cout << endl;
leafDelete(root);
cout << "INorder after Deleting the leaf Node." << endl;
inorder(root);
return 0;
}
|
Java
class GfG {
static class Node {
int data;
Node left;
Node right;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
static Node insert(Node root, int data)
{
if (root == null )
return newNode(data);
if (data < root.data)
root.left = insert(root.left, data);
else if (data > root.data)
root.right = insert(root.right, data);
return root;
}
static void inorder(Node root)
{
if (root != null ) {
inorder(root.left);
System.out.print(root.data + " " );
inorder(root.right);
}
}
static Node leafDelete(Node root)
{
if (root == null ) {
return null ;
}
if (root.left == null && root.right == null ) {
return null ;
}
root.left = leafDelete(root.left);
root.right = leafDelete(root.right);
return root;
}
public static void main(String[] args)
{
Node root = null ;
root = insert(root, 20 );
insert(root, 10 );
insert(root, 5 );
insert(root, 15 );
insert(root, 30 );
insert(root, 25 );
insert(root, 35 );
System.out.println( "Inorder before Deleting the leaf Node. " );
inorder(root);
System.out.println();
leafDelete(root);
System.out.println( "INorder after Deleting the leaf Node. " );
inorder(root);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def insert(root, data):
if root = = None :
return newNode(data)
if data < root.data:
root.left = insert(root.left, data)
else if data > root.data:
root.right = insert(root.right, data)
return root
def inorder(root):
if root ! = None :
inorder(root.left)
print (root.data, end = " " )
inorder(root.right)
def leafDelete(root):
if root = = None :
return None
if root.left = = None and root.right = = None :
return None
root.left = leafDelete(root.left)
root.right = leafDelete(root.right)
return root
if __name__ = = '__main__' :
root = None
root = insert(root, 20 )
insert(root, 10 )
insert(root, 5 )
insert(root, 15 )
insert(root, 30 )
insert(root, 25 )
insert(root, 35 )
print ( "Inorder before Deleting the leaf Node." )
inorder(root)
leafDelete(root)
print ()
print ( "INorder after Deleting the leaf Node." )
inorder(root)
|
C#
using System;
class GfG {
class Node {
public int data;
public Node left;
public Node right;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
static Node insert(Node root, int data)
{
if (root == null )
return newNode(data);
if (data < root.data)
root.left = insert(root.left, data);
else if (data > root.data)
root.right = insert(root.right, data);
return root;
}
static void inorder(Node root)
{
if (root != null ) {
inorder(root.left);
Console.Write(root.data + " " );
inorder(root.right);
}
}
static Node leafDelete(Node root)
{
if (root == null ) {
return null ;
}
if (root.left == null && root.right == null ) {
return null ;
}
root.left = leafDelete(root.left);
root.right = leafDelete(root.right);
return root;
}
public static void Main(String[] args)
{
Node root = null ;
root = insert(root, 20);
insert(root, 10);
insert(root, 5);
insert(root, 15);
insert(root, 30);
insert(root, 25);
insert(root, 35);
Console.WriteLine( "Inorder before Deleting"
+ "the leaf Node. " );
inorder(root);
Console.WriteLine();
leafDelete(root);
Console.WriteLine( "INorder after Deleting"
+ "the leaf Node. " );
inorder(root);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .left = null ;
this .right = null ;
}
}
function newNode(data) {
var temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
function insert(root , data) {
if (root == null )
return newNode(data);
if (data < root.data)
root.left = insert(root.left, data);
else if (data > root.data)
root.right = insert(root.right, data);
return root;
}
function inorder(root) {
if (root != null ) {
inorder(root.left);
document.write(root.data + " " );
inorder(root.right);
}
}
function leafDelete(root) {
if (root == null ) {
return null ;
}
if (root.left == null && root.right == null ) {
return null ;
}
root.left = leafDelete(root.left);
root.right = leafDelete(root.right);
return root;
}
var root = null ;
root = insert(root, 20);
insert(root, 10);
insert(root, 5);
insert(root, 15);
insert(root, 30);
insert(root, 25);
insert(root, 35);
document.write(
"Inorder before Deleting the leaf Node. <br/>"
);
inorder(root);
document.write( "<br/>" );
leafDelete(root);
document.write(
"INorder after Deleting the leaf Node. <br/>"
);
inorder(root);
</script>
|
Output
Inorder before Deleting the leaf Node.
5 10 15 20 25 30 35
INorder after Deleting the leaf Node.
10 20 30
Time Complexity: O(n), As we are visiting every node just once. Here n is the number of nodes in the tree.
Auxiliary Space: O(h), Here h is the height of the tree and the extra space is used in the recursion call stack.
Share your thoughts in the comments
Please Login to comment...