retagged by
11,766 views
19 votes
19 votes

A binary search tree $T$ contains $n$ distinct elements. What is the time complexity of picking an element in $T$ that is smaller than the maximum element in $T$?

  1. $\Theta(n\log n)$
  2. $\Theta(n)$
  3. $\Theta(\log n)$
  4. $\Theta (1)$
retagged by

3 Answers

Best answer
50 votes
50 votes

If our data structure contains $n$ distinct elements then :

In all the standard data structures that we know/study about, If we want to pick/find an element that is not maximum (smaller than maximum) then time complexity will be $\Theta(1)$ because we only need to compare any two elements. Take any two elements that you can access in constant time, compare them, and return the smaller of those two elements.

PS: By “standard data structures that we know/study about” I mean the following :

Binary tree, Binary search tree, AVL tree, sorted or unsorted array, linked lists, arrays, stacks, queues, hash tables, heaps, etc.

NOTE: We don’t need to find the “Second Largest Number”. We need ANY number which is not the largest. So, take ANY two elements & the smaller in them is your answer. So, $O(1)$ time is required.

edited by
10 votes
10 votes

ANS IS D.

HERE WE HAVE TO FIND THE ELEMENT  WHICH IS SMALLER THAN MAXIMUM ELEMENT. WHICH CAN BE FOUND IN CONSTANT TIME. 

2 votes
2 votes
D is the right answer .

For picking an element in BST that is smaller than the maximum element you need at most 1 comparision ,hence constant operation .
Answer:

Related questions

33 votes
33 votes
4 answers
1
10 votes
10 votes
3 answers
2
Arjun asked Feb 18, 2021
8,356 views
Consider the following sequence of operations on an empty stack.$$\textsf{push}(54);\textsf{push}(52);\textsf{pop}();\textsf{push}(55);\textsf{push}(62);\textsf{s}=\texts...