Can I achieve begin insertion on a binary tree in O(log(N))?

时间:2015-10-06 08:16:06

标签: algorithm

Consider a binary tree and some traverse criterion that defines an ordering of the tree's elements.

Does it exists some particular traverse criterion that would allow a begin_insert operation, i.e. the operation of adding a new element that would be at position 1 according to the ordering induced by the traverse criterion, with O(log(N)) cost?

I don't have any strict requirement, like the tree guaranteed to be balanced.

EDIT: But I cannot accept lack of balance if that allows degeneration to O(N) in worst case scenarios.

EXAMPLE:

Let's try to see if in-order traversal would work.

Consider the BT (not a binary search tree)

                      6
                   /     \
                  13      5
                 /  \    /
                2    8  9

In-order traversal gives 2-13-8-6-9-5

Perform begin_insert(7) in such a way that in-order traversal gives 7-2-13-8-6-9-5:

                      6
                   /     \
                  13      5
                 /  \    /
                2    8  9
               /
              7

Now, I think this is not a legitimate O(log(N)) strategy, because if I keep adding values in this way the cost degenerates into O(N) as the tree becomes increasingly unbalanced

                      6
                   /     \
                  13      5
                 /  \    /
                2    8  9
               /
              7
             /
            *
           /
          *
         /

This strategy would work if I rebalance the tree by preserving ordering:

                      8
                   /     \
                  2       9
                 /  \    / \
                7    13 6   5

but this costs at least O(N).

According to this example my conclusion would be that in-order traversal does not solve the problem, but since I received feedback that it should work maybe I am missing something?

1 个答案:

答案 0 :(得分:1)

在二叉树中插入,删除和查找都依赖于相同的搜索算法来找到执行操作的正确位置。这个O的复杂性(树的最大高度)。原因是要找到正确的位置,从根节点开始并比较键以确定是否应该进入左子树或右子树,然后执行此操作直到找到正确的位置。最糟糕的情况是你必须沿着最长的链条行进,这也是树高的定义。

如果你没有任何约束并允许任何树,那么这将是O(N),因为你允许只有左子项的树(例如)。

如果您想获得更好的保证,您必须使用保证树的高度具有下限的算法。例如AVL保证树的平衡,因此最大高度始终为log N,上述所有操作都以O(log N)运行。 Red-black trees不保证log N,但保证树不会太不平衡(最小高度* 2> =最大高度),因此它保持O(log N)的复杂性(详见页面)。

根据您的使用模式,您可能能够找到更具特色的数据结构,从而提供更高的复杂性(请参阅Fibonacci heap)。