Fact-checked by Grok 2 weeks ago

Tree sort

Tree sort is a comparison-based sorting algorithm that builds a binary search tree from the input elements and then retrieves them in sorted order via an in-order traversal of the tree. The process begins by inserting each element into the binary search tree, where the tree's structure ensures that left subtrees contain smaller values and right subtrees contain larger ones, maintaining the search tree property. Once all elements are inserted, an in-order traversal visits nodes in ascending order, effectively producing the sorted list without additional comparisons. This algorithm is a variant of insertion sort, as each insertion mimics the insertion step but leverages the tree for efficient placement. In terms of time complexity, tree sort achieves an average-case performance of O(n log n) when the tree remains balanced, but it can degrade to O(n²) in the worst case if the input causes a degenerate (unbalanced) tree, similar to a linked list. It is stable, preserving the relative order of equal elements, and requires O(n) extra space for the tree structure. Using self-balancing variants like AVL or red-black trees can guarantee the optimal O(n log n) bound.

Overview

Definition

Tree sort is a comparison-based that utilizes a (BST) to organize and retrieve elements in sorted order. It operates by first constructing a BST from the input elements, leveraging the tree's inherent ordering properties to facilitate efficient arrangement without requiring explicit comparisons during the final output phase. The core concept involves inserting each element from the unsorted list into the BST, where the tree maintains the invariant that all values in a node's left subtree are less than the node's value, and all values in the right subtree are greater. A is a specialized that supports this ordering, enabling logarithmic-time insertions on average for balanced trees. Subsequent in-order traversal of the BST—recursively processing the left subtree, the node itself, and then the right subtree—produces the elements in ascending sorted sequence. This hybrid method combines tree insertion for dynamic organization with traversal for extraction, distinguishing it from purely array-based sorters. Tree sort offers an alternative to algorithms like or , proving beneficial in scenarios where tree structures excel, such as online sorting of incrementally arriving data or maintaining for equal elements.

History

Tree sort originated in the early alongside the development of binary search trees (BSTs), which provide the foundational for the algorithm. The BST was independently invented in 1960 by researchers P. F. Windley, A. D. Booth, A. J. T. Colin, and T. N. Hibbard as an efficient method for storing and searching ordered data. This structure enabled the conceptualization of sorting via tree insertion followed by in-order traversal, emerging amid broader research into tree-based algorithms during that decade. A key milestone came with the formal analysis and description of tree-based sorting in influential algorithm literature. Donald Knuth's 1973 book The Art of Computer Programming, Volume 3: Sorting and Searching discusses tree structures and their applications in sorting (Chapter 5) and searching (Chapter 6), including methods that leverage binary trees for ordered data processing and highlighting their properties for producing sorted output. Knuth's work, drawing on prior BST developments, solidified the place of such tree-based approaches in algorithmic theory. In the ensuing decades, tree sort evolved primarily as an educational tool rather than a practical implementation. By the 1980s, variations incorporating balanced BSTs, such as AVL trees invented in 1962, were explored to address structural imbalances, though these adaptations remained niche. As of 2025, tree sort continues to serve mainly pedagogical purposes, illustrating BST operations and traversal techniques in curricula, with limited adoption in production systems due to more efficient alternatives.

Algorithm

Binary Search Tree Basics

A (BST) is a type of where each contains a , and for any given , all s in its left subtree are less than the 's , while all s in its right subtree are greater than or equal to the 's . This ordering property is maintained recursively across all s, ensuring that the tree structure supports efficient searching and organization of data. BSTs can permit duplicate s by directing equal s to the right subtree, allowing multiple s with the same value while preserving the search properties. The efficiency of a BST is largely determined by its , which is the longest from the to a leaf . In a balanced BST, the is approximately logarithmic in the number of , enabling fast operations; however, if insertions occur in a sorted or nearly sorted order, the tree can become unbalanced, resembling a and leading to linear performance degradation. This height dependency underscores the importance of the 's shape in practical applications, where maintaining balance is often crucial for consistent performance. Basic operations in a BST, including search, insertion, and deletion, operate by traversing from the downward, leveraging the ordering to prune unnecessary branches. Each of these operations takes time proportional to the h of the , yielding an average-case complexity of O(log n) for balanced trees but potentially O(n) in the worst case for unbalanced ones. To address the risks of imbalance, self-balancing variants of BSTs, such as AVL trees introduced by Adelson-Velsky and Landis in 1962 and red-black trees developed by in 1972, incorporate rotation and coloring mechanisms to keep the height bounded by O(log n). These enhancements ensure logarithmic performance without delving into the specific balancing algorithms here.

Insertion and In-order Traversal Steps

Tree sort operates in two primary phases: constructing a (BST) from the input elements and then extracting the sorted sequence through traversal. The process begins by initializing an empty BST. Each element from the unsorted input array is inserted sequentially, starting from the first element. During insertion, if the tree is empty, the element becomes the root node. Otherwise, the insertion algorithm compares the new element's key with the current node's key: if smaller, it recurses to the left subtree; otherwise (equal or larger), it recurses to the right subtree. This continues until a null position is found, where the new node is attached as a leaf. Once all elements are inserted, the BST maintains the property that in-order traversal will visit nodes in ascending order. The second phase performs an in-order traversal, which recursively visits the left subtree, processes the current (by adding its value to an output ), and then visits the right subtree. This traversal collects the elements into a sorted or , yielding the final sorted output. The resulting contains the elements in non-decreasing order, assuming the BST properties hold. In this implementation, equal keys are directed to the right subtree, allowing duplicates to be included and preserving their relative order from the input to ensure . The following high-level illustrates the insertion and traversal steps:
function treeSort(inputArray):
    root = null
    for each element in inputArray:
        root = insert(root, element)
    sortedArray = []
    inOrderTraversal(root, sortedArray)
    return sortedArray

function insert(node, key):
    if node is null:
        return new [Node](/page/Node)(key)
    if key < node.key:
        node.left = insert(node.left, key)
    else:
        node.right = insert(node.right, key)
    return node

function inOrderTraversal(node, sortedArray):
    if node is not null:
        inOrderTraversal(node.left, sortedArray)
        append node.key to sortedArray
        inOrderTraversal(node.right, sortedArray)
After traversal, the original tree structure is typically discarded, as the sorted output is stored separately; the process is not in-place unless the input array is overwritten during traversal.

Analysis

Time Complexity

The time complexity of tree sort is primarily determined by the cost of inserting elements into the binary search tree (BST) followed by the in-order traversal to produce the sorted output. The traversal step requires O(n) time, as it visits each of the n nodes exactly once. Thus, the overall performance is dominated by the insertion phase, where each of the n elements is inserted sequentially into the BST. In the average case, assuming random input order that leads to a reasonably balanced tree, each insertion takes O(log n) time on average, resulting in a total time complexity of O(n log n) for the insertions plus O(n) for traversal, yielding O(n log n) overall. This average-case bound arises because the expected height of the tree remains O(log n) under random permutations, ensuring that the search path for each insertion averages logarithmic length. The worst-case time complexity occurs when the input is already sorted or reverse-sorted, causing the BST to degenerate into a linear chain with height O(n). In this scenario, each insertion after the first traverses nearly the entire existing tree, taking O(i) time for the i-th insertion, leading to a total insertion cost of O(n²) and overall O(n²) time complexity. The best case also achieves O(n log n), typically with inputs that maintain tree balance, such as random orders. More formally, the total time T(n) for insertions can be expressed as the sum of the heights traversed during each insertion:
T(n) = \sum_{i=1}^{n} O(h_i) + O(n),
where h_i is the height of the tree after i-1 insertions (i.e., the depth of the path to the insertion point for the i-th element), and the O(n) accounts for the traversal. In the balanced case, each h_i is O(log i), so the sum evaluates to O(n log n); in the skewed case, h_i \approx i, yielding O(n²). This derivation highlights how tree balance directly influences performance.
A key factor affecting the time complexity is the order of input elements, as basic tree sort lacks any balancing mechanism like rotations (found in AVL or red-black trees), making it sensitive to pathological inputs that unbalance the structure.

Space Complexity

Tree sort requires O(n) space to store the binary search tree (BST) constructed from n elements, as each element is represented by a node containing the value and two pointers to child nodes. This storage is necessary regardless of the input order, since the tree holds all elements during the sorting process. In comparison to array-based sorts like insertion sort, tree sort incurs higher constant factors due to the additional pointers—typically two per node—beyond the element values themselves. The auxiliary space for recursive operations in tree sort, such as insertion into the and the subsequent in-order traversal, is O(h), where h is the height of the tree. In the average case for random inputs, the is balanced with h = O(log n), leading to O(log n) auxiliary space from the recursion stack. However, in the worst case of skewed inputs (e.g., already sorted data), h = O(n), resulting in O(n) auxiliary space. Tree sort is not truly in-place, as the tree structure demands additional memory proportional to n, separate from the input array. Even if the output sorted list is constructed in a new array during traversal, it requires another O(n) space; attempts to reuse the input array cannot eliminate the tree's overhead. The total space complexity can thus be expressed as: S(n) = O(n) + O(h) where the O(n) term dominates for the nodes and the O(h) accounts for the recursion stack.

Implementation and Examples

Pseudocode

The pseudocode for tree sort outlines a recursive implementation using a (BST), where elements are inserted to form the tree and then extracted in sorted order via , producing a new list containing all input elements. To ensure stability (preserving the relative order of equal elements), duplicates are inserted into the right subtree. The node structure for the BST is defined as follows:
structure Node
    value: element
    left: Node or null
    right: Node or null
end structure
The insertion function recursively builds the by placing each value in the appropriate subtree based on comparisons. Duplicates (value == current.value) are treated as greater or equal and inserted to the right to maintain stability.
procedure insert(current: Node, value: element) returns Node
    if current is null
        return new Node with value, left=null, right=null
    if value < current.value
        current.left = insert(current.left, value)
    else
        current.right = insert(current.right, value)
    return current
end procedure
The in-order traversal function recursively visits nodes to append values to a result list in ascending order: left subtree, current node, right subtree.
procedure inOrder(current: Node, result: list)
    if current is null
        return
    inOrder(current.left, result)
    append current.value to result
    inOrder(current.right, result)
end procedure
The main tree sort procedure initializes an empty tree, inserts all input elements, performs the in-order traversal to populate a new sorted list, and returns it. This pseudocode assumes no null or invalid inputs for simplicity. Iterative alternatives for insertion and traversal exist to mitigate stack overflow risks in deeply unbalanced trees.
function treeSort(input: array of elements) returns array
    root = null
    for each value in input
        root = insert(root, value)
    sorted = empty list
    inOrder(root, sorted)
    return sorted
end function

Worked Example

To illustrate the tree sort process, consider the unsorted array [5, 3, 8, 4, 2]. The algorithm begins by constructing a binary search tree (BST) through sequential insertion of each element, following standard BST rules where each new node is placed to the left if smaller than the parent or to the right if larger or equal, recursing until the appropriate leaf position is found. Start with an empty tree and insert 5, which becomes the root node. Next, insert 3, which is less than 5 and thus becomes the left child of 5. Insert 8, which is greater than 5 and becomes the right child of 5. Then, insert 4, which is greater than 3 (the left child of 5) but less than 5, so it becomes the right child of 3. Finally, insert 2, which is less than 3 and thus becomes the left child of 3. The resulting BST structure is:
    5
   / \
  3   8
 / \
2   4
To obtain the sorted array, perform an in-order traversal of the tree, which visits nodes in the order: left subtree, root, right subtree. This yields: 2 (left of 3), 3 (root of left subtree), 4 (right of 3), 5 (root), 8 (right of 5), producing the sorted array [2, 3, 4, 5, 8]. As an edge case, consider the already-sorted input [1, 2, 3, 4, 5]. Sequential insertion into a BST results in a right-skewed tree forming a linear chain (1 as root, 2 as right child of 1, 3 as right child of 2, and so on), which demonstrates how tree sort can degrade to quadratic performance in the worst case due to unbalanced structure. To demonstrate handling of duplicates and stability, consider the input [5, 3, 5, 4]. Insert 5 (root), 3 (left of 5), second 5 (>=5, right of 5), 4 (>3, <5, right of 3). The tree is:
    5
   / \
  3   5
   \
    4
In-order traversal: 3, 4, 5 (first), 5 (second), preserving the order of the two 5s as in the input. Sorted: [3, 4, 5, 5].

Comparisons and Applications

Comparison to Other Algorithms

Tree sort shares similarities with in its average-case of O(n log n), achieved through dynamic partitioning of data during insertions into the , but employs a more efficient pivot-based partitioning that reduces constant factors and performs better in for large datasets. However, both algorithms risk a worst-case O(n²) performance— from poor pivot choices and tree sort from skewed tree structures—though 's randomized variants mitigate this more reliably. Tree sort is , preserving the relative order of equal elements when duplicates are handled by inserting to the right subtree, whereas is typically unstable unless modified with additional space. In comparison to , tree sort evokes a divide-and-conquer by incrementally building a sorted structure through insertions, but it relies on tree height for efficiency rather than merge sort's fixed logarithmic depth, leading to less predictable performance. guarantees O(n log n) time in all cases and maintains by preserving the order of equal elements during merging, making it preferable for applications requiring consistent behavior, such as of large files, while tree sort's potential for imbalance introduces variability. Both tree sort and heap sort leverage tree structures for organization, with tree sort using a binary search tree for insertions and in-order traversal, contrasted by heap sort's use of a complete binary heap for selection-based extraction. Heap sort operates in-place with a guaranteed O(n log n) time complexity across all cases and minimal auxiliary space, offering reliable performance without the degeneration risk inherent to tree sort's O(n²) worst case from unbalanced trees. Tree sort stands out for its integration of search and sorting operations within the same binary search tree framework, which proves advantageous in database indexing contexts where maintaining sorted access to data facilitates efficient queries alongside ordering.
AlgorithmAverage Time ComplexityWorst-Case Time ComplexityStability
QuicksortO(n log n)O(n²)No
Merge sortO(n log n)O(n log n)Yes
Heap sortO(n log n)O(n log n)No
Tree sortO(n log n)O(n²)Yes

Advantages and Disadvantages

Tree sort provides an intuitive approach for learning binary search trees, as it leverages fundamental BST operations like insertion and in-order traversal to produce a sorted sequence, making it a valuable educational tool in curricula. This method also enables partial sorting, where subsets of the data can be traversed and extracted in order without rebuilding the entire tree. Additionally, it integrates seamlessly with tree-based data structures in storage systems, allowing sorted output directly from the tree without separate steps. Recent variants, such as TSort, optimize tree sort for DRAM-persistent memory systems, improving and reducing writes in byte-addressable storage scenarios as of 2024. In practical scenarios, tree sort excels as a aid and finds niche use in dynamic datasets where elements arrive incrementally, supporting online without requiring all data upfront. Despite these strengths, tree sort has notable limitations that restrict its broader adoption. The algorithm is highly sensitive to input order, potentially degrading to O(n²) performance in the worst case when the tree becomes unbalanced, such as with already sorted or reverse-sorted inputs. Moreover, it incurs higher constant factors due to pointer manipulations and random memory accesses in tree construction and traversal, leading to poorer performance compared to array-based sorts. As of November 2025, tree sort is rarely implemented in sorting functions; for instance, 's sorted() employs Powersort, an adaptive based on and , optimized for real-world data (introduced in Python 3.11). Similarly, Java's Arrays.sort() uses for object arrays and a dual-pivot variant for primitives, prioritizing and over tree-based approaches. To address key drawbacks, tree sort can be improved by employing self-balancing BST variants like AVL trees or red-black trees, which ensure logarithmic height and consistent O(n log n) worst-case performance regardless of input .

References

  1. [1]
    Sorting
    Tree Sort. For each item in the array, add it to a binary search tree. Traverse the tree inorder, writing each item back into the array. This is O ( n log ...
  2. [2]
    Tree Sort - UAH
    Perform an Inorder tree traversal to list records in sorted order. Analysis: Tree Sort runs in O(n log n) time.
  3. [3]
    [PDF] Sorting - Web - UNLV Computer Science
    Tree sort is a fast form of insertion sort. We insert the items of X one at a time into a search structure, such as a binary search tree, and then create an ...
  4. [4]
    Sorting in Binary Trees | Baeldung on Computer Science
    Jan 24, 2024 · Tree sort is an online sorting algorithm that builds a binary search tree from the elements input to be sorted, and then traverses the tree, in- ...
  5. [5]
    treesort (1)
    treesort (1). (algorithm). Definition: A sort algorithm that first builds a binary search tree of the keys, then accesses the keys with an in-order traversal.
  6. [6]
    What is tree sort? - Educative.io
    Aug 18, 2025 · Tree sort is a sorting algorithm that builds a binary search tree (BST) from the input elements and then performs an in-order traversal to ...
  7. [7]
    Binary search tree
    Binary Search Tree (BST) was invented by P.F. Windley, A.D. Booth, A.J.T. Colin, and T.N. Hibbard in 1960. In computer science, BST, sometimes called ordered ...
  8. [8]
    The Art of Computer Programming (TAOCP)
    by Donald E. Knuth. Click here to sign up for The Art of Computer Programming Newsletter , which features updates on new editions and promotions.
  9. [9]
    [PDF] ©David Gries, 2018 Extensions to BSTs The binary search tree (BST ...
    AVL trees were invented in 1962 by Russians Adelson-Velsky and Landis. The tree is named after the authors: AV for Adelson-Velsky and L for Landis. In an AVL ...
  10. [10]
    Tree Sort - GeeksforGeeks
    Sep 11, 2023 · Tree sort is a sorting algorithm that is based on Binary Search Tree data structure. It first creates a binary search tree from the elements of the input list ...
  11. [11]
    12.11. Binary Search Trees - OpenDSA
    Binary Search Tree Definition¶. A binary search tree (BST) is a binary tree that conforms to the following condition, known as the binary search tree property.
  12. [12]
    CS 225 | Binary Search Trees
    The properties of a binary search tree are recursive: if we consider any node as a “root,” these properties will remain true.
  13. [13]
    ICS 46 Spring 2022, Notes and Examples: Binary Search Trees
    A binary search tree is a binary tree in which every (internal) node stores a unique key. For every node n containing a key k: All of the nodes in n's left ...
  14. [14]
    [PDF] 1 Binary search trees
    A binary search tree is a data structure composed of nodes. Each node has a key, which determines the node's position in the tree.
  15. [15]
    CS 367-3 - Binary Search Trees - cs.wisc.edu
    One property of a binary search tree is that an in-order traversal walks over the nodes in order of their keys (thus the name in-order).
  16. [16]
    Binary Search Trees
    Binary search trees store collections of items that can be ordered, such as integers. Binary search trees support the following standard operations. search ...<|control11|><|separator|>
  17. [17]
    SearchTree: Binary Search Trees - Software Foundations
    Insert and lookup operations on BSTs take time proportional to the height of the tree. If the tree is balanced, the operations therefore take logarithmic time.
  18. [18]
    [PDF] Rank-Balanced Trees - Siddhartha Sen
    Abstract. Since the invention of AVL trees in 1962, a wide variety of ways to balance binary search trees have been proposed. Notable are red-black trees, ...
  19. [19]
  20. [20]
    Binary Search Tree Insertion
    A binary search tree, sometimes called an ordered or sorted binary tree is a binary tree in which nodes are ordered in the following way.Missing: steps | Show results with:steps
  21. [21]
    Operations on Binary Search Tree's
    InOrderTraversal : The idea of inorder traversal is that we visit the nodes in the order left-root-right, meaning for any subtree in the path, left node must be ...
  22. [22]
    Binary Search Trees
    Space complexity: Θ ( n ) extra space. Time complexity: Best case is Θ ( n log ⁡ n ) if your tree is nearly balanced; worst case is Θ ( n 2 ) if degenerate.
  23. [23]
  24. [24]
    [PDF] Sorting
    The time complexity of tree sort is quadratic in the worst case. O(n log n) ... Auxiliary space complexity is space used over and above the space needed ...
  25. [25]
    Complexity of different operations in Binary tree, Binary Search Tree ...
    Dec 21, 2022 · In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees.
  26. [26]
    Binary tree sort is an in-place sorting algorithm. - Sarthaks eConnect
    Feb 19, 2022 · The correct option is (b) False Easy explanation - In binary tree sort it is required to reserve one tree node for each array element.
  27. [27]
    Lecture 5: Binary Search Trees, BST Sort | Introduction to Algorithms
    In this lecture, binary search trees are introduced, and several operations are covered: insertion, finding a value, finding the minimum element.<|control11|><|separator|>
  28. [28]
    Binary Search Tree Insertion
    The new tree node is always inserted as a leaf node. Pseudocode for an iterative version of this algorithm is shown below. Iterative Insertion into a Binary ...
  29. [29]
    [PDF] Binary Search Tree
    The ordering is: the left subtree, the right subtree, the current node. 3. Page 4. Inorder Traversal Pseudocode. This recursive algorithm takes as the input a.
  30. [30]
    HW13 – ECE 264 Advanced C Programming - Purdue Engineering
    This homework is about sorting. You will implement the tree sort algorithm to help you learn about binary search trees (BSTs) and sorting algorithms.
  31. [31]
    [PDF] Sorting algorithm
    Quicksort is a space-optimized version of the binary tree sort. Instead of inserting items sequentially into an explicit tree, quicksort organizes them ...
  32. [32]
    [PDF] Sorting
    Mergesort and quicksort are the two standard divide-and-conquer sorting algorithms. ... Tree sort is a fast form of insertion sort. We ... Heap sort is a ...
  33. [33]
    Binary Search Tree - GeeksforGeeks
    Sep 24, 2025 · A Binary Search Tree (BST) is a type of binary tree data structure in which each node contains a unique key and satisfies a specific ordering property.Handling duplicates in BST · Binary Tree to BST · Insertion in BST · Search in BST<|control11|><|separator|>
  34. [34]
    [PDF] Sorting on Byte-Addressable Storage: The Resurgence of Tree ...
    quicksort, heapsort, and insertion sort. It begins with quicksort and ... Figure 4: Performance of tree-sort variants when sorting 400 million data elements with ...<|control11|><|separator|>
  35. [35]
    Sorting algorithm - Wikipedia
    History and concepts​​ Bubble sort was analyzed as early as 1956. Asymptotically optimal algorithms have been known since the mid-20th century – new algorithms ...Merge sort · Block sort · Bubble sort · Insertion sort
  36. [36]
    Sorting Techniques — Python 3.14.0 documentation
    The Timsort algorithm used in Python does multiple sorts efficiently because it can take advantage of any ordering already present in a dataset.Sorting Techniques · Key Functions · Operator Module Functions...
  37. [37]
    Sorting in Java | Baeldung
    Jan 5, 2024 · Let's start by sorting integer arrays first using Arrays.sort() method. We'll define the following int arrays in a @Before jUnit method:<|control11|><|separator|>