O(logn) 总是一棵树吗?

发布于 2024-08-22 13:50:56 字数 437 浏览 5 评论 0原文

我们总是看到(二叉搜索)树上的操作在最坏情况下运行时间为 O(logn),因为树的高度是 logn。我想知道如果我们被告知算法的运行时间是 logn 的函数,例如 m + nlogn,我们是否可以得出结论它必须涉及(增强的)树?

编辑: 感谢您的评论,我现在意识到分治法和二叉树在视觉/概念上非常相似。我从未将两者联系起来。但我想到了一种情况,其中 O(logn) 不是一个分治算法,它涉及一棵没有 BST/AVL/红黑树属性的树。

这是带有 Find/Union 操作的不相交集合数据结构,其运行时间为 O(N + MlogN),其中 N 是元素数量,M 是 Find 操作的数量。

如果我错过了什么,请告诉我,但我看不出分而治之是如何在这里发挥作用的。我只是在这个(不相交集)情况下看到它有一个没有 BST 属性的树,并且运行时间是 logN 的函数。所以我的问题是为什么/为什么我不能从这个案例中进行概括。

We always see operations on a (binary search) tree has O(logn) worst case running time because of the tree height is logn. I wonder if we are told that an algorithm has running time as a function of logn, e.g m + nlogn, can we conclude it must involve an (augmented) tree?

EDIT:
Thanks to your comments, I now realize divide-conquer and binary tree are so similar visually/conceptually. I had never made a connection between the two. But I think of a case where O(logn) is not a divide-conquer algo which involves a tree which has no property of a BST/AVL/red-black tree.

That's the disjoint set data structure with Find/Union operations, whose running time is O(N + MlogN), with N being the # of elements and M the number of Find operations.

Please let me know if I'm missing sth, but I cannot see how divide-conquer comes into play here. I just see in this (disjoint set) case that it has a tree with no BST property and a running time being a function of logN. So my question is about why/why not I can make a generalization from this case.

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

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

发布评论

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

评论(7

故人的歌 2024-08-29 13:50:56

你所拥有的完全是倒退的。 O(lg N) 通常意味着某种分治算法,实现分治的一种常见方法是二叉树。虽然二叉树是所有分而治之算法的一个重要子集,但无论如何它们都是一个子集。

在某些情况下,您可以相当直接地将其他分而治之算法转换为二叉树(例如,对另一个答案的评论已经尝试声称二分搜索是类似的)。然而,仅举另一个明显的例子,多路树(例如 B 树、B+ 树或 B* 树),而显然一棵树显然不是二叉树。

同样,如果您非常想要,您可以扩展一点,即多路树可以表示为二叉树的扭曲版本。如果您愿意,您可能可以将所有例外延伸到说所有例外都是(至少类似)二叉树。然而,至少对我来说,所做的只是使“二叉树”成为“分而治之”的代名词。换句话说,你所做的只是扭曲词汇,从本质上抹掉一个既独特又有用的术语。

What you have is exactly backwards. O(lg N) generally means some sort of divide and conquer algorithm, and one common way of implementing divide and conquer is a binary tree. While binary trees are a substantial subset of all divide-and-conquer algorithms, the are a subset anyway.

In some cases, you can transform other divide and conquer algorithms fairly directly into binary trees (e.g. comments on another answer have already made an attempt at claiming a binary search is similar). Just for another obvious example, however, a multiway tree (e.g. a B-tree, B+ tree or B* tree), while clearly a tree is just as clearly not a binary tree.

Again, if you want to badly enough, you can stretch the point that a multiway tree can be represented as sort of a warped version of a binary tree. If you want to, you can probably stretch all the exceptions to the point of saying that all of them are (at least something like) binary trees. At least to me, however, all that does is make "binary tree" synonymous with "divide and conquer". In other words, all you accomplish is warping the vocabulary and essentially obliterating a term that's both distinct and useful.

十秒萌定你 2024-08-29 13:50:56

不,您还可以对排序数组进行二分搜索(例如)。但不要相信我的话 http://en.wikipedia.org/wiki/Binary_search_algorithm

No, you can also binary search a sorted array (for instance). But don't take my word for it http://en.wikipedia.org/wiki/Binary_search_algorithm

〃温暖了心ぐ 2024-08-29 13:50:56

作为一个反例:

given array 'a' with length 'n'
y = 0
for x = 0 to log(length(a))
    y = y + 1
return y

运行时间是 O(log(n)),但这里没有树!

As a counter example:

given array 'a' with length 'n'
y = 0
for x = 0 to log(length(a))
    y = y + 1
return y

The run time is O(log(n)), but no tree here!

烟雨凡馨 2024-08-29 13:50:56

答案是否定的。已排序数组的二分查找时间复杂度为O(log(n))

Answer is no. Binary search of a sorted array is O(log(n)).

维持三分热 2024-08-29 13:50:56

采用对数时间的算法常见在二叉树的操作中。

O(logn) 的示例:

  • 使用二分搜索或平衡搜索树在排序数组中查找项目。

  • 按二分查找已排序输入数组中的值。

Algorithms taking logarithmic time are commonly found in operations on binary trees.

Examples of O(logn):

  • Finding an item in a sorted array with a binary search or a balanced search tree.

  • Look up a value in a sorted input array by bisection.

七色彩虹 2024-08-29 13:50:56

由于 O(log(n)) 只是一个上限,因此所有 O(1) 算法(例如 function (a, b) return a+b; )都满足条件。

但我必须同意所有 Theta(log(n)) 算法看起来都像树算法,或者至少可以抽象为一棵树。

As O(log(n)) is only an upper bound also all O(1) algorithms like function (a, b) return a+b; satisfy the condition.

But I have to agree all Theta(log(n)) algorithms kinda look like tree algorithms or at least can be abstracted to a tree.

傲娇萝莉攻 2024-08-29 13:50:56

简短回答:

仅仅因为算法将 log(n) 作为其分析的一部分并不意味着涉及树。例如,以下是一个非常简单的算法,O(log(n)

for(int i = 1; i < n; i = i * 2)
  print "hello";

如您所见,没有涉及树。John 还提供了一个很好的示例,说明如何在这些都需要 O(log(n)) 时间,并且可以创建或引用其他代码示例,因此不要基于渐近时间复杂度做出假设,请查看代码以了解。当然,

更多关于树的信息:

仅仅因为算法涉及“树”并不意味着O(logn)您也需要知道树类型以及操作如何影响。

一些示例:

  • 示例 1)

插入或搜索以下不平衡树的时间复杂度为 O(n)

输入图片此处描述

  • 示例 2)

插入或搜索以下平衡树都需要 O(log(n))

平衡二叉树:

在此处输入图像描述

3 阶平衡树:

在此处输入图像描述

其他评论

如果您使用的树没有办法“平衡” ”,那么您的操作很可能会花费 O(n) 时间,而不是 O(logn)。如果您使用自平衡树,则插入通常需要更多时间,因为树的平衡通常发生在插入阶段。

Short Answer:

Just because an algorithm has log(n) as part of its analysis does not mean that a tree is involved. For example, the following is a very simple algorithm that is O(log(n)

for(int i = 1; i < n; i = i * 2)
  print "hello";

As you can see, no tree was involved. John, also provides a good example on how binary search can be done on a sorted array. These both take O(log(n)) time, and there are of other code examples that could be created or referenced. So don't make assumptions based on the asymptotic time complexity, look at the code to know for sure.

More On Trees:

Just because an algorithm involves "trees" doesn't imply O(logn) either. You need to know the tree type and how the operation affects the tree.

Some Examples:

  • Example 1)

Inserting or searching the following unbalanced tree would be O(n).

enter image description here

  • Example 2)

Inserting or search the following balanced trees would both by O(log(n)).

Balanced Binary Tree:

enter image description here

Balanced Tree of Degree 3:

enter image description here

Additional Comments

If the trees you are using don't have a way to "balance" than there is a good chance that your operations will be O(n) time not O(logn). If you use trees that are self balancing, then inserts normally take more time, as the balancing of the trees normally occur during the insert phase.

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