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

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

  • <tfoot id='N45m1'></tfoot>

      <bdo id='N45m1'></bdo><ul id='N45m1'></ul>
      1. 如何有效地合并两个 BST?

        How to merge two BST#39;s efficiently?(如何有效地合并两个 BST?)

        <small id='4pkNj'></small><noframes id='4pkNj'>

          • <bdo id='4pkNj'></bdo><ul id='4pkNj'></ul>
          • <legend id='4pkNj'><style id='4pkNj'><dir id='4pkNj'><q id='4pkNj'></q></dir></style></legend>
                1. <tfoot id='4pkNj'></tfoot>

                2. <i id='4pkNj'><tr id='4pkNj'><dt id='4pkNj'><q id='4pkNj'><span id='4pkNj'><b id='4pkNj'><form id='4pkNj'><ins id='4pkNj'></ins><ul id='4pkNj'></ul><sub id='4pkNj'></sub></form><legend id='4pkNj'></legend><bdo id='4pkNj'><pre id='4pkNj'><center id='4pkNj'></center></pre></bdo></b><th id='4pkNj'></th></span></q></dt></tr></i><div id='4pkNj'><tfoot id='4pkNj'></tfoot><dl id='4pkNj'><fieldset id='4pkNj'></fieldset></dl></div>
                    <tbody id='4pkNj'></tbody>
                  本文介绍了如何有效地合并两个 BST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何合并两棵保持BST性质的二叉搜索树?

                  如果我们决定从树中取出每个元素并将其插入到另一个元素中,则该方法的复杂度将是 O(n1 * log(n2)),其中 n1 是我们分裂的树的节点数(比如 T1),n2 是另一棵树的节点数(比如 T1>T2).此操作后,只有一个 BST 有 n1 + n2 个节点.

                  If we decide to take each element from a tree and insert it into the other, the complexity of this method would be O(n1 * log(n2)), where n1 is the number of nodes of the tree (say T1), which we have splitted, and n2 is the number of nodes of the other tree (say T2). After this operation only one BST has n1 + n2 nodes.

                  我的问题是:我们能做得比 O(n1 * log(n2)) 更好吗?

                  My question is: can we do any better than O(n1 * log(n2))?

                  推荐答案

                  Naaff 的回答有更多细节:

                  Naaff's answer with a little more details:

                  • 将 BST 展平为排序列表是 O(N)
                    • 这只是对整个树的有序"迭代.
                    • 同时做这件事是 O(n1+n2)
                    • 保持指向两个列表头部的指针
                    • 选择较小的头部并移动其指针
                    • 这就是归并排序的合并方式
                    • 请参阅下面的代码片段以了解算法[1]
                    • 在我们的例子中,排序列表的大小为 n1+n2.所以 O(n1+n2)
                    • 结果树将是二分搜索列表的概念 BST

                    三步 O(n1+n2) 结果为 O(n1+n2)

                    Three steps of O(n1+n2) result in O(n1+n2)

                    对于相同数量级的 n1 和 n2,这优于 O(n1 * log(n2))

                    For n1 and n2 of the same order of magnitude, that's better than O(n1 * log(n2))

                    [1] 从排序列表中创建平衡 BST 的算法(在 Python 中):

                    [1] Algorithm for creating a balanced BST from a sorted list (in Python):

                    def create_balanced_search_tree(iterator, n):
                        if n == 0:
                            return None
                        n_left = n//2
                        n_right = n - 1 - n_left
                        left = create_balanced_search_tree(iterator, n_left)
                        node = iterator.next()
                        right = create_balanced_search_tree(iterator, n_right)
                        return {'left': left, 'node': node, 'right': right}
                    

                    这篇关于如何有效地合并两个 BST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 中生成随机数的最佳方法是什么?)
                3. <legend id='TI3L4'><style id='TI3L4'><dir id='TI3L4'><q id='TI3L4'></q></dir></style></legend>
                        <bdo id='TI3L4'></bdo><ul id='TI3L4'></ul>
                        <i id='TI3L4'><tr id='TI3L4'><dt id='TI3L4'><q id='TI3L4'><span id='TI3L4'><b id='TI3L4'><form id='TI3L4'><ins id='TI3L4'></ins><ul id='TI3L4'></ul><sub id='TI3L4'></sub></form><legend id='TI3L4'></legend><bdo id='TI3L4'><pre id='TI3L4'><center id='TI3L4'></center></pre></bdo></b><th id='TI3L4'></th></span></q></dt></tr></i><div id='TI3L4'><tfoot id='TI3L4'></tfoot><dl id='TI3L4'><fieldset id='TI3L4'></fieldset></dl></div>
                              <tbody id='TI3L4'></tbody>
                          • <tfoot id='TI3L4'></tfoot>

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