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.
- Ravi is sitting inside train M which is travelling at a speed of 45 km/h. Another train N, whose length is half the length of train M and travelling with a...
Length of a train, whose speed is 60 km/h, is 300 metres. Find the time taken (in seconds) by train to cross a bridge of length 600 metres.
- The distance between city L and city M is 624 km. A train starts from L at 8 a.m. at 48 km/h. Another train starts from M at 9:30 a.m. towards L at a speed...
How many seconds will a train 66 m in length, travelling at the rate of 43 km an hour, take to pass another train 86 m long, proceeding in the same dire...
Train A, moving at 90 km/h, crosses a pole in 8 seconds. Find the time it takes to cross another train of equal length, coming from the opposite directi...
A train has to cover a distance of 112 km in 14 hours. If it covers half journey in 3/5th time, then the speed of covering the remaining dist...
Two trains, A and B, cross each other in 15 seconds and 35 seconds respectively, when running in opposite and the same direction respectively. If the sp...
Two trains, each 100 meters long, are running in opposite directions. If one train's speed is twice that of the other and they take 10 seconds to cross ...
Train 'C' can cross a pole in 6 seconds and a 120 metres long bridge in 10 seconds. Train 'D' whose length is 25% of the length of train 'C' runs in the...
Two trains ‘G’ and ‘H’ of equal lengths are running on parallel tracks at speeds of 30 m/s and 90 m/s, respectively. If they can cross each othe...