为什么二进制搜索logn的时间复杂性,而BST的时间复杂性是n?

发布于 2025-02-06 15:16:37 字数 291 浏览 3 评论 0 原文

算法中,4 th Robert Sedgewick的Edition,不同算法的时间复杂表的表现为:

”时间复杂度表“

基于此表,bST的搜索时间复杂性为n,并且本身的二进制搜索是logn。

两者有什么区别?我已经看到了对这些的解释,但它们是有道理的,但是,我似乎不明白为什么bst的搜索时间复杂性不是logn,因为我们通过不断将树折断并忽略其他部分来搜索。

In Algorithms, 4th edition by Robert Sedgewick, the time complexity table for different algorithms is given as:

Time complexity table

Based on this table, the searching time complexity of a BST is N, and of binary search in and of itself is logN.

What is the difference between the two? I have seen explanations about these separately and they made sense, however, I can't seem to understand why the searching time complexity of a BST isn't logN, as we are searching by continually breaking the tree in half and ignoring the other parts.

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

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

发布评论

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

评论(2

栖竹 2025-02-13 15:16:37

来自

...平均而言,每个比较都允许操作跳过大约一半的树,以便每个查找,插入或删除的时间都与存储在树中存储的项目数的对数成比例成正比,o(log n log n )。但是,有时可能会发生最坏情况,当树未平衡并且时间复杂性为O(n)时,这三个功能的时间复杂性是O(n)。

因此,您有点期望log(n),但不能绝对保证。

From binary-search-trees-bst-explained-with-examples

...on average, each comparison allows the operations to skip about half of the tree, so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree, O(log n) . However, some times the worst case can happen, when the tree isn't balanced and the time complexity is O(n) for all three of these functions.

So, you kind of expect log(N) but it's not absolutely guaranteed.

一身软味 2025-02-13 15:16:37

BST的搜索时间复杂性是N,并且本身的二进制搜索是logn。两者有什么区别?

区别在于,排序阵列上的二进制搜索始终始于中间元素(即 n 是奇数时的中间)。这不能在BST中保证。 root 可能是中间元素,但不必是。

例如,这是一个有效的BST:

               10
              /
             8
            /
           5
          /
         2
        /
       1

...但是它不是一个平衡的BST,因此在鉴于该树根的根部找到值1的过程将包括访问其节点 all 。但是,如果在排序列表中显示相同的值( 1,2,5,8,10 ),则二进制搜索将从5开始,从不访问8或10

。平衡树与表平衡

我们可以使用自我平衡搜索树(如AVL)扩展给定表,然后我们得到:

实现 搜索 插入 删除删除
(无序列表) 顺序 搜索

the searching time complexity of a BST is N, and of binary search in and of itself is logN. What is the difference between the two?

The difference is that a binary search on a sorted array always starts at the middle element (i.e. the median when n is odd). This cannot be guaranteed in a BST. The root might be the middle element, but it doesn't have to be.

For instance, this is a valid BST:

               10
              /
             8
            /
           5
          /
         2
        /
       1

...but it is not a balanced one, and so the process of finding the value 1 given the root of that tree, will include visiting all its nodes. If however the same values were presented in a sorted list (1,2,5,8,10), a binary search would start at 5 and never visit 8 or 10.

Adding self-balancing trees to the table

We can extend the given table with self-balancing search trees, like AVL, and then we get this:

implementation search insert delete
sequential search (unordered list) ???? ???? ????
binary search (ordered array) lg???? ???? ????
BST ???? ???? ????
AVL lgN lgN lgN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文