Smalltalk: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Be..anyone
+Object REXX (1988)
en>Diego Moya
Syntax: what "syntax fits in a postcard" means - it's not the language syntactic specification, but an example
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Use dmy dates|date=July 2012}}
The author's name is Oswaldo but he never really liked that specify. Distributing production is my profession but soon I'll be on [https://www.google.com/search?hl=en&gl=us&tbm=nws&q=personalized&btnI=lucky personalized]. My [http://Thesaurus.com/browse/family+lives family lives] in New Mexico but I'm going to have to keep in pa or a couple. One of the proper things on the earth for her is to attempt to do ceramics but she can't make it her sector. His wife and he maintain a site. You might want to check it out: http://www.rok-i.com/Textcube/guestbook/<br><br>Also visit my web blog; franklin plumbing plano, [http://www.rok-i.com/Textcube/guestbook/ http://www.rok-i.com/],
{{Infobox data structure
|name=Splay tree
|type=tree
|invented_by=[[Daniel Dominic Sleator]] and [[Robert Endre Tarjan]]
|invented_year=1985
|space_avg=O(n)
|space_worst=O(n)
|search_avg=O(log n)
|search_worst=amortized O(log n)
|insert_avg=O(log n)
|insert_worst=amortized O(log n)
|delete_avg=O(log n)
|delete_worst=amortized O(log n)
}}
 
A '''splay tree''' is a self-adjusting [[binary search tree]] with the additional property that recently accessed elements are quick to access again.  It performs basic operations such as insertion, look-up and removal in [[big O notation|O]](log n) [[amortized analysis|amortized]] time. For many sequences of non-random operations, splay trees perform better than other search trees, even when the specific pattern of the sequence is unknown.  The splay tree was invented by [[Daniel Dominic Sleator]] and [[Robert Endre Tarjan]] in 1985.<ref name="SleatorTarjan" />
 
All normal operations on a binary search tree are combined with one basic operation, called ''splaying''. Splaying the tree for a certain element rearranges the tree so that the element is placed at the root of the tree.  One way to do this is to first perform a standard binary tree search for the element in question, and then use [[tree rotation]]s in a specific fashion to bring the element to the top. Alternatively, a top-down algorithm can combine the search and the tree reorganization into a single phase.
 
== Advantages ==
Good performance for a splay tree depends on the fact that it is self-optimizing, in that frequently accessed nodes will move nearer to the root where they can be accessed more quickly. The worst-case height—though unlikely—is O(n), with the average being O(log ''n'').
Having frequently used nodes near the root is an advantage for nearly all practical applications (also see [[Locality of reference]]),{{Citation needed|date=January 2010}} and is particularly useful for implementing [[cache (computing)|cache]]s and [[Garbage collection (computer science)|garbage collection]] algorithms.
 
Advantages include:
* Simple implementation—simpler than other self-balancing binary search trees, such as [[red-black tree]]s or [[AVL tree]]s.
* Comparable performance—[[average-case performance]] is as efficient as other trees.{{Citation needed|date=January 2010}}
* Small memory footprint—splay trees do not need to store any bookkeeping data.
* Possibility of creating a [[persistent data structure]] version of splay trees—which allows access to both the previous and new versions after an update. This can be useful in [[functional programming]], and requires amortized O(log ''n'') space per update.
* Working well with nodes containing identical keys—contrary to other types of self-balancing trees. Even with identical keys, performance remains amortized O(log ''n''). All tree operations preserve the order of the identical nodes within the tree, which is a property similar to [[Sorting algorithm#Classification|stable sorting algorithms]]. A carefully designed find operation can return the leftmost or rightmost node of a given key.
 
== Disadvantages ==
The most significant disadvantage of splay trees is that the height of a splay tree can be linear.  For example, this will be the case after accessing all ''n'' elements in non-decreasing order.  Since the height of a tree corresponds to the worst-case access time, this means that the actual cost of an operation can be high. However the [[amortized]] access cost of this worst case is logarithmic, O(log ''n'').  Also, the expected access cost can be reduced to O(log ''n'') ''by using a randomized variant.<ref>{{cite web|title=Randomized Splay Trees: Theoretical and Experimental Results|url=http://www2.informatik.hu-berlin.de/~albers/papers/ipl02.pdf|accessdate=31 May 2011}}</ref>
 
A splay tree can be worse than a static tree by at most a constant factor.
 
The representation of splay trees can change even when they are accessed in a 'read-only' manner (i.e. by ''find'' operations).  This complicates the use of such splay trees in a multi-threaded environment.  Specifically, extra management is needed if multiple threads are allowed to perform ''find'' operations concurrently.
 
==Operations==
 
===Splaying===
When a node ''x'' is accessed, a splay operation is performed on ''x'' to move it to the root. To perform a splay operation we carry out a sequence of ''splay steps'', each of which moves ''x'' closer to the root. By performing a splay operation on the node of interest after every access, the recently accessed nodes are kept near the root and the tree remains roughly balanced, so that we achieve the desired amortized time bounds.
 
Each particular step depends on three factors:
* Whether ''x'' is the left or right child of its parent node, ''p'',
* whether ''p'' is the root or not, and if not
* whether ''p'' is the left or right child of ''its'' parent, ''g'' (the ''grandparent'' of x).
 
It is important to remember to set ''gg'' (the ''great-grandparent'' of x) to now point to x after any splay operation. If ''gg'' is null, then x obviously is now the root and must be updated as such.
 
There are three types of splay steps, each of which has a left- and right-handed case. For the sake of brevity, only one of these two is shown for each type. These three types are:
 
'''Zig Step:''' This step is done when ''p'' is the root. The tree is rotated on the edge between ''x'' and ''p''.  Zig steps exist to deal with the parity issue and will be done only as the last step in a splay operation and only when ''x'' has odd depth at the beginning of the operation.
 
[[File:splay tree zig.svg|center]]
 
'''Zig-zig Step:''' This step is done when ''p'' is not the root and ''x'' and ''p'' are either both right children or are both left children. The picture below shows the case where ''x'' and ''p'' are both left children. The tree is rotated on the edge joining ''p'' with ''its'' parent ''g'', then rotated on the edge joining ''x'' with ''p''. Note that zig-zig steps are the only thing that differentiate splay trees from the ''rotate to root'' method introduced by Allen and Munro<ref name="AllenMunro">{{Citation |author=Allen, Brian; and Munro, Ian |title=Self-organizing search trees |journal=Journal of the ACM |volume=25 |pages=526–535 |year=1978 |issue=4 |doi=10.1145/322092.322094 }}</ref> prior to the introduction of splay trees.
 
[[Image:Zigzig.gif|center]]
 
'''Zig-zag Step:''' This step is done when ''p'' is not the root and ''x'' is a right child and ''p'' is a left child  or vice versa. The tree is rotated on the edge between ''p'' and x, and then rotated on the resulting edge between ''x'' and g.
 
[[Image:Zigzag.gif|center]]
 
===Insertion===
To insert a node ''x'' into a splay tree:
#First insert the node as with a normal [[binary search tree]].
#Then splay the newly inserted node ''x'' to the top of the tree.
 
===Deletion===
To delete a node ''x'', we use the same method as with a binary search tree:  if ''x'' has two children, we swap its value with that of either the rightmost node of its left sub tree (its in-order predecessor) or the leftmost node of its right subtree (its in-order successor). Then we remove that node instead. In this way, deletion is reduced to the problem of removing a node with 0 or 1 children.
 
Unlike a binary search tree, in a splay tree after deletion, we splay the parent of the removed node to the top of the tree.
'''OR'''
The node to be deleted is first splayed, i.e. brought to the root of the tree and then deleted. This leaves the tree with two sub trees. The maximum element of the left sub tree (''': METHOD 1'''), or minimum of the right sub tree (''': METHOD 2''') is then splayed to the root. The right sub tree is made the right child of the resultant left sub tree (for '''METHOD 1'''). The root of left sub tree is the root of melded tree.
 
== Implementation ==
Below there is an implementation of splay trees in C++, which uses pointers to represent each node on the tree. This implementation is based on the second method of deletion on a splay tree.  Also, unlike the above definition, this C++ version does ''not'' splay the tree on finds - it only splays on insertions and deletions.
 
<source lang="cpp">
#include <functional>
 
#ifndef SPLAY_TREE
#define SPLAY_TREE
 
template< typename T, typename Comp = std::less< T > >
class splay_tree {
private:
  Comp comp;
  unsigned long p_size;
 
  struct node {
    node *left, *right;
    node *parent;
    T key;
    node( const T& init = T( ) ) : left( 0 ), right( 0 ), parent( 0 ), key( init ) { }
  } *root;
 
  void left_rotate( node *x ) {
    node *y = x->right;
    x->right = y->left;
    if( y->left ) y->left->parent = x;
    y->parent = x->parent;
    if( !x->parent ) root = y;
    else if( x == x->parent->left ) x->parent->left = y;
    else x->parent->right = y;
    y->left = x;
    x->parent = y;
  }
 
  void right_rotate( node *x ) {
    node *y = x->left;
    x->left = y->right;
    if( y->right ) y->right->parent = x;
    y->parent = x->parent;
    if( !x->parent ) root = y;
    else if( x == x->parent->left ) x->parent->left = y;
    else x->parent->right = y;
    y->right = x;
    x->parent = y;
  }
 
  void splay( node *x ) {
    while( x->parent ) {
      if( !x->parent->parent ) {
        if( x->parent->left == x ) right_rotate( x->parent );
        else left_rotate( x->parent );
      } else if( x->parent->left == x && x->parent->parent->left == x->parent ) {
        right_rotate( x->parent->parent );
        right_rotate( x->parent );
      } else if( x->parent->right == x && x->parent->parent->right == x->parent ) {
        left_rotate( x->parent->parent );
        left_rotate( x->parent );
      } else if( x->parent->left == x && x->parent->parent->right == x->parent ) {
        right_rotate( x->parent );
        left_rotate( x->parent );
      } else {
        left_rotate( x->parent );
        right_rotate( x->parent );
      }
    }
  }
 
  void replace( node *u, node *v ) {
    if( !u->parent ) root = v;
    else if( u == u->parent->left ) u->parent->left = v;
    else u->parent->right = v;
    if( v ) v->parent = u->parent;
  }
 
  node* subtree_minimum( node *u ) {
    while( u->left ) u = u->left;
    return u;
  }
 
  node* subtree_maximum( node *u ) {
    while( u->right ) u = u->right;
    return u;
  }
public:
  splay_tree( ) : root( 0 ), p_size( 0 ) { }
 
  void insert( const T &key ) {
    node *z = root;
    node *p = 0;
   
    while( z ) {
      p = z;
      if( comp( z->key, key ) ) z = z->right;
      else z = z->left;
    }
   
    z = new node( key );
    z->parent = p;
   
    if( !p ) root = z;
    else if( comp( p->key, z->key ) ) p->right = z;
    else p->left = z;
   
    splay( z );
    p_size++;
  }
 
  node* find( const T &key ) {
    node *z = root;
    while( z ) {
      if( comp( z->key, key ) ) z = z->right;
      else if( comp( key, z->key ) ) z = z->left;
      else return z;
    }
    return 0;
  }
       
  void erase( const T &key ) {
    node *z = find( key );
    if( !z ) return;
   
    splay( z );
   
    if( !z->left ) replace( z, z->right );
    else if( !z->right ) replace( z, z->left );
    else {
      node *y = subtree_minimum( z->right );
      if( y->parent != z ) {
        replace( y, y->right );
        y->right = z->right;
        y->right->parent = y;
      }
      replace( z, y );
      y->left = z->left;
      y->left->parent = y;
    }
   
    delete z;
    p_size--;
  }
 
  const T& minimum( ) { return subtree_minimum( root )->key; }
  const T& maximum( ) { return subtree_maximum( root )->key; }
 
  bool empty( ) const { return root == 0; }
  unsigned long size( ) const { return p_size; }
};
 
#endif // SPLAY_TREE
</source>
 
== Analysis ==
A simple [[amortized analysis]] of static splay trees can be carried out using the [[potential method]]. Suppose that size(''r'') is the number of nodes in the subtree rooted at ''r'' (including ''r'') and rank(''r'') = log<sub>2</sub>(size(''r'')). Then the potential function P(''t'') for a splay tree ''t'' is the sum of the ranks of all the nodes in the tree. This will tend to be high for poorly balanced trees, and low for well-balanced trees. We can bound the amortized cost of any zig-zig or zig-zag operation by:
 
:amortized cost = cost + P(t<sub>f</sub>) - P(t<sub>i</sub>) &le; 3(rank<sub>f</sub>(''x'') - rank<sub>i</sub>(''x'')),
 
where ''x'' is the node being moved towards the root, and the subscripts "f" and "i" indicate after and before the operation, respectively. When summed over the entire splay operation, this [[telescoping series|telescopes]] to 3(rank(root)) which is O(log ''n''). Since there's at most one zig operation, this only adds a constant.
 
== Performance theorems ==
There are several theorems and conjectures regarding the worst-case runtime for performing a sequence ''S'' of ''m'' accesses in a splay tree containing ''n'' elements.
 
;Balance Theorem:<ref name="SleatorTarjan">{{Citation |first1=Daniel D. |last1=Sleator |first2=Robert E. |last2=Tarjan |title=Self-Adjusting Binary Search Trees |url=http://www.cs.cmu.edu/~sleator/papers/self-adjusting.pdf |journal=Journal of the ACM ([[Association for Computing Machinery]]) |volume=32 |issue=3 |pages=652–686 |year= 1985 |doi=10.1145/3828.3835 }}</ref>  The cost of performing the sequence ''S'' is <math>O\left[m(1 + \log n) + n\log n\right]</math>.  In other words, splay trees perform as well as static balanced binary search trees on sequences of at least ''n'' accesses.
;Static Optimality Theorem:<ref name="SleatorTarjan" />  Let <math>q_i</math> be the number of times element ''i'' is accessed in ''S''.  The cost of performing ''S'' is <math>O\left[m + \sum_{i=1}^n q_i\log\frac{m}{q_i}\right]</math>.  In other words, splay trees perform as well as optimum static binary search trees on sequences of at least ''n'' accesses.
;Static Finger Theorem:<ref name="SleatorTarjan" />  Let <math>i_j</math> be the element accessed in the <math>j^{th}</math> access of ''S'' and let ''f'' be any fixed element (the finger). The cost of performing ''S'' is <math>O\left[m + n\log n + \sum_{j=1}^m \log(|i_j-f| + 1)\right]</math>.
;Working Set Theorem:<ref name="SleatorTarjan" />  Let <math>t(j)</math> be the number of distinct elements accessed between access ''j'' and the previous time element <math>i_j</math> was accessed.  The cost of performing ''S'' is <math>O\left[m + n\log n + \sum_{j=1}^m \log(t(j) + 1)\right]</math>.
;Dynamic Finger Theorem:<ref name="ColeEtAl">{{Citation |author=Cole, Richard |coauthors=Mishra, Bud; Schmidt, Jeanette; and Siegel, Alan |title=On the Dynamic Finger Conjecture for Splay Trees. Part I: Splay Sorting log n-Block Sequences |journal=[[SIAM Journal on Computing]] |volume=30 |pages=1–43 |year=2000 }}</ref><ref name="Cole">{{Citation |author=Cole, Richard |title=On the Dynamic Finger Conjecture for Splay Trees. Part II: The Proof |journal=SIAM Journal on Computing |volume=30 |pages=44–85 |year=2000 |doi=10.1137/S009753979732699X }}</ref> The cost of performing ''S'' is <math>O\left[m + n + \sum_{j=1}^m \log(|i_{j + 1} - i_j| + 1)\right]</math>.
;Scanning Theorem:<ref name="Tarjan">{{Citation |author=Tarjan, Robert E. |title=Sequential access in splay trees takes linear time |journal=Combinatorica |volume=5 |pages=367–378 |year=1985 |doi=10.1007/BF02579253 |issue=4 }}</ref>  Also known as the '''Sequential Access Theorem'''.  Accessing the ''n'' elements of a splay tree in symmetric order takes O(''n'') time, regardless of the initial structure of the splay tree. The tightest upper bound proven so far is <math>4.5n</math>.<ref name="Elmasry">{{Citation |author=Elmasry, Amr |title=On the sequential access theorem and Deque conjecture for splay trees |journal=Theoretical Computer Science |volume=314 |pages=459–466 |year=2004 |issue=3 |doi=10.1016/j.tcs.2004.01.019 }}</ref>
 
== Dynamic optimality conjecture ==
{{unsolved|computer science|Do splay trees perform as well as any other binary search tree algorithm?}}
In addition to the proven performance guarantees for splay trees there is an unproven conjecture of great interest from the original Sleator and Tarjan paper.  This conjecture is known as the ''dynamic optimality conjecture'' and it basically claims that splay trees perform as well as any other binary search tree algorithm up to a constant factor.
 
:'''Dynamic Optimality Conjecture:<ref name="SleatorTarjan" />''' Let <math>A</math> be any binary search tree algorithm that accesses an element <math>x</math> by traversing the path from the root to <math>x</math> at a cost of <math>d(x)+1</math>, and that between accesses can make any rotations in the tree at a cost of 1 per rotation. Let <math>A(S)</math> be the cost for <math>A</math> to perform the sequence <math>S</math> of accesses.  Then the cost for a splay tree to perform the same accesses is <math>O[n + A(S)]</math>.
 
There are several corollaries of the dynamic optimality conjecture that remain unproven:
 
:'''Traversal Conjecture:<ref name="SleatorTarjan" />''' Let <math>T_1</math> and <math>T_2</math> be two splay trees containing the same elements. Let <math>S</math> be the sequence obtained by visiting the elements in <math>T_2</math> in preorder (i.e., depth first search order).  The total cost of performing the sequence <math>S</math> of accesses on <math>T_1</math> is <math>O(n)</math>.
 
:'''Deque Conjecture:<ref name="Tarjan" /><ref name="Pettie">{{Citation |author=Pettie, Seth |title=Splay Trees, Davenport-Schinzel Sequences, and the Deque Conjecture |journal=Proceedings of the 19th ACM-SIAM Symposium on Discrete Algorithms |pages=1115–1124 |year=2008 |bibcode=2007arXiv0707.2160P |volume=0707 |arxiv=0707.2160 }}</ref><ref name="Sundar">{{Citation |author=Sundar, Rajamani |title=On the Deque conjecture for the splay algorithm |journal=Combinatorica |volume=12 |pages=95–124 |year=1992 |issue=1 |doi=10.1007/BF01191208 }}</ref>''' Let <math>S</math> be a sequence of <math>m</math> [[double-ended queue]] operations (push, pop, inject, eject).  Then the cost of performing <math>S</math> on a splay tree is <math>O(m + n)</math>.
 
:'''Split Conjecture:<ref name="Lucas">{{Citation |author=Lucas, Joan M. |title=On the Competitiveness of Splay Trees: Relations to the Union-Find Problem |journal=Online Algorithms, Center for Discrete Mathematics and Theoretical Computer Science ([[DIMACS]]) Series in Discrete Mathematics and Theoretical Computer Science Vol. 7 |pages=95–124 |year=1991 }}</ref>''' Let <math>S</math> be any permutation of the elements of the splay tree.  Then the cost of deleting the elements in the order <math>S</math> is <math>O(n)</math>.
 
==See also==
* [[Finger tree]]
* [[Link/cut tree]]
* [[Scapegoat tree]]
* [[Zipper (data structure)]]
* [[tree data structure|Trees]]
* [[Tree rotation]]
* [[AVL tree]]
* [[B-tree]]
* [[T-tree]]
* [[List of data structures]]
* [[Iacono's working set structure]]
* [[Geometry of binary search trees]]
* [[Splaysort]], a sorting algorithm using splay trees
 
==Notes==
{{reflist}}
 
==References==
* [[Donald Knuth|Knuth, Donald]]. ''[[The Art of Computer Programming]]'', Volume 3: ''Sorting and Searching'', Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Page 478 of section 6.2.3.
 
== External links ==
* [http://www.nist.gov/dads/HTML/splaytree.html NIST's Dictionary of Algorithms and Data Structures: Splay Tree]
* [http://www.link.cs.cmu.edu/link/ftp-site/splaying/ Implementations in C and Java (by Daniel Sleator)]
* (link is broken: page not found) [http://wiki.algoviz.org/AlgovizWiki/SplayTrees Pointers to splay tree visualizations]
* [http://github.com/fbuihuu/libtree Fast and efficient implentation of Splay trees]
* [http://github.com/cpdomina/SplayTree Top-Down Splay Tree Java implementation]
* [http://arxiv.org/abs/1003.0139 Zipper Trees]
* [http://www.youtube.com/watch?v=G5QIXywcJlY splay tree video]
 
{{CS-Trees}}
{{Data structures}}
 
{{DEFAULTSORT:Splay Tree}}
[[Category:Binary trees]]

Latest revision as of 06:40, 9 January 2015

The author's name is Oswaldo but he never really liked that specify. Distributing production is my profession but soon I'll be on personalized. My family lives in New Mexico but I'm going to have to keep in pa or a couple. One of the proper things on the earth for her is to attempt to do ceramics but she can't make it her sector. His wife and he maintain a site. You might want to check it out: http://www.rok-i.com/Textcube/guestbook/

Also visit my web blog; franklin plumbing plano, http://www.rok-i.com/,