0% completed
A Binary Search Tree (BST) is a type of binary tree where each node has at most two children, and the tree maintains a specific order. For any given node:
- The left subtree contains only nodes with values less than the node’s value.
- The right subtree contains only nodes with values greater than the node’s value.
This property enables efficient searching, insertion, and deletion operations, making BSTs useful for various applications where ordered data needs to be stored and accessed quickly.
Basic Operations on a BST and Their Complexity Analysis
1. Insertion
Time Complexity
-
Insertion Process:
- Traversal: The traversal path depends on the value of the key being inserted. The function will travel down one branch of the tree, either to the left or right, at each recursive step.
- Number of Recursive Calls:
- In a balanced BST, the height
h
of the tree is approximately \log n, wheren
is the number of nodes. Therefore, the function will make up to O(\log n) recursive calls. - In an unbalanced BST (e.g., if the tree resembles a linked list), the height
h
can be as large asn
. In this case, the function may make up to O(n) recursive calls.
- In a balanced BST, the height
Overall Time Complexity:
- Average Case: O(\log n) for a balanced BST.
- Worst Case: O(n) for an unbalanced BST (e.g., skewed tree).
Space Complexity
-
Call Stack (Recursive Space):
- The space complexity due to recursion is directly proportional to the maximum depth of the call stack.
- In a balanced BST, the maximum depth of the recursive stack is O(\log n).
- In an unbalanced BST, the maximum depth can reach O(n) if the tree is skewed.
-
Auxiliary Space:
- The function itself does not use additional space apart from the recursion stack.
Overall Space Complexity:
- Average Case: O(\log n) for a balanced BST.
- Worst Case: O(n) for an unbalanced BST.
2. Search
Time Complexity
- Search Process:
- Traversal: At each recursive step, the function decides to move left (
search(root.left, key)
) or right (search(root.right, key)
) based on the value ofkey
relative toroot.val
. - Number of Recursive Calls:
- In a balanced BST, the height
h
of the tree is approximately \log n, wheren
is the number of nodes. The function will make up to O(\log n) recursive calls in this case. - In an unbalanced BST (e.g., if the tree is skewed), the height
h
can be up ton
. The function may make up to O(n) recursive calls.
- In a balanced BST, the height
- Traversal: At each recursive step, the function decides to move left (
Overall Time Complexity:
- Average Case: O(\log n) for a balanced BST.
- Worst Case: O(n) for an unbalanced BST.
Space Complexity
-
Call Stack (Recursive Space):
- The space complexity due to recursion is directly proportional to the maximum depth of the call stack.
- In a balanced BST, the maximum depth of the recursive stack is O(\log n).
- In an unbalanced BST, the maximum depth can reach O(n) if the tree is skewed.
-
Auxiliary Space:
- The function itself does not use any additional space beyond the call stack.
Overall Space Complexity:
- Average Case: O(\log n) for a balanced BST.
- Worst Case: O(n) for an unbalanced BST.
3. Deletion
Time Complexity
-
Traversal and Search for the Node:
- Number of Recursive Calls:
- In a balanced BST, the traversal depth is O(\log n), where
n
is the number of nodes in the tree. - In an unbalanced BST, the traversal depth can be up to O(n) if the tree is skewed.
- In a balanced BST, the traversal depth is O(\log n), where
- Number of Recursive Calls:
-
Deleting the Node:
- Case 1: Node with No Children:
- Directly removes the node by returning
null
. - Time Complexity: O(1).
- Directly removes the node by returning
- Case 2: Node with One Child:
- The node is replaced by its single child.
- Time Complexity: O(1).
- Case 3: Node with Two Children:
- Finds the in-order successor (the smallest node in the right subtree) using the
findMin
method, and recursively deletes it. - Finding the In-order Successor: The
findMin
method traverses the leftmost path in the right subtree.- Time Complexity of
findMin
: O(h), whereh
is the height of the tree.
- Time Complexity of
- Finds the in-order successor (the smallest node in the right subtree) using the
- Case 1: Node with No Children:
Overall Time Complexity:
- Average Case: O(\log n) for a balanced BST.
- Worst Case: O(n) for an unbalanced BST, including finding and deleting the node.
Space Complexity
-
Call Stack (Recursive Space):
- The space complexity depends on the recursion depth, which is proportional to the height of the tree.
- Balanced Tree: O(\log n) space for the recursive stack.
- Unbalanced Tree: O(n) space for the recursive stack in the worst case.
-
Auxiliary Space:
- The
findMin
method does not use any additional space other than traversing nodes.
- The
Overall Space Complexity:
- Average Case: O(\log n) for balanced trees.
- Worst Case: O(n) for unbalanced trees.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible