in DS edited by
12,156 views
41 votes
41 votes

Which one of the following is the tightest upper bound that represents the time complexity of inserting an object into a binary search tree of $n$ nodes?

  1. $O(1)$
  2. $O(\log n)$
  3. $O(n)$
  4. $O(n \log n)$
in DS edited by
by
12.2k views

2 Comments

@Bhagirathi @Dhananjay So does we have to take the meaning of tightest upper bound as the worst case time complexity always and not the average ?

0
0

skewed tree

1
1

4 Answers

51 votes
51 votes
Best answer

Option (C) is True .
Suppose that we need to insert a node $z$ such that $k = key[z]$. Using binary search we find a nil such that replacing it by $z$ does not break the BST-property

BST-Insert$\bf{(x, z, k)}$

  1. : if $x = nil$ then return “Error”
  2. : $y \leftarrow x$
  3. : while true do $\{$
  4. : if  $key[y] < k$
  5. : then $z \leftarrow left[y]$
  6. : else $z \leftarrow right[y]$
  7. : if $z = nil$ break
  8. : $\}$
  9. : if $key[y] > k$ then $left[y] \leftarrow z$
  10. : else $right[p[y]] \leftarrow z$


Time Complexity Analysis :

  1. Best $Case = O(1)$ , When it is smallest/greatest element and BST contains only all greater/smaller element than inserting element respectively.
  2. Avg $Case = O(\log n)$ , When it belongs between some elements .
  3. Worst $Case = O(n)$ , When it is smallest/greatest element and BST contains only all smaller/greater element than inserting element respectively.
edited by

4 Comments

Hi @GO Classes

Is tightest upper bound and worst case time are one and the same?

If yes then should we always consider worst case time if they ask tightest upper bound time?

Kindly confirm.

Thanks,

0
0

@Nandhakumar

If a question asks about the time complexity of an algorithm without specifying whether it's the best, worst, or average case, it means they want to know the complexity in all scenarios.

For example, if the best case is $ \theta(n) $ and the worst case is $ \theta(n^2) $, you can say that the algorithm's time complexity is $ O(n^2) $ or $ O(n^3) $ or even $ O(n^4) $. However, if they insist on the tightest bound, then the answer would be $ O(n^2) $.

Summary:

Question (MSQ):

What is the tightest bound time complexity of an algorithm that has a best-case time complexity $ \theta(n) $ and a worst-case time complexity $ \theta(n^2) $? (Please try to solve it without looking at the answers)

A. $ \Omega(n) $

B. $ \Omega(n^2) $

C. $ O(n^2) $

D. $ O(n^3) $

The correct answers are A and C.

($ \Omega(n) $ is tighest lower bound and $ O(n^2) $ is tighest upper bound).

3
3

Thank you @Sachin Mittal 1sir for the quick update. Got it now.

0
0
42 votes
42 votes

Since, in worst case the tree can grow upto the height of n (skewed tree), therefore tightest upper bound is O(n)

So, answer is (C)

18 votes
18 votes

(C) O(n)

To insert an element into BST, first we need to find its place & it might take O(n) time like in the following tree:

Skewed Tree

To insert element 60 in this tree we need to traverse all the nodes.

4 Comments

why is not answer  O(logn) ?
0
0
O(logn) is average case

suppose the tree is like this

10

  \

   20

so to insert 21 we have to go from 10 then 20 then only can insert 21 .. tht's why it takes O(n) time in Worst Case.
6
6
@Bikram sir,
What does the "tightest upper bound" signify here? will the answer change if the word "tightest" is removed?
0
0
4 votes
4 votes

O(1) : to create a node

O(n) : to find place

O(1) : to link

total= O(1) + O(n) + O(1)  = O(n) 

C

3 Comments

O(n) to find place only if it is a skewed tree. If it is a balanced tree then O(logn) to find place, isn't ?
2
2
yes this is the case from tightest bound refers to worst case am I  correct
0
0
Answer:

Related questions