Recursive Splay Tree

99封情书 提交于 2019-12-22 10:30:54

问题


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?


回答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).




回答2:


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



来源:https://stackoverflow.com/questions/2374006/recursive-splay-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!