Tree sort

From Wikipedia, the free encyclopedia
(Redirected from Binary tree sort)
Jump to navigation Jump to search

Template:Short description Script error: No such module "Unsubst". Template:Infobox Algorithm

A tree sort is a sort algorithm that builds a binary search tree from the elements to be sorted, and then traverses the tree (in-order) so that the elements come out in sorted order.[1] Its typical use is sorting elements online: after each insertion, the set of elements seen so far is available in sorted order.

Tree sort can be used as a one-time sort, but it is equivalent to quicksort as both recursively partition the elements based on a pivot, and since quicksort is in-place and has lower overhead, tree sort has few advantages over quicksort. It has better worst case complexity when a self-balancing tree is used, but even more overhead.

Efficiency

Adding one item to a binary search tree is on average an O(log n)Script error: No such module "Check for unknown parameters". process (in big O notation). Adding n items is an O(n log n)Script error: No such module "Check for unknown parameters". process, making tree sorting a 'fast sort' process. Adding an item to an unbalanced binary tree requires O(n)Script error: No such module "Check for unknown parameters". time in the worst-case: When the tree resembles a linked list (degenerate tree). This results in a worst case of O(n²)Script error: No such module "Check for unknown parameters". time for this sorting algorithm. This worst case occurs when the algorithm operates on an already sorted set, or one that is nearly sorted, reversed or nearly reversed. Expected O(n log n)Script error: No such module "Check for unknown parameters". time can however be achieved by shuffling the array, but this does not help for equal items.

The worst-case behaviour can be improved by using a self-balancing binary search tree. Using such a tree, the algorithm has an O(n log n)Script error: No such module "Check for unknown parameters". worst-case performance, thus being degree-optimal for a comparison sort. However, tree sort algorithms require separate memory to be allocated for the tree, as opposed to in-place algorithms such as quicksort or heapsort. On most common platforms, this means that heap memory has to be used, which is a significant performance hit when compared to quicksort and heapsortScript error: No such module "Unsubst".. When using a splay tree as the binary search tree, the resulting algorithm (called splaysort) has the additional property that it is an adaptive sort, meaning that its running time is faster than O(n log n)Script error: No such module "Check for unknown parameters". for inputs that are nearly sorted.

Example

The following tree sort algorithm in pseudocode accepts a collection of comparable items and outputs the items in ascending order:

Template:Syntaxhighlight

In a simple functional programming form, the algorithm (in Haskell) would look something like this:

 data Tree a = Leaf | Node (Tree a) a (Tree a)

 insert :: Ord a => a -> Tree a -> Tree a
 insert x Leaf = Node Leaf x Leaf
 insert x (Node t y s)
     | x <= y = Node (insert x t) y s
     | x > y  = Node t y (insert x s)

 flatten :: Tree a -> [a]
 flatten Leaf = []
 flatten (Node t x s) = flatten t ++ [x] ++ flatten s

 treesort :: Ord a => [a] -> [a]
 treesort = flatten . foldr insert Leaf

In the above implementation, both the insertion algorithm and the retrieval algorithm have O(n²)Script error: No such module "Check for unknown parameters". worst-case scenarios.

External links

Template:Sister project

References

<templatestyles src="Reflist/styles.css" />

  1. Script error: No such module "citation/CS1".

Script error: No such module "Check for unknown parameters".

Script error: No such module "Navbox".