证明二叉树中重复调用 successor() 的效率?

发布于 2024-12-20 15:38:58 字数 151 浏览 5 评论 0原文

我需要 CLRS 算法书中关于此练习的提示:

证明无论我们从高度为 h 的二叉搜索树中的哪个节点开始,k 次连续调用 Tree-Successor 都会花费 O(k+h) 时间.

I need a hint for this exercise from the CLRS Algorithms book:

Prove that no matter what node we start at in a height-h binary search tree, k successive calls to Tree-Successor take O(k+h) time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-12-27 15:38:58
  • 连续调用 TREE-SUCCESSOR 后,设 x 为起始节点,z 为结束节点。
  • pxz 之间的简单路径(包含)。
  • yp 访问的 xz 的共同祖先。
  • p的长度最多为2h,即O(h)
  • output 为其值在x.keyz.key 之间的元素。
  • 输出的大小为O(k)
  • 在执行k次连续调用TREE-SUCCESSOR时,
    访问 p 中的节点,
    除了节点 x、y 和 z 之外,
    如果访问p中节点的子树,那么它的所有元素都在output中。
  • 所以运行时间是O(h+k)
  • Let x be the starting node and z be the ending node after k successive calls to TREE-SUCCESSOR.
  • Let p be the simple path between x and z inclusive.
  • Let y be the common ancestor of x and z that p visits.
  • The length of p is at most 2h, which is O(h).
  • Let output be the elements that their values are between x.key and z.key inclusive.
  • The size of output is O(k).
  • In the execution of k successive calls to TREE-SUCCESSOR,
    the nodes that are in p are visited,
    and besides the nodes x, y and z,
    if a sub tree of a node in p is visited then all its elements are in output.
  • So the running time is O(h+k).
谜兔 2024-12-27 15:38:58

提示:举一个小例子,观察结果,尝试推断原因。

首先,需要考虑以下一些事项。

从某个节点开始,连续 k 次调用 Tree-Succcesor 构成部分树遍历。这次步行访问了多少个(至少和最多)节点? (提示:考虑 key(x))。请记住,一条边最多被访问两次(为什么?)。

最后提示:结果是O(2h+k)

Hint: work out a small example, observe the result, try to extrapolate the reason.

To get started, here are some things to consider.

Start at a certain node, k succesive calls to Tree-Succcesor consititutes a partial tree walk. How many (at least and at most) nodes does this walk visit? (Hint: Think about key(x)). Keep in mind that an edge is visited at most twice (why?).

Final hint: The result is O(2h+k).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文