• edited by
21,381 views
65 65 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)$

6 Answers

Best answer
64 64 votes

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
58 58 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)

22 22 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.

5 5 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

0 0 votes
Ans : (C)

It's a very common trap when it comes to BST , We always think BST as a Balanced BST in first view .
But when asked about worst case always go for Skewed BST.

There for :

Worst case time in : Searching -> O(n) , Inserting ->O(n) , Deleting -> O(n)
0 0 votes
## Answer

The tightest upper bound for the time complexity of inserting an object into a binary search tree (BST) of  nodes is **** (Option C).

---

### Explanation

The time complexity of inserting a node into a Binary Search Tree is directly proportional to the **height** of the tree, as the algorithm must traverse from the root to a leaf position to find the correct insertion point.

* **Average Case:** In a randomly built or balanced BST, the height is , leading to a complexity of .
* **Worst Case (Skewed Tree):** In the worst-case scenario, the BST can become **skewed** (left or right). This occurs when elements are inserted in a strictly increasing or decreasing order. In a skewed tree, the height is , and the structure effectively behaves like a linked list.
* **Insertion Logic:** To insert a new element in a skewed tree, you might have to compare it with every single node in the tree to reach the bottom-most leaf.
* Number of comparisons =
* Time Complexity =

Since the term **"tightest upper bound"** refers to the most restrictive Big-O notation that still accounts for the worst-case scenario,  is the correct answer.
Answer:
Position:
Show:

Related questions

41 41 votes
4 answers 4 answers
17.6k
17.6k views
Arjun asked Sep 24, 2014
17,580 views
The preorder traversal sequence of a binary search tree is $30, 20, 10, 15, 25, 23, 39, 35, 42$. Which one of the following is the postorder traversal sequence of the sam...
24 24 votes
4 answers 4 answers
9.5k
9.5k views
Arjun asked Sep 24, 2014
9,456 views
A tourist covers half of his journey by train at $60\;\text{km/h}$, half of the remainder by bus at $30\;\text{km/h}$ and the rest by cycle at $10\;\text{km/h}$. The aver...
32 32 votes
2 answers 2 answers
7.4k
7.4k views
Arjun asked Sep 24, 2014
7,424 views
Out of all the $2$-digit integers between $1$ and $100,$ a $2$-digit number has to be selected at random. What is the probability that the selected number is not divisibl...
35 35 votes
6 answers 6 answers
11.5k
11.5k views
Arjun asked Sep 24, 2014
11,541 views
What will be the maximum sum of $44, 42, 40, \dots$ ?$502$$504$$506$$500$