📑 Table of Contents

The Day–Stout–Warren (DSW) algorithm is a method for efficiently balancing binary search trees – that is, decreasing their height to O(log n) nodes, where n is the total number of nodes. Unlike a self-balancing binary search tree, it does not do this incrementally during each operation, but periodically, so that its cost can be amortized over many operations. The algorithm was designed by Quentin F. Stout and Bette Warren in a 1986 CACM paper,[1] based on work done by Colin Day in 1976.[2]

The algorithm requires linear (O(n)) time and is in-place. The original algorithm by Day generates as compact a tree as possible: all levels of the tree are completely full except possibly the bottom-most. It operates in two phases. First, the tree is turned into a linked list by means of an in-order traversal, reusing the pointers in the (threaded) tree's nodes. A series of left-rotations forms the second phase.[3] The Stout–Warren modification generates a complete binary tree, namely one in which the bottom-most level is filled strictly from left to right. This is a useful transformation to perform if it is known that no more inserts will be done. It does not require the tree to be threaded, nor does it require more than constant space to operate.[1] Like the original algorithm, Day–Stout–Warren operates in two phases, the first entirely new, the second a modification of Day's rotation phase.[1][3]

A 2002 article by Timothy J. Rolfe brought attention back to the DSW algorithm;[3] the naming is from the section title "6.7.1: The DSW Algorithm" in Adam Drozdek's textbook.[4] Rolfe cites two main advantages: "in circumstances in which one generates an entire binary search tree at the beginning of processing, followed by item look-up access for the rest of processing" and "pedagogically within a course on data structures where one progresses from the binary search tree into self-adjusting trees, since it gives a first exposure to doing rotations within a binary search tree."

Pseudocode

edit

The following is a presentation of the basic DSW algorithm in pseudocode, after the Stout–Warren paper.[1][note 1] It consists of a main routine with three subroutines. The main routine is given by

  1. Allocate a node, the "pseudo-root", and make the tree's actual root the right child of the pseudo-root.
  2. Call tree-to-vine with the pseudo-root as its argument.
  3. Call vine-to-tree on the pseudo-root and the size (number of elements) of the tree.
  4. Make the tree's actual root equal to the pseudo-root's right child.
  5. Dispose of the pseudo-root.

The subroutines are defined as follows:[note 2]

routine tree-to-vine(root)
    // Convert tree to a "vine", i.e., a sorted linked list,
    // using the right pointers to point to the next node in the list
    tail ← root
    rest ← tail.right
    while rest ≠ nil
        if rest.left = nil
            tail ← rest
            rest ← rest.right
        else
            temp ← rest.left
            rest.left ← temp.right
            temp.right ← rest
            rest ← temp
            tail.right ← temp
routine vine-to-tree(root, size)
    leaves ← size + 1 − 2⌊log2(size + 1)⌋
    compress(root, leaves)
    size ← size − leaves
    while size > 1
        compress(root, ⌊size / 2⌋)
        size ← ⌊size / 2⌋
routine compress(root, count)
    scanner ← root
    for i ← 1 to count
        child ← scanner.right
        scanner.right ← child.right
        scanner ← scanner.right
        child.right ← scanner.left
        scanner.left ← child

Notes

edit
  1. ^ This version does not produce perfectly balanced nodes; Stout and Warren present a modification that does, in which the first call to compress is replaced by a different subroutine.
  2. ^ In the original presentation, tree-to-vine computed the tree's size as it went. For the sake of brevity, we assume this number to be known in advance.

References

edit
  1. ^ a b c d Stout, Quentin F.; Warren, Bette L. (September 1986). "Tree rebalancing in optimal space and time" (PDF). Communications of the ACM. 29 (9): 902–908. doi:10.1145/6592.6599. hdl:2027.42/7801. S2CID 18599490.
  2. ^ Day, A. Colin (1976). "Balancing a Binary Tree". Comput. J. 19 (4): 360–361. doi:10.1093/comjnl/19.4.360.
  3. ^ a b c Rolfe, Timothy J. (December 2002). "One-Time Binary Search Tree Balancing: The Day/Stout/Warren (DSW) Algorithm". SIGCSE Bulletin. 34 (4). ACM SIGCSE: 85–88. doi:10.1145/820127.820173. S2CID 14051647.{{cite journal}}: CS1 maint: deprecated archival service (link)
  4. ^ Drozdek, Adam (1996). Data Structures and Algorithms in C++. PWS Publishing Co. pp. 173–175. ISBN 0-534-94974-6.

📚 Artikel Terkait di Wikipedia

Tree rotation

performing a tree rotation on it does not change the final result. The Day–Stout–Warren algorithm balances an unbalanced BST. Tamari lattice, a partially ordered

DSW

an international nongovernmental organization based in Germany Day–Stout–Warren algorithm for balancing binary search trees dsw (command), an obsolete Unix

Self-balancing binary search tree

optimize database queries or other list-processing algorithms. Search data structure Day–Stout–Warren algorithm Fusion tree Skip list Sorting Donald Knuth. The

Donald Trump and fascism

event as fascistic. Elizabeth Warren stated that "it's looking more and more like a fascist state out there every day", and Scott Wiener stated that

Andrew Tate

has been described by experts as a "blatant attempt to manipulate the algorithm" and artificially boost his content. Magee, Caolán (27 January 2023).

List of Google Easter eggs

it )" states, "418. I'm a teapot. The requested entity body is short and stout. Tip me over and pour me out." Clicking on the picture of the teapot or

List of Massachusetts Institute of Technology alumni

dropout) – founder of Instabase Katie Bouman (PhD 2017) – developer of the algorithm used in filtering the first images of a black hole taken by the Event

Largest prehistoric animals

doi:10.1038/s42003-025-07653-4. ISSN 2399-3642. PMC 12018936. PMID 40269118. Stout, Jeremy B. (23 October 2020). "New early Pleistocene Alligator (Eusuchia: