Day–Stout–Warren Algorithm
   HOME

TheInfoList



OR:

The Day–Stout–Warren (DSW) algorithm is a method for efficiently balancing
binary search tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a Rooted tree, rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left ...
s 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 In computer science, a self-balancing binary search tree (BST) is any node-based binary search tree that automatically keeps its height (maximal number of levels below the root) small in the face of arbitrary item insertions and deletions.Donald ...
, 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, 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. It operates in two phases. First, the tree is turned into a
linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whi ...
by means of an
in-order traversal In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure, exactly once. S ...
, reusing the pointers in the ( threaded) tree's nodes. A series of left-rotations forms the second phase. 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. Like the original algorithm, Day–Stout–Warren operates in two phases, the first entirely new, the second a modification of Day's rotation phase. A 2002 article by Timothy J. Rolfe brought attention back to the DSW algorithm; the naming is from the section title "6.7.1: The DSW Algorithm" in Adam Drozdek's textbook. 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

The following is a presentation of the basic DSW algorithm in pseudocode, after the Stout–Warren paper.This version does not produce perfectly balanced nodes; Stout and Warren present a modification that does, in which the first call to is replaced by a different subroutine. It consists of a main routine with three
subroutine In computer programming, a function (also procedure, method, subroutine, routine, or subprogram) is a callable unit of software logic that has a well-defined interface and behavior and can be invoked multiple times. Callable units provide a ...
s. The main routine is given by # Allocate a node, the "pseudo-root", and make the tree's actual root the right child of the pseudo-root. # Call ''tree-to-vine'' with the pseudo-root as its argument. # Call ''vine-to-tree'' on the pseudo-root and the size (number of elements) of the tree. # Make the tree's actual root equal to the pseudo-root's right child. # Dispose of the pseudo-root. The subroutines are defined as follows: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. 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


References

{{DEFAULTSORT:Day-Stout-Warren algorithm Search trees Amortized data structures