×

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

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

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

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

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

4 minutes read.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

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.

Given a Binary Tree, Print the Pre-order Traversal in Recursive

Implementation #include <stdio.h> #include <stdlib.h>   /* Creating a binary tree node that consists of some data along with the pointer to the left and right child.  */ struct __nod {     int record;     struct...

4 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Implementation of Queue

Implementation of queue: We can implement the queue through the array and linked list. An array is the easiest way to implement the queue. When a queue is created with the...

7 minutes read.

A Full Binary Tree with n Nodes

Implementation // Writing the implementation of the above approach in C++ #include <bits/stdc++.h> using namespace std; // We are creating a class that will create a node and its left and right children.  struct __nod...

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

Insertion in B+ Tree

We will learn how to insert a node in the B+ tree and what are the different properties we are going to follow. Except for the root node, every node should...

5 minutes read.

Applications of trees in data structures

Data structures Storage used to organize and store data is known as the data structure. It is a method of managing computerized data to translate or retrieve it more efficiently. A...

7 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

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.

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end...

3 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.