<bdo id='Q50Uf'></bdo><ul id='Q50Uf'></ul>

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

  1. <i id='Q50Uf'><tr id='Q50Uf'><dt id='Q50Uf'><q id='Q50Uf'><span id='Q50Uf'><b id='Q50Uf'><form id='Q50Uf'><ins id='Q50Uf'></ins><ul id='Q50Uf'></ul><sub id='Q50Uf'></sub></form><legend id='Q50Uf'></legend><bdo id='Q50Uf'><pre id='Q50Uf'><center id='Q50Uf'></center></pre></bdo></b><th id='Q50Uf'></th></span></q></dt></tr></i><div id='Q50Uf'><tfoot id='Q50Uf'></tfoot><dl id='Q50Uf'><fieldset id='Q50Uf'></fieldset></dl></div>
    <tfoot id='Q50Uf'></tfoot>
  2. <legend id='Q50Uf'><style id='Q50Uf'><dir id='Q50Uf'><q id='Q50Uf'></q></dir></style></legend>
    1. 连接/合并/加入两个 AVL 树

      时间:2023-10-07

        <small id='3pox2'></small><noframes id='3pox2'>

            <tbody id='3pox2'></tbody>

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

          • <bdo id='3pox2'></bdo><ul id='3pox2'></ul>
              <tfoot id='3pox2'></tfoot>

              • <legend id='3pox2'><style id='3pox2'><dir id='3pox2'><q id='3pox2'></q></dir></style></legend>

                本文介绍了连接/合并/加入两个 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 树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:数据结构的“侵入性"是什么意思? 下一篇:实现“最后[秒/分钟/小时]中的点击次数";数

                相关文章

                最新文章

              • <legend id='soKO6'><style id='soKO6'><dir id='soKO6'><q id='soKO6'></q></dir></style></legend>

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

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

                    <bdo id='soKO6'></bdo><ul id='soKO6'></ul>