Day–Stout–Warren algorithm
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 their 1986 paper Tree Rebalancing in Optimal Time and Space, based on work done by Colin Day in 1976.
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. 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.
A 2002 article by Timothy J. Rolfe has recently brought attention back to the DSW algorithm after a long hiatus; the naming is from the section title "6.7.1: The DSW Algorithm" in Adam Drozdek's Data Structures and Algorithms in C++ (PWS Publishing Co., 1996) pp. 173–175. 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."
External links[edit]
- An explanation and some experiments by Timothy J. Rolfe
- The original paper by Stout and Warren
- Prof. Quentin Stout's homepage at University of Michiga
http://web.eecs.umich.edu/~qstout/pap/CACM86.pdf
Tree Rebalancing in Optimal Time and Space
ABSTRACT: A simple algorithm is given which takes an arbitrary binary search tree and rebalances it to form another of optimal shape, using time linear in the number of nodes and only a constant amount of space (beyond that used to store the initial tree). This algorithm is therefore optimal in its use of both time and space. Previous algorithms were optimal in at most one of these two measures, or were not applicable to all binary search trees. When the nodes of the tree are stored in an array, a simple addition to this algorithm results in the nodes being stored in sorted order in the initial portion of the array, again using linear time and constant space.
1. INTRODUCTION
A binary search tree is an efficient and widely used structure to maintain ordered data. Because the fundamental operations of insertion, deletion, and searching require accessing nodes along a single path from the root, for randomly generated trees of n nodes (using the standard insertion algorithm), the expected time to perform each of these operations is @log(n)) [5](随机插入二叉树的高度CLRS有证明). Unfortunately, it is possible for a binary tree to have very long branches, and the worst-case time is 8(n). Further, there is experimental evidence that if a tree is grown as a long intermixed sequence of random insertions and deletions(随机插入删除,高度非对数级?), as opposed to just insertions, then the expected time is worse than logarithmic [4].
( b, c, d, and e are perfectly bal- anced, but all six are route balanced.)
To avoid the worst-case linear time it is necessary to keep the tree balanced, that is, the tree should not be allowed to have unnecessarily long branches. This problem has been studied intensely, and there are many notions of balance and balancing strate- gies, such as AVL trees, weight-balanced trees, self- organizing trees, etc. [5]. Here we are concerned with perhaps the simplest strategy; periodically re- balance the entire tree into an equivalent tree of optimal shape. This strategy has been discussed by many authors, and several algorithms have been presented [l, 3, 61; recently Chang and Iyengar [Z] surveyed this work and presented additional algo- rithms. No previous algorithm could rebalance an arbitrary binary search tree in time linear in the number of nodes, while using only a fixed amount of additional space beyond that originally occupied by the tree. The main result of this article is a simple algorithm which accomplishes this.
One notion of “optimal shape” used in rebalancing trees is that of perfect balance, which requires that at each node p, the number of nodes in p’s left subtree differs by no more than 1 from the number of nodes in p’s right subtree. It is easy to see that in a per- fectly balanced tree of n nodes the maximum depth of the nodes is Llg(n)J, and for each depth 0 5 d < tlg(n)J there are exactly 2d nodes at depth d.. The depth of a node is the number of links which must be traversed in traveling from the root to the node. The depth of the root is 0, and the children of a node of depth d have depth d + 1.) Using these properties, it is also easy to show that, among all binary trees of n nodes, perfectly balanced trees minimize the maximum depth of the nodes and minimize the average depth of the nodes(最小化深度最大值和平均值,). Therefore perfectly balanced trees have the best pos- sible worst-case time and the best possible expected case time for each standard tree operation.
However, perfectly balanced trees are not the larg- est class of trees with all these properties. A binary tree with n nodes, where all nodes are at depth Llg(n)J or less, and where there are exactly zd nodes at depth d for each depth 0 <= d < Llg(n)l, will be called route balanced. Route balanced trees are pre- cisely those binary trees which minimize the maxi- mum depth of the nodes and minimize the average depth of the nodes. Every perfectly balanced tree is route balanced, but not vice-versa(路径平衡弱于最优平衡). For example, in Figure I, only trees b, c, d, and e are perfectly bal- anced, but all six are route balanced. With the ex- ception of Day [3], p revious authors concentrated on creating perfectly balanced trees. Although perfect balancing fits naturally into a top-down approach, we know of no reason to prefer a perfectly balanced tree over a route balanced tree, and our basic algo- rithm creates route balanced trees. If for some rea- son a perfectly balanced tree is needed. then a modi- fied version of our basic algorithm, still requiring only linear time and constant additional space, can produce it. No previous algorithm produces a per- fectly balanced tree using only constant additional space