Deletion in Binary Search Tree - javatpoint
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
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
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.
← 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.
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
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
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.
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:
free(cur);
}
else if (cur->left && cur->right)
{
Node* succ = findMinimum(cur- >right);
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);
}
}
← 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
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