Deletion in Binary Search Tree (Implementation)
We will now write the implementation of the delete function which covers all the cases that we discussed previously.
Introduction #
Let’s implement the delete function for BSTs. We’ll build upon the code as we cater to each case.
1. Deleting an Empty Tree #
Let’s start with a skeleton function definition and cater to the first case. We return false
if the root node is null
.
In our current implementation of JavaScript, we always initialize an object of the Binary Search Tree with a value of the root node. However, there can be other implementations where we initialize an empty tree and insert values afterward. Moreover, the empty tree can also arise if we delete all the values from the tree.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.