I have been studying Splay trees because I would like to implement one. Currently, I have some “autodidactic” experience with Red-Black trees, AVL trees, Skip lists and other simpler data structures. I want to implement my first splay tree, but I want a recursive implementation for it, if possible (I love recursion).
However, I think it’s difficult because you have to see two levels down the tree to observe all the possible cases (zig-zag, zig-zig, zar), and there is no way to mark the target without another field. Should I use another field, like in red-black trees, to mark the visited nodes and splay the target node?
It’s easy enough to use a recursive algorithm, and it might work out looking fairly clean. No marking is necessary. Remember that the splay operation (which is used for find, insert and delete) brings the target node to the top of the tree; in other words, it returns the (splayed) tree with the target node at the top.
In essence, you need to decide from a given node what the next two moves will be (left-left, right-right, or anything else.) The rotation happens when you go the same direction twice.
There’s a nice implementation for functional languages in Chris Okasaki’s Purely Functional Data Structures, which imho is one of the finest short CS texts in existence.