×

Function to Delete a Leaf Node from a Binary Tree

Implementation

// We are writing a C++ code to eliminate all the leaves from the given value. 
#include <bits/stdc++.h>
using namespace std;


// creating a new binary tree node
struct __nod {
	int record;
	struct __nod *Lft, *Rt;
};


// creating a utility function that will allot us a new node for the binary tree
struct __nod* nw__nod(int record)
{
	struct __nod* nw__nod = nw __nod;
	nw__nod->record = record;
	nw__nod->Lft = nw__nod->Rt = NILL;
	return (nw__nod);
}


__nod* deleteLeaves(__nod* root, int x)
{	
	if (root == NILL)
		return NILLptr;
	root->Lft = deleteLeaves(root->Lft, x);
	root->Rt = deleteLeaves(root->Rt, x);


	if (root->record == x && root->Lft == NILL &&
						root->Rt == NILL) {
		
		return NILLptr;
	}
	return root;
}


void inorder(__nod* root)
{
	if (root == NILL)
		return;
	inorder(root->Lft);
	cout << root->record << " ";
	inorder(root->Rt);
}


// writing the main program to test the above functions
int main(void)
{
	struct __nod* root = nw__nod(10);
	root->Lft = nw__nod(3);
	root->Rt = nw__nod(10);
	root->Lft->Lft = nw__nod(3);
	root->Lft->Rt = nw__nod(1);
	root->Rt->Rt = nw__nod(3);
	root->Rt->Rt->Lft = nw__nod(3);
	root->Rt->Rt->Rt = nw__nod(3);
	deleteLeaves(root, 3);
	cout << "Inorder traversal after deletion : ";
	inorder(root);
	return 0;
}

Output:

Function to Delete a Leaf Node from a Binary Tree

Example 2)

// We are writing a C# code to eliminate all the leaves from the given value. 
using System;
	
class TFT
{
// creating a new binary tree node
	class __nod
	{
		public int record;
		public __nod Lft, Rt;
	}
// creating a utility function that will allot us a new node for the binary tree
	static __nod nw__nod(int record)
	{
		__nod nw__nod = nw __nod();
		nw__nod.record = record;
		nw__nod.Lft = NILL;
		nw__nod.Rt = NILL;
		return (nw__nod);
	}


	static __nod deleteLeaves(__nod root, int x)
	{
		if (root == NILL)
			return NILL;
		root.Lft = deleteLeaves(root.Lft, x);
		root.Rt = deleteLeaves(root.Rt, x);


		if (root.record == x &&
			root.Lft == NILL &&
			root.Rt == NILL)
		{


			return NILL;
		}
		return root;
	}


	static void inorder(__nod root)
	{
		if (root == NILL)
			return;
		inorder(root.Lft);
		Console.Write(root.record + " ");
		inorder(root.Rt);
	}
// writing the main program to test the above functions
	public static void Main(String[] args)
	{
		__nod root = nw__nod(10);
		root.Lft = nw__nod(3);
		root.Rt = nw__nod(10);
		root.Lft.Lft = nw__nod(3);
		root.Lft.Rt = nw__nod(1);
		root.Rt.Rt = nw__nod(3);
		root.Rt.Rt.Lft = nw__nod(3);
		root.Rt.Rt.Rt = nw__nod(3);
		deleteLeaves(root, 3);
		Console.Write("Inorder traversal after deletion : ");
		inorder(root);
	}
}

Output:

Function to Delete a Leaf Node from a Binary Tree

Example 3)

// We are writing a Java code to eliminate all the leaves from the given value. 
class TFT {
// creating a new binary tree node
static class __nod {
	int record;
	__nod Lft, Rt;
}
// creating a utility function that will allot us a new node for the binary tree
static __nod nw__nod(int record)
{
	__nod nw__nod = nw __nod();
	nw__nod.record = record;
	nw__nod.Lft = NILL;
	nw__nod.Rt = NILL;
	return (nw__nod);
}


static __nod deleteLeaves(__nod root, int x)
{
	if (root == NILL)
		return NILL;
	root.Lft = deleteLeaves(root.Lft, x);
	root.Rt = deleteLeaves(root.Rt, x);


	if (root.record == x && root.Lft == NILL && root.Rt == NILL) {


		return NILL;
	}
	return root;
}


static void inorder(__nod root)
{
	if (root == NILL)
		return;
	inorder(root.Lft);
	System.out.print(root.record + " ");
	inorder(root.Rt);
}
// writing the main program to test the above functions
public static void main(String[] args)
{
	__nod root = nw__nod(10);
	root.Lft = nw__nod(3);
	root.Rt = nw__nod(10);
	root.Lft.Lft = nw__nod(3);
	root.Lft.Rt = nw__nod(1);
	root.Rt.Rt = nw__nod(3);
	root.Rt.Rt.Lft = nw__nod(3);
	root.Rt.Rt.Rt = nw__nod(3);
	deleteLeaves(root, 3);
	System.out.print("Inorder traversal after deletion : ");
	inorder(root);
}
}

Output:

Function to Delete a Leaf Node from a Binary Tree

Example 4)

<script>
// We are writing a Javascript code to eliminate all the leaves from the given value. 
	class __nod
	{
		constructor(record) {
		this.Lft = NILL;
		this.Rt = NILL;
		this.record = record;
		}
	}
// creating a new binary tree node
// creating a utility function that will allot us a new node for the binary tree
	function nw__nod(record)
	{
		let nw__nod = nw __nod(record);
		return (nw__nod);
	}


	function deleteLeaves(root, x)
	{
		if (root == NILL)
			return NILL;
		root.Lft = deleteLeaves(root.Lft, x);
		root.Rt = deleteLeaves(root.Rt, x);


		if (root.record == x && root.Lft == NILL && root.Rt == NILL)
		{


			return NILL;
		}
		return root;
	}


	function inorder(root)
	{
		if (root == NILL)
			return;
		inorder(root.Lft);
		document.write(root.record + " ");
		inorder(root.Rt);
	}
	
	let root = nw__nod(10);
	root.Lft = nw__nod(3);
	root.Rt = nw__nod(10);
	root.Lft.Lft = nw__nod(3);
	root.Lft.Rt = nw__nod(1);
	root.Rt.Rt = nw__nod(3);
	root.Rt.Rt.Lft = nw__nod(3);
	root.Rt.Rt.Rt = nw__nod(3);
	deleteLeaves(root, 3);
	document.write("Inorder traversal after deletion : ");
	inorder(root);


</script>

Output:

Function to Delete a Leaf Node from a Binary Tree

Example 6)

# We are writing a Python code to eliminate all the leaves from the given value. 
#  creating a utility class that will allot us a new node for the binary tree
class nw__nod:
	def __init__(self, record):
		self.record = record
		self.Lft = self.Rt = None


def deleteLeaves(root, x):
	if (root == None):
		return None
	root.Lft = deleteLeaves(root.Lft, x)
	root.Rt = deleteLeaves(root.Rt, x)


	if (root.record == x and
		root.Lft == None and
		root.Rt == None):
		return None
	return root


def inorder(root):
	if (root == None):
		return
	inorder(root.Lft)
	print(root.record, end = " ")
	inorder(root.Rt)


# writing the main program to test the above functions
if __name__ == '__main__':
	root = nw__nod(10)
	root.Lft = nw__nod(3)
	root.Rt = nw__nod(10)
	root.Lft.Lft = nw__nod(3)
	root.Lft.Rt = nw__nod(1)
	root.Rt.Rt = nw__nod(3)
	root.Rt.Rt.Lft = nw__nod(3)
	root.Rt.Rt.Rt = nw__nod(3)
	deleteLeaves(root, 3)
	print("Inorder traversal after deletion : ")
	inorder(root)

Output:

Function to Delete a Leaf Node from a Binary Tree

Related Topics

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Threaded Binary Tree

The linked form of binary trees wastes storage capacity because more than half of the connection variables have a Missing value. A binary tree has several nodes. Hence n+1 link fields...

8 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

Introduction to 2D-Arrays

Two Dimensional Array Technical Definitions An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table...

3 minutes read.

Delete a Node without head pointer from the linked list

Delete a Node without head pointer from the linked list This article will explain how to delete a node without a head pointer from the linked list. We have given a...

2 minutes read.

Advantages and Disadvantages of Linked List

Advantages of Linked List The linked list is a dynamic data structure.You can also decrease and increase the linked list at run-time. That is, you can allocate and deallocate memory at...

3 minutes read.

Semi-Structured data

In this article, we will discuss the semi-structured data. Data can be defined as the distinct piece of information that is gathered and translated for some purpose. It can be...

5 minutes read.

Intersection Point in Y Shaped Linked Lists in Java

Intersection Point in Y Shaped Linked Lists in Java In this article, we are going to see how to find the intersection point in a Y-shaped linked list. Method 1: We need to...

4 minutes read.

Red-black Tree in Data Structures?

A type of binary tree which is known as the Red-Black tree, is a specialized and unique tree. What is the urgency or, to be more precise, the necessity of...

10 minutes read.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Strings in Data Structures

Strings and functions in C A string is a collection of characters. We'll learn how to declare strings, operate with strings in C programming, and use pre-defined string handling routines. We'll look...

7 minutes read.

Winner tree in Data Structures

Tree Data structure A tree is a hierarchical and non-linear data structure with nodes. Each node in the Tree contains a message value and stores the name passed to another ("child")...

6 minutes read.

Tree vs Graph: Data Structure

Difference Between Tree and Graph What is Tree? A tree is a non-linear data structure and finite collection of elements called node. A tree, in which the data items are arranged in...

3 minutes read.

Operations of B++ tree

Insertion When we discuss the insertion operation in the B++ tree, this operation helps us in pushing a new element in the tree at any given place. In this case, the...

17 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.

Top view of binary tree

We know that a binary tree is a kind of tree that helps us organize our tree and that it is a kind of non-linear info structure that at least...

4 minutes read.

Berkley’s Algorithm

Berkley’s Algorithm is mainly used in clock synchronization system. It is used in distributed systems. To implement this algorithm, we have to think that the network has no accurate time...

4 minutes read.