Treesort, Insertion Order, and Tree Structure
If we take a set of key values and insert them into the binary search tree, an inorder traversal will print out the nodes in order
The performance of insertion and retrieval (and therefore of treesort) depends on the depth of the tree
- If a set of N keys are inserted in order (or in reverse order), the resulting tree will have depth N (essentially be a linked list)
- Random ordering will result in depths closer to lg N (the optimum), depending on the exact ordering used
- Examples (same keys, different sequences):
- MI AZ TX CA NV AK VA AR NY
- VA NV AK TX AZ MI AR CA NY (quicksort example, lecture 14)
- AK AR AZ CA MI NV NY TX VA (sorted order)
Treesort performance is essentially the same as quicksort when the first key is always chosen as the pivot (left subtree keys all less than pivot, right subtree keys all greater than pivot)
- Has same worst-case behavior as quicksort