Search, Insertion, and Deletion Operations in a Binary Search Tree (BST)
A Binary Search Tree is a special type of binary tree that satisfies the following property: for any node in the tree, all values in its left subtree are less than the node's value, and all values in its right subtree are greater than the node's value. This property makes Binary Search Trees highly efficient for search, insertion, and deletion operations.
1. Search Operation
-
Goal: Find a specific value within the Binary Search Tree.
-
Process: Utilizing the BST property, we can quickly narrow down the search scope.
- Start from the root node: Compare the target value with the root node's value.
- If they are equal: Congratulations, the search is successful.
- If the target value is less than the root node's value: This indicates the target value can only exist in the root's left subtree (if it exists). Therefore, we recursively continue the search in the left subtree.
- If the target value is greater than the root node's value: This indicates the target value can only exist in the root's right subtree. Therefore, we recursively continue the search in the right subtree.
- Encountering a null node: If during the search, based on the comparison result, we need to proceed to a child node, but that child node is empty (
null), it means the value we are looking for does not exist in the tree, and the search fails.
-
Example: Searching for the value
8in the BST shown in the diagram below.10 / \ 5 15 / \ / \ 3 7 12 18 / 8- Start from the root node
10. Since8 < 10, go to the left subtree. - The current node is
5. Since8 > 5, go to the right subtree. - The current node is
7. Since8 > 7, go to the right subtree. - The current node is
8. They are equal, the search is successful.
- Start from the root node
2. Insertion Operation
-
Goal: Insert a new value that does not exist in the tree into the correct position in the BST while maintaining the BST property.
-
Process: The insertion operation is very similar to the search operation. First, we must find the position where the new node should be placed. This position is necessarily an empty child node location of some leaf node.
- Start comparing from the root node: Compare the new node's value with the current node.
- If the new value is less than the current node's value:
- If the current node's left child is empty, insert the new node as the left child.
- If the left child is not empty, recursively perform the insertion operation in the left subtree.
- If the new value is greater than the current node's value:
- If the current node's right child is empty, insert the new node as the right child.
- If the right child is not empty, recursively perform the insertion operation in the right subtree.
- Note: If the new value equals the current node's value, depending on the specific implementation, one can choose to disallow duplicate values (ignore the insertion) or insert the new node as the right child (or left child), etc. Typically, BSTs do not contain duplicate values.
-
Example: Insert
10, 5, 15, 3, 7, 12, 18, 8sequentially into an empty tree. The final tree formed is the one shown in the example above.
3. Deletion Operation
-
Goal: Remove a specified node from the BST while maintaining the BST property. This is the most complex of the three operations because the node to be deleted may have zero, one, or two children. We need to discuss three cases.
-
Process:
- Locate the node to delete: First, use the search algorithm to find the node to be deleted, denoted as
node. - Handle based on the number of children of
node:-
Case 1:
nodeis a leaf node (has no children)- This is the simplest case. Simply set the pointer from its parent node to
null. - Example: Delete node
3. Just set the left pointer of node5tonull.
- This is the simplest case. Simply set the pointer from its parent node to
-
Case 2:
nodehas only one child- Make
node's parent node bypassnodeand point directly tonode's only child. - Example: Delete node
7(which has a left child8).- Find the parent node of
7, which is5. - Change
5's right pointer (originally pointing to7) to point to7's left child8. - This way, node
7is removed from the tree, and its child8takes its place.
- Find the parent node of
- Make
-
Case 3:
nodehas two children- This is the most complex case. We cannot simply delete it because two subtrees would need to be reconnected. Our strategy is:
a. Find a replacement: Innode's right subtree, find the node with the minimum value (this node is callednode's in-order successor). According to the BST property, this node is the smallest node greater thannode's value. Alternatively, you can choose the node with the maximum value innode's left subtree (in-order predecessor).
b. Copy the value: Copy the value of this found successor node (or predecessor node) intonode, overwritingnode's original value.
c. Delete the successor node: Now, the problem transforms into deleting that successor node (or predecessor node). This node has at most one child (because it is the minimum node in the right subtree, so it cannot have a left child; similarly, the maximum node in the left subtree cannot have a right child). This brings us back to Case 1 or Case 2, allowing us to delete it easily. - Example: Delete the root node
10.- Node
10has two children. - Find the node with the minimum value in its right subtree (rooted at
15). This node is12(it has no left child). - Replace the value of
10with12. - Now, there are two
12s in the original tree. We need to delete the original12in the right subtree. This node has only one right child (or none, as in this example). Delete it according to Case 2. Finally, the tree structure still satisfies the BST property.
- Node
- This is the most complex case. We cannot simply delete it because two subtrees would need to be reconnected. Our strategy is:
-
- Locate the node to delete: First, use the search algorithm to find the node to be deleted, denoted as
By mastering the three core operations of search, insertion, and deletion, you have grasped the fundamental working principles of a Binary Search Tree. The average time complexity of these operations is O(log n), where n is the number of nodes in the tree, making BST a very practical data structure.