README for splay_set (Ling Li, Caltech, Jan. 16, 2001) ---------------------------------------------------------------------- Splay trees, due to Sleator and Tarjan, are binary trees where often accessed items tend to be near the root. Operations doing a splay are create, mem, insert and delete. This implementation of splay uses in-place modification of the tree. ---------------------------------------------------------------------- Main idea of splay: The splay operation is not equivalent to rotating an node all the way to the root using single rotations. Instead, double rotations (together with single rotation) are used here. Most of the time the node will be moved to the root by double rotations. Only when the depth of the accessed node is odd (if we define the depth of the root as 0), a single rotation at the end is needed. A path consisting of the nodes and directions from the root to the node being accessed (thus has the depth information) is useful to the splay operation. However, we can do splay without explicitly constructing a path, since the path information is already contained in the recursion of splay. The only reason that we need a path is we don't know how to recursively run splay before we know the depth of the node. OK, if moving the node to the root is not insisted, we may do the job in one recursion. In splay_set.ml, a function that moves the accessed node to the root OR the left/right child of the root, which is named splay2, is used. It also provides the direction (Left or Right) information, or strictly, the position of the node asked after splay2 (Root/Left/Right), to its caller. Then the recursion is easy. First, splay2 checks whether the node asked is already in its root, or if the tree is too simple (such as a Leaf). If so, then return the original tree, and the position information 'Root'. Otherwise, it calls itself to splay2 its left or right subtree. If the last call returns that the node is in the root (of the left/right subtree), nothing need to do and just return the position of the node (Left/Right). If the node is also in the left or right subtree of the subtree, then a double rotation is carried out, and 'Root' is returned. Splay2 uses only the double rotations to lift an accessed node. So we still need another function (splay) to do a single rotation, if needed, to complete the splay operation. ---------------------------------------------------------------------- Files: splay_set.mli: interface of the splay_set module splay_set.ml : implementation of the splay_set test.ml : program that tests the splay_set Makefile : used by make (under Linux) Compiling: make : make a executable test program make clean : clean all stuffs except sources and Makefile.dep Test commands: ?, help : help insert : insert string delete : delete string mem : give the membership of bye, quit : quit ---------------------------------------------------------------------- Interface of the splay_set: MakeSplaySet is called with an ordered type (EltSig) to produce a splay set. The ordered type should have an element type 'elt' and a comparison function 'compare' with standard output. ---------------------------------------------------------------------- Methods in the splay_set: splay2: Lift x by using only zig(zag)-zig(zag) rotations This is a recursive function. It finds x or the one adjacent to x in the splay tree, and then lift it to the root or to the left or right node of the root, using only zig(zag)-zig(zag) rotations. This is a sub-routine of splay. splay: Lift x to the root By first calling splay2 then making a zig or zag rotation, if necessary, splay lifts the target node to the root. join: Join two splay trees S and S' Assuming that all items in S are smaller than x, and all items in S' are larger than x, join makes S the left subtree of the minimum node of S'. However, to find the minimum node of S', a value x is used here acting as -Infinity. The minimum node of S' is lifted to the root. This is called by delete. create: Make an empty tree mem: Membership function After splaying x from the tree, mem tests the membership of x by judging the value of the root. insert: Insert an item x to the splay tree Splaying the tree gives a chance to split the tree into two parts, one smaller than x and one larger than x, is x is not in the tree. The two parts can be made the left and right sub- trees of a new root with value x. delete: Delete an item x from the splay tree Splay x from the tree, then join the left and right subtrees of x, if x is in the tree. ---------------------------------------------------------------------- This file together with the programs of splay_set can be found at http://www.cs.caltech.edu/~ling/course/cs134b/lab1