连接/合并/连接两个 AVL 树

发布于 2024-08-17 01:20:23 字数 88 浏览 10 评论 0原文

假设我有两棵 AVL 树,并且第一棵树中的每个元素都小于第二棵树中的任何元素。将它们连接成一棵 AVL 树的最有效方法是什么?我到处搜索但没有发现任何有用的东西。

Assume that I have two AVL trees and that each element from the first tree is smaller then any element from the second tree. What is the most efficient way to concatenate them into one single AVL tree? I've searched everywhere but haven't found anything useful.

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

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

发布评论

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

评论(4

绝不放开 2024-08-24 01:20:23

假设你可以销毁输入树:

  1. 删除左树最右边的元素,并用它构造一个新的根节点,其左子节点是左树,其右子节点是右树:O(log n)
  2. 确定并设置该节点的平衡因子:O(log n)。在(暂时)违反不变量的情况下,平衡因子可能超出范围 {-1, 0, 1}
  3. 旋转以使平衡因子回到范围内: O(log n) 旋转: O(log n)

因此,整个操作可以在 O(log n) 内完成。

编辑:再想一想,在以下算法中推理旋转更容易。它也很可能更快:

  1. 确定两棵树的高度:O(log n)。
    假设右边的树更高(另一种情况是对称的):
  2. 从左边的树中删除最右边的元素(如果需要,旋转并调整其计算的高度)。令n 为该元素。 O(log n)
  3. 在右树中,向左导航,直到到达其子树最多比 left 高 1 个节点。令r 为该节点。 O(log n)
  4. 用值为 n 的新节点以及子树 leftr 替换该节点。 O(1)
    通过构造,新节点是 AVL 平衡的,其子树 1 比 r 高。

  5. 相应地增加其父级的余额。 O(1)

  6. 并像插入后一样重新平衡。 O(logn)

Assuming you may destroy the input trees:

  1. remove the rightmost element for the left tree, and use it to construct a new root node, whose left child is the left tree, and whose right child is the right tree: O(log n)
  2. determine and set that node's balance factor: O(log n). In (temporary) violation of the invariant, the balance factor may be outside the range {-1, 0, 1}
  3. rotate to get the balance factor back into range: O(log n) rotations: O(log n)

Thus, the entire operation can be performed in O(log n).

Edit: On second thought, it is easier to reason about the rotations in the following algorithm. It is also quite likely faster:

  1. Determine the height of both trees: O(log n).
    Assuming that the right tree is taller (the other case is symmetric):
  2. remove the rightmost element from the left tree (rotating and adjusting its computed height if necessary). Let n be that element. O(log n)
  3. In the right tree, navigate left until you reach a node whose subtree is at most one 1 taller than left. Let r be that node. O(log n)
  4. replace that node with a new node with value n, and subtrees left and r. O(1)
    By construction, the new node is AVL-balanced, and its subtree 1 taller than r.

  5. increment its parent's balance accordingly. O(1)

  6. and rebalance like you would after inserting. O(log n)
嗳卜坏 2024-08-24 01:20:23

一种超简单的解决方案(无需对树之间的关系进行任何假设)是这样的:

  1. 将两棵树合并排序到一个合并数组中(同时迭代两棵树)。
  2. 从数组构建一棵 AVL 树 - 将中间元素作为根,并递归地应用于左半部分和右半部分。

两个步骤都是 O(n)。它的主要问题是它需要 O(n) 的额外空间。

One ultra simple solution (that works without any assumptions in the relations between the trees) is this:

  1. Do a merge sort of both trees into one merged array (concurrently iterate both trees).
  2. Build an AVL tree from the array - take the middle element to be the root, and apply recursively to left and right halves.

Both steps are O(n). The major issue with it is that it takes O(n) extra space.

°如果伤别离去 2024-08-24 01:20:23

我读到的这个问题的最佳解决方案可以在这里找到< /a>.如果您纠正此问题,则与 meriton 的答案非常接近:

在算法的第三步向左导航直到到达子树与左树高度相同的节点。这并不总是可能的(参见反例图像)。执行此步骤的正确方法是两次查找高度为 hh+1 的子树,其中 h 是左树的高度

counterexample

The best solution I read to this problem can be found here. Is very close to meriton's answer if you correct this issue:

In the third step of the algorithm navigates left until you reach the node whose sub tree has the same height as the left tree. This is not always possible, (see counterexample image). The right way to do this step is two find for a subtree with height h or h+1 where h is the height of the left tree

counterexample

流云如水 2024-08-24 01:20:23

我怀疑您只需要步行一棵树(希望是较小的)并将其每个元素单独添加到另一棵树。 AVL 插入/删除操作并非旨在处理一次添加整个子树。

I suspect that you'll just have to walk one tree (hopefully the smaller) and individually add each of it's elements to the other tree. The AVL insert/delete operations are not designed to handle adding a whole subtree at a time.

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