I am trying to implement a recursive splay tree, bottom up. I recurse down to the node I am need to splay up, and I find the parent and grandparent of that node. Then I am able to either zig zag or zig zig depending on the situation just fine. The problem is after this is done, I return the node which has been splayed once to the previous recursive call. The previous recursive call is referenced to the parent of the node, which is now the child of that node. How do I recurse up splaying the node as I go up?
Asked
Active
Viewed 1,505 times
3
-
What is the difference between a recursive splay tree and a normal one? – Svante Mar 03 '10 at 20:29
-
recursive means the node is splayed from the bottom up, recursively. a normal one means splays a node up from the top down – Andrew Mar 03 '10 at 22:10
2 Answers
1
If I recall correctly, you build a left and right tree as you recurse down to the target node. When you find the target, you construct the final left and right trees using the (original) children of the target, attach the resulting trees as the new children of the target, and return the result in tail-recursive fashion (i.e., all the way back up the stack without further modification).

comingstorm
- 25,557
- 3
- 43
- 67
0
When the tree gets totally "unbalanced" and really large (say as example 100000 int keys) then recursive algorithms may throw stackoverflow exception. Better to use a parent pointer in each node to get the parent or grand parent. This is one way of doing it. worked fine for me.
The results on the runtime are obvious heresplay tree runtime profiling

Community
- 1
- 1