home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
misc
/
volume35
/
m2-splay22
/
part01
/
README
next >
Wrap
Text File
|
1993-01-20
|
4KB
|
138 lines
SPLAY TREE LIBRARY Version 2.2 930119
=====================================
This library contains the following files:
splay.def Definition module for the splay tree library
splay.mod Implementation of splay tree
splayItem.def Definition module for data stored in the tree
splayItem.mod Implementation module for data stored in
the tree
splayTest.mod A short test program
For a full introduction to splay trees see
Sleator D. and Tarjan R. "Self adjusting
binary trees", JACM Vol 32. No 3, 1985, pp 652-
686.
Changes since version 2.1
=========================
Changed updating of weight information so the basic operations
now remain in O(lgn) instead of O(n).
Introduction
============
In short, splay trees are a form of balanced binary trees which
moves every accessed node to the root. This means that the tree
will behave very well when there is some form of locality in the
data processed. Furthermore it can be shown that the amortized
access cost is O(lgn) for the basic operations (insert, delete and
find).
The splay tree also has the nice property that an item accessed
t operations ago can be located in O(lgt) time.
All in all practical tests have shown splay trees to be an excellent
substitution for the more well known r-b-trees or some other variations
on balanced trees.
Since modula2 lacks possibilities to create generic modules my
approach is to provide a separate module which gives the operations
and type of the element stored in the tree. This module
(here called splayItem) must support operations to create, destroy
and compare elements. This scheme provides for a fairly generic
implementation, (see the test program and splayItem.[mod,def])
In the test program supplied the create and destroy routines
are empty since the objects doesn't use any dynamic memory. The
prize to pay for the generic structure is a little overhead for
each comparison.
If speed is essential (as always) you may hard code the comparison
between elements in the implementation if you are only working
with simple types like integers or something like that. Or you
might want to rewrite the code to handle the more classic
variation with a key and a generic pointer stored in each
node. These changes are trivial and shouldn't take long
time to do.
Rank
====
This implementation includes management of the rank of elements, i.e
their ordering in an in-order tree traversal. To maintain the rank operation
in O(lgn) time for an element it's necessary to do some extra work
after each basic operation. If you are not interested in maintaining
rank of the elements you may delete the code for maintaining the weight
(number of nodes in subtrees for each node) of each node.
This is basically a matter of deleting most of the code at the end of
each basic operation.
Since maintaining the rank requires an explicit stack whos size
must be decided at run time it's possible to get a checked run time
error if the tree grows to large for the stack. This shouldn't pose
a problem since the required stack size is in order O(lgn) where
n is the number of elements in the tree, and the default stack quite
big.
Portability
===========
There are basically two things you may want to change.
1. The first are the routines used for I/O to
print a string, cardinal, int and so on. I have used
our local library routines. Change these to your own
favorites.
2. Error handling are done by a call to error.raise() which
again is a local library function. It calls the topmost
function in an error stack with the string supplied as
argument. You probably want to change this to your own
favorite way of handling errors.
Todo
====
* Make two versions one with and one without rank operations.
* Dynamic stack to avoid any run time errors
Test program
============
Supplied with this distribution is a test program called
splayTest (what else?) it performs some basic tests at first
and then starts an exhaustive test until any errors have been
encountered or stopped by a CTRL-C.
Bugs
====
To the best of my knowledge this code is bug free. But if you
do discover some irregularities please drop me a note.
Standard disclaimer: This code is put in public domain and
distributed as is. You may use this code commercially or
otherwise but I won't take any responsibility whatsoever.
Johan Persson (ljp@sm.luth.se)
----
Johan Persson E-mail: ljp@sm.luth.se
Dept. of Computer Science
Technical University of Lulea
S-951 87 Lulea