查找最大整数小于最大键,但在AVL树中却不

发布于 2025-01-26 22:25:14 字数 132 浏览 3 评论 0原文

给定avl树T,我需要找到比树中最大钥匙小的最大整数,但不在树上。

我已经写了辅助功能来获得最大的节点,但我的想法被卡住了。

我尝试查看最大节点的排名及其继任者的等级,但没有任何帮助。

任何想法都会有所帮助!

Given AVL tree T, I need to find the maximal integer which is smaller than maximal key in tree, but is not in tree.

I've written auxiliary functions to get the biggest node, yet my thoughts are stuck.

I tried looking at the rank of the maximal node and the rank of it's successor yet that doesn't do any help.

Any ideas would be really helpful!

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

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

发布评论

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

评论(2

戏剧牡丹亭 2025-02-02 22:25:14

总体想法是:用maxcrawl< - max)初始化变量爬网)。搜索第二个最大值。如果它小于crawl-1,您的答案是crawl-1。否则,有两种可能性:

  1. 第二个最大值等于crawl-1:分配crawl< - crawl-1
  2. 第二个最大值等于爬网

在任何情况下,搜索第三个最大值。如果它小于crawl-1,您的答案是crawl-1。否则,有两种可能性:

  1. 第三个最大值等于crawl-1:分配crawl< - crawl-1
  2. 第三个最大值等于爬网

...

您注意到递归结构吗?

您要么在某个步骤中找到答案,要么您将获得最小的元素而没有答案。在这种情况下,答案将为crawl-1(在此步骤中将存储min-1)。

要找到连续的最大值,您可以迭代删除最大值。例如,要找到第二个最大值,请删除最大值并在新的AVL树中找到最大值。

时间复杂性将为o(n * log n)。每个拆卸操作都采用o(log n),您最多可以做n删除。

The general idea is: initialize a variable crawl with the max (crawl <- max). Search the second maximum. If it's smaller than crawl-1, your answer is crawl-1. Else, there are two possibilities:

  1. The second maximum is equal to crawl-1: assign crawl <- crawl-1.
  2. The second maximum is equal to crawl.

In any of the cases, search the third maximum. If it's smaller than crawl-1, your answer is crawl-1. Else, there are two possibilities:

  1. The third maximum is equal to crawl-1: assign crawl <- crawl-1.
  2. The third maximum is equal to crawl.

...

Did you notice the recursion structure ?

Either you will find the answer at some step or you will achieve the smallest element without answer. In this case the answer will be crawl-1 (which will store min-1 at this step).

To find the successive maximums, you can iteratively remove the maximum. For example, to find the second maximum, remove the maximum and find the maximum in the new AVL tree.

The time complexity would be O(n * log n). Each removal operation takes O(log n) and you do at most n removals.

离线来电— 2025-02-02 22:25:14

如果我们知道等级数组和所有值都是不同的(n值),我们可以进行以下二进制搜索:

如果max -max -min + 1 = n,答案是min-1

否则,将[Min,Max]的中间元素占据。

  1. max -med + 1 = n/2。在这种情况下,请考虑med之前的元素是med_before。如果med_before&lt; Med -1,答案是MED -1。否则,转到[min,med_before]

  2. max -med + 1&gt; N/2。在这种情况下,请考虑Med之后的元素是Med_after。转到[Med_after,Max]

If we know the rank array and all values are distinct (n values), we can do a binary search like the following:

If max - min + 1 = n, the answer is min-1.

Else, take the median element of [min, max] in the tree.

  1. max - med + 1 = n/2. In this case, consider that the element right before med is med_before. If med_before < med - 1, the answer is med - 1. Else, go to [min, med_before].

  2. max - med + 1 > n/2. In this case, consider that the element right after med is med_after. Go to [med_after, max].

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