KonkurStudy
همه کارت‌ها

Worst-case height of an unbalanced BST

A plain BST has no balance guarantee, so its worst-case height is Θ(n)\Theta(n), making search/min/max O(n)O(n).

کارت مفهوم کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

Intuition

Most BST operations (search, insert, find-min/max) walk a single root-to-node path, so their cost is bounded by the tree height hh. A balanced tree keeps h=Θ(log⁡n)h=\Theta(\log n), but a plain BST gives no balance guarantee — its shape depends entirely on insertion order.

Result

If keys arrive in sorted (increasing or decreasing) order, every new node attaches at the end of one spine, degenerating the tree into a single chain ("linked list") of nn nodes. Then h=n−1=Θ(n)h = n-1 = \Theta(n) so search, find-min, and find-max all cost O(n)O(n) in the worst case — not O(log⁡n)O(\log n).

Worked example

Finding the minimum = walk down left children until none remain:

Insert n, n-1, ..., 2, 1  ->  left chain:

   n
  /
 n-1
 /
...
 /
 1   <- minimum, n-1 steps down => O(n)

The leftmost path has length n−1n-1, so reaching key 11 takes Θ(n)\Theta(n) steps.

Exam trick: When a question says worst case for a plain BST, the answer is O(n)O(n). Reflexively answering O(log⁡n)O(\log n) gives the balanced/average-case bound; that only holds with a balancing scheme such as AVL or red-black.

فرمول‌های کلیدی

Worst-case height
hworst=n−1=Θ(n)h_{worst} = n - 1 = \Theta(n)

Height of a degenerate (chain) BST built from sorted insertions of n keys.

Balanced height (contrast)
hbalanced=Θ(log⁡n)h_{balanced} = \Theta(\log n)

Height when balance is enforced (AVL, red-black); gives O(log n) search.

#binary-search-tree#tree-height#worst-case#skewed-tree#complexity

سوالات تمرینی