**1. Best-Case Analysis for BST Insertion:**
* **Input Sequence:** The best-case performance for inserting
n distinct elements into an initially empty BST occurs when the elements are inserted in an order that keeps the tree as balanced as possible. This means inserting the median element of the sorted set first, then recursively inserting the medians of the left and right sub-problems. For example, if inserting elements from
1 to
n, the sequence would begin with
⌊n/2⌋, then
⌊n/4⌋,
⌊3n/4⌋, and so on. This strategy ensures the tree maintains a height of
O(logn).
* **Total Time Complexity:**
O(nlogn)
* **Justification:** In a perfectly balanced BST, the height of the tree is
O(logn). Each insertion operation involves traversing a path from the root to a leaf node (or an empty slot where the new node will be placed). The length of this path (and thus the number of comparisons) is proportional to the current height of the tree. Since the tree remains balanced throughout the insertions, each of the
n insertions takes approximately
O(logi) time, where
i is the number of nodes already in the tree. Summing these operations, the total number of comparisons for
n insertions is
∑i=1nO(logi)=O(nlogn).
**2. Worst-Case Analysis for BST Insertion:**
* **Input Sequence:** The worst-case performance occurs when the elements are inserted in a strictly increasing or strictly decreasing order. For example, inserting elements
1,2,3,…,n in that sequence, or
n,n−1,…,1. This causes the BST to degenerate into a skewed tree, essentially a linked list, where each node has only one child.
* **Total Time Complexity:**
O(n2)
* **Justification:** When elements are inserted in a strictly sorted order, each new element is always placed as the child of the deepest node (e.g., always the right child for increasing order). The
i-th element inserted will traverse a path of length
i−1 (making
i comparisons) to find its position. For instance, the first element takes 1 comparison, the second takes 2, the third takes 3, and so on, up to
n comparisons for the
n-th element. The total number of comparisons is the sum of an arithmetic series:
1+2+⋯+n=2n(n+1), which is
O(n2).
**3. Average-Case Analysis for BST Insertion:**
* **Input Sequence:** The average-case performance is typically observed when the
n distinct elements are inserted in a **random permutation**. This means that any of the
n! possible orderings of the
n elements is equally likely to be the input sequence.
* **Total Time Complexity:**
O(nlogn)
* **Intuition:** While individual insertions can still take
O(n) time in a randomly built BST (e.g., if a few elements happen to form a short skewed path), the probability of the tree consistently degenerating into a worst-case (skewed) structure is very low for a truly random input sequence. On average, the randomness in the input order helps to distribute elements across the tree, preventing extreme skewness. This results in the tree's height growing proportionally to
logn. The expected number of comparisons for a single insertion into a randomly built BST of
i nodes is
O(logi). Therefore, summing over
n insertions, the total expected number of comparisons is
O(nlogn). The average shape of a randomly built BST closely resembles that of a balanced tree.