Repaso
EspaƱol
Algorithms & DSIntermediateALG-22

Worst-case search time in an unbalanced BST built from sorted input?

Options

  1. O(1)
  2. O(log n)
  3. O(n) (Correct option)
  4. O(n log n)

Answer

O(n)

Why

Inserting 1, 2, 3, 4, 5 in order into a plain BST makes each new key the right child of the previous one. The 'tree' is really a straight line, like a linked list.

Searching then visits every node: O(n).

Self-balancing trees (AVL, red-black) reorganize themselves to keep height about log n, guaranteeing O(log n).

Remember

Unbalanced BST can degrade to O(n).

Key terms

Practice this question in Repaso