Jonathan E. Landrum 98472f16c0 Save State | 8 anni fa | |
---|---|---|
src | 8 anni fa | |
.gitattributes | 9 anni fa | |
.gitignore | 9 anni fa | |
LICENSE | 8 anni fa | |
README.md | 8 anni fa | |
tree-rotation.gif | 8 anni fa |
This is a Java implementation of a Splay Tree. It is utilized in a fictional "recent contacts" program similar to the one used in Android's starred contacts list.
Splay Trees are Binary Search Trees with an added property that any time an element is accessed (search, add, delete) it is then migrated to the root of the tree by way of left– and right–rotations:
This keeps the most recently used items near the top of the tree, at the expense of keeping the tree quite degenerate"). The tree is called a Splay Tree because of how stretched out it can usually get. Nonetheles, because of the principle of locality, the operations search, insert, and delete all operate on the order of O(lg") n) amortized time, even though the true worst case is O(n) for all three.
For example, if you want a Splay Tree of type String
, add this line in your program:
SplayTree<String> tree = new SplayTree<String>();
SplayTree()
, SplayTree(T d)
, or SplayTree(Node<T> n)
addElement(T d)
or addElement(Node<T> n)
findElement(T d)
or findElement(Node<T> n)
removeElement(T d)
or removeElement(Node<T> n)
Look at the contents of the file RecentContacts.java for an example use of each of these methods.
Also included in this program are the files used for testing the public methods of the various classes. I built this program using Eclipse and tested it with JUnit. If you wish to run these unit tests, you will need to have JUnit 4+ installed.