Question
What is the time complexity of searching an element in
a balanced binary search tree (BST) with nnn nodes?Solution
In a balanced binary search tree (BST) , the height of the tree is maintained as O(log n)), ensuring that each level of the tree splits the search space roughly in half. This property allows searching for an element in the tree with logarithmic time complexity. For example, consider a balanced BST with 15 nodes. The height is approximately log 215≈4, so searching for any element will involve checking at most 4 nodes from the root to a leaf. The logarithmic reduction at each step (halving the search space) makes this approach efficient for large datasets. Balanced trees such as AVL or Red-Black trees maintain this logarithmic height through rotation operations during insertion and deletion, ensuring the O(log n) search time even after multiple modifications. Explanation of Incorrect Options: A) O(n) : This would be the time complexity for searching in an unbalanced BST or a linked list, where all nodes are skewed to one side. However, a balanced BST ensures O(log n) complexity. C) O(n2) This complexity is not applicable to BST search operations. It might appear in poorly optimized nested loops or certain pathological cases in other algorithms. D) O(1) Constant time search is achieved in hash tables, not in BSTs, as BSTs require traversing nodes to locate the target element. E) O(nlog n) This is the complexity of sorting algorithms like Merge Sort or Heap Sort, not searching in a BST.
What is the smallest 4-digit number that is exactly divisible by 9, 12 and 15?
- 60% of 'X' is (3/2) of 'Y'. Find the ratio X:Y, assuming X and Y are co-prime.
- Consider a number of the form '49678a' which is divisible by 6 but not divisible by 5. Find the value of (a × 2 + 3).
- If '38k721' is divisible by 3, then what is the highest possible value of 'k'?
A lending library has a fixed charge for the first 7 days and an additional charge for each day thereafter. Sahil paid Rs . 215 for a book kept for 15 ...
In a factory of 50 labours, if the two labours left the job whose wages were 1120 and 1340 and two new labours join the factory which results in decreas...
- The number of pencils with Tom and Jerry is 11 more than with Spike. Also, Jerry and Spike together have 9 more than Tom. If Tom and Spike together have 30...
- Suppose '68472m' is divisible by 6 but not divisible by 5. Find the value of (m × 3 - 2).
- What is to be subtracted from (13/20) so that the result is (7/25)?
- What number should be subtracted from both 36 and 28 so that the resulting numbers are in the ratio of 7:5?