0% found this document useful (0 votes)
7 views

Deletion in Binary Search Tree - javatpoint

The document explains the process of deleting nodes from a binary search tree (BST) while maintaining its properties. It outlines three scenarios for deletion: when the node is a leaf, has one child, or has two children, detailing the steps for each case. Additionally, it provides an algorithm and function for performing the deletion operation in a BST.

Uploaded by

Bhagya Lakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Deletion in Binary Search Tree - javatpoint

The document explains the process of deleting nodes from a binary search tree (BST) while maintaining its properties. It outlines three scenarios for deletion: when the node is a leaf, has one child, or has two children, detailing the steps for each case. Additionally, it provides an algorithm and function for performing the deletion operation in a BST.

Uploaded by

Bhagya Lakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement 

DS Tutorial

DS Tutorial

DS Introduction

DS Algorithm

Asymptotic Analysis

DS Pointer

DS Structure

DS Array

DS Array

2D Array

DS Linked List

Linked List

Types of Linked List

Singly Linked List

Doubly Linked List

Circular Linked List

Circular Doubly List

Skip list in DS

https://www.tpointtech.com/deletion-in-binary-search-tree 1/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

DS Stack
Advertisement

DS Stack

Array Implementation

Linked List Implementation

Javatpoint.com is now
TpointTech.com
Javatpoint.com is now changed to TpointTech.com, so we
request you to subscribe our newsletter for further updates.

Your Email Subscribe 

← prev next →

Deletion
Delete function is used to delete the specified node from a binary search tree. However,
we must delete a node from a binary search tree in such a way, that the property of
binary search tree doesn't violate. There are three situations of deleting a node from
binary search tree.

The node to be deleted is a leaf node


It is the simplest case, in this case, replace the leaf node with the NULL and simple free
the allocated space.

In the following image, we are deleting the node 85, since the node is a leaf node,
therefore the node will be replaced with NULL and allocated space will be freed.

https://www.tpointtech.com/deletion-in-binary-search-tree 2/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

The node to be deleted has only one child.


In this case, replace the node with its child and delete the child node, which now
contains the value which is to be deleted. Simply replace it with the NULL and free the
allocated space.

In the following image, the node 12 is to be deleted. It has only one child. The node will
be replaced with its child node and the replaced node 12 (which is now leaf node) will
simply be deleted.

https://www.tpointtech.com/deletion-in-binary-search-tree 3/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

The node to be deleted has two children.


It is a bit complexed case compare to other two cases. However, the node which is to be
deleted, is replaced with its in-order successor or predecessor recursively until the node
value (to be deleted) is placed on the leaf of the tree. After the procedure, replace the
node with NULL and free the allocated space.

In the following image, the node 50 is to be deleted which is the root node of the tree.
The in-order traversal of the tree given below.

6, 25, 30, 50, 52, 60, 70, 75.

replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree,
which will simply be deleted.

https://www.tpointtech.com/deletion-in-binary-search-tree 4/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

Algorithm
Delete (TREE, ITEM)

Home Python Java JavaScript HTML SQL PHP
Step 1: IF TREE = NULL
Write "item not found in the tree" ELSE IF ITEM < TREE -> DATA
Delete(TREE->LEFT, ITEM)
ELSE IF ITEM > TREE -> DATA
Delete(TREE -> RIGHT, ITEM)
ELSE IF TREE -> LEFT AND TREE -> RIGHT
SET TEMP = findLargestNode(TREE -> LEFT)
SET TREE -> DATA = TEMP -> DATA
Delete(TREE -> LEFT, TEMP -> DATA)
ELSE
SET TEMP = TREE
IF TREE -> LEFT = NULL AND TREE -> RIGHT = NULL
SET TREE = NULL
ELSE IF TREE -> LEFT != NULL
SET TREE = TREE -> LEFT
ELSE
SET TREE = TREE -> RIGHT
[END OF IF]
FREE TEMP
[END OF IF]

Step 2: END

Function:

void deletion(Node*& root, int item)


{
Node* parent = NULL;
https://www.tpointtech.com/deletion-in-binary-search-tree 5/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Node* cur = root;


Advertisement

search(cur, item, parent);


if (cur == NULL)
return;

if (cur->left == NULL && cur->right == NULL)


{
if (cur != root)
{
if (parent->left == cur)
parent->left = NULL;
else
parent->right = NULL;
}
else
root = NULL;

free(cur);
}
else if (cur->left && cur->right)
{
Node* succ = findMinimum(cur- >right);

int val = succ->data;

deletion(root, succ->data);

cur->data = val;
}

else
{
Node* child = (cur->left)? Cur- >left: cur->right;

if (cur != root)
{
if (cur == parent->left)
parent->left = child;
else
https://www.tpointtech.com/deletion-in-binary-search-tree 6/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

parent->right = child;
} Advertisement

else
root = child;
free(cur);
}
}

Node* findMinimum(Node* cur)


{
while(cur->left != NULL) {
cur = cur->left;
}
return cur;
}

Next Topic Doubly Linked List

← prev next →

https://www.tpointtech.com/deletion-in-binary-search-tree 7/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

Related Posts

Searching in BST
Searching Searching means finding or locating some specific element or node within a
data structure. However, searching for some specific node in binary search tree is pretty
easy due to the fact that, element in BST are stored in a particular order. Compare the
element with...
 1 min read

Insertion in BST
Insertion Insert function is used to add a new element in a binary search tree at
appropriate location. Insert function is to be designed in such a way that, it must node

https://www.tpointtech.com/deletion-in-binary-search-tree 8/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

violate the property of binary search tree at each value. Allocate the memory for...
Advertisement
 2 min read

Learn Important Tutorial

Python Java

Javascript HTML

Database PHP

C++ React

B.Tech / MCA

https://www.tpointtech.com/deletion-in-binary-search-tree 9/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

DBMS Data
Structures

Operating
DAA
System

Computer Compiler
Network Design

Computer Discrete
Organization Mathematics

Ethical Computer
Hacking Graphics

Web Software
Technology Engineering

https://www.tpointtech.com/deletion-in-binary-search-tree 10/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

Cyber
Automata
Security

C
C++
Programming

Java .Net

Python Programs

Control Data
System Warehouse

Preparation

https://www.tpointtech.com/deletion-in-binary-search-tree 11/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

Aptitude Reasoning

Verbal Interview
Ability Questions

Company
Questions
Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 12/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 13/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 14/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 15/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 16/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 17/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 18/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 19/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 20/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 21/22
2/14/25, 11:28 AM Deletion in Binary Search Tree - javatpoint

Advertisement

https://www.tpointtech.com/deletion-in-binary-search-tree 22/22

You might also like