×

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

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 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.

Quick Sort

Quicksort is a sorting algorithm that uses a divide-and-conquer strategy. A pivot element is used to divide an array into subarrays (element selected from the array).  The pivot element should be...

4 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 minutes read.

Given a Generate all Structurally Unique Binary Search Trees

Implementation // Creating a C++ program that will help us build all the binary search trees for the keys from 1 to n.  #include <bits/stdc++.h> using namespace std; // creating a structure that will...

8 minutes read.

Burning binary tree

Burn the Binary tree starting from the target node You have given a binary tree and a target node value. Now you have to burn the tree from target node. You...

4 minutes read.

Perfect Binary Tree

Complete binary trees are an important and general topic in the concept – of tree data structures. Before discussing a complete binary tree, we need to know the concept of...

4 minutes read.

Minimum Spanning Tree

Before getting to know about the minimum spanning tree, we should first discuss about what is a spanning tree. A spanning tree is basically a sub or minimized graph that...

7 minutes read.

2-3 Trees and Basic Operations on them

2-3 Trees, like any other AVL trees or B-trees, are just a type of Height Balanced Tree. 2-3 Trees are the B-trees of order 3. Like every other B-tree, the...

4 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.

Object-Oriented Analysis and Design

While designing a system, one should know all the requirements or needs of the plan beforehand, and to do so, we should use a systematic approach to analyze the goal...

3 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.