## How to delete a Node in a Binary Search Tree

**Recommended to visit before going through this article:**

**What is a Binary Search Tree? How to represent it ? And, related terminologies…**

**How to insert a node in a Binary Search Tree ?**

**How to search a node in a Binary Search Tree ?**

Deleting a node in a given Binary Search Tree is a process to delete any existing node; let’s say if **node A** has to be deleted then you got to follow below steps –

**STEP 1:** **If there is no node in a given BST** then return saying **node A** can not be deleted as there is no node in the BST.

**STEP 2:** Find **Node A** in a given Binary Search Tree which we need to delete. To find so, just compare the value of **node A** with the root node’s value:

**if node A has a value greater than the root node’s value** – traverse down the root node in its right node and Go to Step 2 by considering this right node as the root node (Note: if there is no right node straight go to Step 3)

**if node A has a value lesser than the root node’s value** – traverse down the root node in its left node and Go to Step 2 by considering this left node as the root node (Note: if there is no left node straight go to Step 3)

**if node A has a equal to the root node’s value** – it just means you have found the node which is to be deleted from the tree – **You got to delete this node and to do so just Go to Step 4**

**STEP 3:** Just return saying **node A** can not be deleted as it is not present in the BST.

**STEP 4:** Once the Node to be deleted is found using step 2: three cases may arise –

**case 1: this node has no children [ in this case – just assign null to the parent of this node – You are done deleting the node ]**

**case 2: this node has only one Child** **[ in this case – just assign this node’s right child or left child reference whichever it has to the parent of this node – You are done deleting the node ]**

**case 3: this node has both the children** **[ In this case – just replace this node with its in order successor node followed by deleting in order successor from its original position in the BST ]**

**Above Algorithm can be implemented using two popular ways – Recursive and an Iterative way**

**BST,java**

**Node.java**

package org.gontuseries.bst; public class Node { Node(int value) { this.value = value; this.leftNode = null; this.rightNode = null; } private int value; private Node leftNode; private Node rightNode; // --- writing all getters and setters for all properties //i.e for value, leftNode, rightNode }

**Time Complexity:** The run time complexity of delete operation using Recursive way is: O(height of a Binary Search Tree) **i.e O(h) [worst-case]**

a) In case of a skewed Binary Search Tree the height is equal to the number of nodes in it; so, it becomes O(**n)[worst-case]**

b) In case of a Binary Search Tree built using some Tree Balancing Techniques like AVL, RED Black etc the height is equal to log (number of nodes in it); so it becomes **log(n) [worst-case]**

where, ‘n’ is the number of nodes in a binary search tree.