### Deleting a node from a BST --- Part 1 (easy cases)

• Warning.....

• Fact:

• Deleting a node from a Binary Search Tree is very complicated because:

 The resulting tree after the deletion must also be a Binary Search Tree

• The best known algorithm used to delete a node from a Binary Search Tree is called:

• Overview: Hibbard delete algorithm

• Hibbard distinguish 3 cases in the deletion method:

1. The deletion node has no subtree (i.e., the deletion node is a leaf node)

 This case is very easy to handle --- we can simply delete the node

2. The deletion node has only one subtree

 This case is pretty easy to handle --- we chort-circuit the BST at the deletion node (see example below)

3. The deletion node has two subtrees

 This case is very tough to handle ....

• Case 1: deletion node has no subtree (i.e., is a leaf node)

• Deleting a node that has no subtree (= a leaf node) can be done easily as follows:

• Delete the leaf node by making the link variable in the parent node equal to null

Note:

 Depending on whether the deletion node is the left child or the right child, you must update the left or the right in the parent node !

Example 1: if the deletion node is a left child of its parent node

• Delete the node 3 in the following BST: • After setting left = null in parent node, we have: Example 2: if the deletion node is a right child of its parent node

• Delete the node 18 in the following BST: • After setting right = null in parent node, we have: Special situation: the deletion node is the root node

• If the delete node is the root node....

Note: if the root node has no subtree, then the root node is the only node in the tree: • After deleting the root node, we have an empty tree: • Case 1: algorithm to delete a node with no subtree

• Java code used to detect and remove a node with no subtree:

 ``` // p points to the node that you want to remove if ( p.left == null && p.right == null ) // Case 0: p has no subtrees { /* ======================================================= Special case: p is the root node If the root has no subtrees, then it's the ONLY node ! Removing the last node will result in an EMPTY tree ! ======================================================== */ if ( p == root ) { root = null; return; } parent = myParent; // myParent was set by findNode(x).... /* -------------------------------- Delete p from p's parent -------------------------------- */ if ( parent.left == p ) parent.left = null; else parent.right = null; return; } ```

• Case 2: deletion node has one subtree

• Deleting a node that has one subtree can be done easily as follows:

• Delete the node by short-circuiting the BST at the deletion node

I.e.: make the parent node of the deletion node point to the subtree of the deletion node

 The resulting tree is a Binary Search Tree !!!

Example 1: if the deletion node only has a left subtree

• Delete the node 5 in the following BST: • After linking subtree to the parent node: Example 2: if the deletion node only has a right subtree

• Delete the node 18 in the following BST: • After linking subtree to the parent node: Special situation: the deletion node is the root node

• If the root node has only one subtree: • We must update the head variable to point to a new root node (which is the subtree of the root node): • Case 2: algorithm to delete a node with no subtree

• Java code used to detect and remove a node with one subtree:

 ``` // p points to the node that you want to remove /* ================================================== Case 1a: p has a right subtree ================================================== */ if ( p.left == null ) // If true, p has a right subtree { /* ======================================== Handle special situation: p is the root ========================================= */ if ( p == root ) { root = root.right; // After root is deleted, BST = right tree of root return; } parent = myParent; // myParent was set by findNode(x).... /* ---------------------------------------------- Link p's RIGHT subtree to the parent node ---------------------------------------------- */ if ( parent.left == p ) { // Hang p's RIGHT tree to parent.left parent.left = p.right; // Link p's right subtree to parent left } else { // Hang p's RIGHT tree to parent.right parent.right = p.right; // Link p's right subtree to parent right } return; } /* ================================================== We must do the same when p has a LEFT subtree Case 1b: p has a left subtree ================================================== */ if ( p.right == null ) // If true, p has a left child { if ( p == root ) { root = root.left; return; } parent = myParent; // myParent was set by findNode(x).... /* ---------------------------------------------- Link p's left child as p's parent child ---------------------------------------------- */ if ( parent.left == p ) parent.left = p.left; else parent.right = p.left; return; } ```