KonkurStudy
همه کارت‌ها

Height and Leaf Bounds of a Binary Search Tree

A plain BST is not balanced, so its height ranges from Θ(log⁡n)\Theta(\log n) down to n−1n-1 and its leaf count from 11 to ⌈n/2⌉\lceil n/2\rceil.

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

اهمیت
۰/۵

Intuition

A BST enforces only the ordering invariant (left < node < right), not balance. So its shape depends entirely on insertion order, and any universally true claim must hold for every possible shape — from a perfectly balanced tree to a degenerate chain.

Result

For a BST on nn nodes (height/depth counted in edges, root at depth 00):

  • Height: minimum ⌈log⁡2n⌉\lceil \log_2 n\rceil (balanced), maximum n−1n-1 (degenerate chain). So depth is at most n−1n-1 always.
  • Leaves: minimum 11 (a chain has a single leaf), maximum ⌈n/2⌉\lceil n/2\rceil (complete tree). So you can never claim "at least log⁡n\log n leaves" or "n−1n-1 leaves."

Worked example (n=4n=4)

Degenerate (insert 1,2,3,4):     Balanced:
1                                    2
 \                                 /   \
  2          depth = 3 = n-1      1     3
   \         1 leaf                      \
    3                                     4
     \       depth = 2 = ceil(log2 4)
      4      2 leaves (max = ceil(4/2)=2)

The chain reaches depth n−1=3n-1=3; no shape exceeds it. Max leaves is ⌈4/2⌉=2\lceil 4/2\rceil=2, so n−1=3n-1=3 leaves is impossible.

Pitfall: Treating a BST as automatically balanced. The tempting log⁡n\log n answers assume balance, which a plain BST does not guarantee — the degenerate chain (depth n−1n-1, one leaf) breaks them. Only a self-balancing tree (AVL, red-black) forces Θ(log⁡n)\Theta(\log n) height.

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

Height range
⌈log⁡2n⌉≤h≤n−1\lceil \log_2 n\rceil \le h \le n-1

Min height for a balanced BST, max for a degenerate chain.

Leaf range
1≤ℓ≤⌈n/2⌉1 \le \ell \le \lceil n/2\rceil

A chain has one leaf; a complete binary tree maximizes leaves.

#binary-search-tree#tree-height#leaves#degenerate-tree#balanced-tree

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