The following is a presentation of the basic DSW algorithm in pseudocode, after the Stout–Warren paper.910 It consists of a main routine with three subroutines. The main routine is given by
The subroutines are defined as follows:11
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. http://www.eecs.umich.edu/~qstout/pap/CACM86.pdf ↩
Day, A. Colin (1976). "Balancing a Binary Tree". Comput. J. 19 (4): 360–361. doi:10.1093/comjnl/19.4.360. https://doi.org/10.1093%2Fcomjnl%2F19.4.360 ↩
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. Archived from the original on 2012-12-13. http://penguin.ewu.edu/~trolfe/DSWpaper/ ↩
Drozdek, Adam (1996). Data Structures and Algorithms in C++. PWS Publishing Co. pp. 173–175. ISBN 0-534-94974-6. 0-534-94974-6 ↩
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. ↩
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. ↩