<tfoot id='Wth31'></tfoot>

<small id='Wth31'></small><noframes id='Wth31'>

      <bdo id='Wth31'></bdo><ul id='Wth31'></ul>
    <i id='Wth31'><tr id='Wth31'><dt id='Wth31'><q id='Wth31'><span id='Wth31'><b id='Wth31'><form id='Wth31'><ins id='Wth31'></ins><ul id='Wth31'></ul><sub id='Wth31'></sub></form><legend id='Wth31'></legend><bdo id='Wth31'><pre id='Wth31'><center id='Wth31'></center></pre></bdo></b><th id='Wth31'></th></span></q></dt></tr></i><div id='Wth31'><tfoot id='Wth31'></tfoot><dl id='Wth31'><fieldset id='Wth31'></fieldset></dl></div>

      1. <legend id='Wth31'><style id='Wth31'><dir id='Wth31'><q id='Wth31'></q></dir></style></legend>

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

        Concatenating/Merging/Joining two AVL trees(连接/合并/加入两个 AVL 树)

        <legend id='LVmas'><style id='LVmas'><dir id='LVmas'><q id='LVmas'></q></dir></style></legend>
      2. <small id='LVmas'></small><noframes id='LVmas'>

          <i id='LVmas'><tr id='LVmas'><dt id='LVmas'><q id='LVmas'><span id='LVmas'><b id='LVmas'><form id='LVmas'><ins id='LVmas'></ins><ul id='LVmas'></ul><sub id='LVmas'></sub></form><legend id='LVmas'></legend><bdo id='LVmas'><pre id='LVmas'><center id='LVmas'></center></pre></bdo></b><th id='LVmas'></th></span></q></dt></tr></i><div id='LVmas'><tfoot id='LVmas'></tfoot><dl id='LVmas'><fieldset id='LVmas'></fieldset></dl></div>

          • <tfoot id='LVmas'></tfoot>
              <bdo id='LVmas'></bdo><ul id='LVmas'></ul>
                    <tbody id='LVmas'></tbody>
                  本文介绍了连接/合并/加入两个 AVL 树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有两个 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.

                  推荐答案

                  假设您可能会破坏输入树:

                  Assuming you may destroy the input trees:

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

                  因此,整个操作可以在 O(log n) 中执行.

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

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

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

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

                  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.

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

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

                  这篇关于连接/合并/加入两个 AVL 树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                    <tbody id='0EzV2'></tbody>
                  <legend id='0EzV2'><style id='0EzV2'><dir id='0EzV2'><q id='0EzV2'></q></dir></style></legend>
                      <bdo id='0EzV2'></bdo><ul id='0EzV2'></ul>

                        <small id='0EzV2'></small><noframes id='0EzV2'>

                      • <i id='0EzV2'><tr id='0EzV2'><dt id='0EzV2'><q id='0EzV2'><span id='0EzV2'><b id='0EzV2'><form id='0EzV2'><ins id='0EzV2'></ins><ul id='0EzV2'></ul><sub id='0EzV2'></sub></form><legend id='0EzV2'></legend><bdo id='0EzV2'><pre id='0EzV2'><center id='0EzV2'></center></pre></bdo></b><th id='0EzV2'></th></span></q></dt></tr></i><div id='0EzV2'><tfoot id='0EzV2'></tfoot><dl id='0EzV2'><fieldset id='0EzV2'></fieldset></dl></div>

                        1. <tfoot id='0EzV2'></tfoot>