二叉搜索树的搜索时间

发布于 2024-07-13 04:48:22 字数 43 浏览 7 评论 0原文

有谁知道如何计算二叉搜索树的搜索时间(即最坏情况、最佳情况和平均情况)?

Does anyone know how to figure out search time for a binary search tree(i.e. worst-case, best-case, and average-case)?

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

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

发布评论

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

评论(3

叫嚣ゝ 2024-07-20 04:48:22

对于非自平衡树(可能但对于搜索树来说不常见),最坏情况是 O(n),这适用于退化二叉树(链表)。

在这种情况下,您平均必须搜索一半的列表才能找到所需的元素。

完美平衡树的最佳情况是 O(log2 n),因为您将每个树级别的搜索空间减半。

平均情况介于两者之间,完全取决于数据:-)

由于您很少能够控制将数据插入树中的顺序,因此自平衡树通常更可取,因为它们会添加少量的每次插入或删除的时间,它们大大加快了搜索速度。 它们最坏的情况比不平衡的树要好得多。

                 8
         _______/ \_______
        /                 \
       4                  12
    __/ \__             __/ \__
   /       \           /       \
  2         6        10        14
 / \       / \       / \       / \
1   3     5   7     9  11    13  15

在这个完美平衡的树中,您可以看到每 n 层都有 2n-1 个节点。 这意味着对于 15 个节点,您无需搜索超过 4 个节点即可找到它(例如,要查找 13,您可以搜索 812、1413)。 这就是 log2n 数字的来源。

正如已经说过的,退化的不平衡树是一个链表。 如果您的数据按顺序到达并将其插入不平衡二叉树中,您将得到:

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -+
                                           |
+------------------------------------------+
|
+-> 10 -> 11 -> 12 -> 13 -> 14 -> 15

在这种情况下要查找 13,您需要搜索 1234567< /code>、89101112 和 <代码>13,因此 O(n)。

For a non-self-balancing tree (possible but unusual for a search tree), worst case is O(n), which is for the degenerate binary tree (a linked list).

In this case, you have to search, on average, half the list before finding your desired element.

Best case is O(log2 n) for a perfectly balanced tree, since you cut the search space in half for every tree level.

Average case is somewhere in between those two and depends entirely on the data :-)

Since you rarely get to control the sequence in which data is inserted into a tree, self-balancing trees are usually preferable since, while they add a small amount of time to each insertion or deletion, they greatly speed up searching. Their worst case is so much better than unbalanced trees.

                 8
         _______/ \_______
        /                 \
       4                  12
    __/ \__             __/ \__
   /       \           /       \
  2         6        10        14
 / \       / \       / \       / \
1   3     5   7     9  11    13  15

In this perfectly balanced tree, you can see that you get 2n-1 nodes for every n levels. That means for 15 nodes, you never have to search more than four nodes to find it (e.g., to find 13, you search 8, 12, 14 and 13). That's where the log2n figure comes from.

A degenerate unbalanced tree, as already stated, is a linked list. If your data arrived in sequence and you inserted it into an unbalanced binary tree, you'd get:

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -+
                                           |
+------------------------------------------+
|
+-> 10 -> 11 -> 12 -> 13 -> 14 -> 15

To find 13 in that case, you'd need to search 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 and 13, hence O(n).

眼泪淡了忧伤 2024-07-20 04:48:22

可能想将其标记为“作业”。 这是一个很好的起点:http://en.wikipedia.org/wiki/Binary_search_tree

,平衡二叉搜索树的最坏情况查找为 O(log n),最好情况为 O(1)(当所需值为根时),平均情况为 O(log n) n) (叶子包含的值比它们的父辈多得多)。

最坏的情况是最有趣的,通过认识到二叉树的第一层有 1 个节点,第二层有 2 个节点,第三层有 4 个节点,依此类推,很容易看出最坏的情况。 因此,深度n的二叉树中的节点数恰好是2^n - 1。 指数函数的数学反函数是对数,因此:O(log n)

不平衡树可能与链表一样糟糕,并且可能具有如下形状:

  1
 / \
    2
   / \
      3
     / \
        4
       / \

在这种情况下,最坏情况的访问时间是O(n)

Might want to tag this one as "homework". Here's a good starting point: http://en.wikipedia.org/wiki/Binary_search_tree

In general, a balanced binary search tree has a worst-case lookup of O(log n), best case of O(1) (when the desired value is the root) and an average case of O(log n) (the leaves contain exponentially more values than their parents).

The worst case is the most interesting and is easily seen by recognizing that the first level of a binary tree has 1 node, the second has 2, the third has 4 and so on. Thus, the number of nodes in a binary tree of depth n is precisely 2^n - 1. The mathematical inverse of the exponential function is the logarithm, thus: O(log n).

An unbalanced tree can be as bad as a linked list and may have a shape like the following:

  1
 / \
    2
   / \
      3
     / \
        4
       / \

In this situation, the worst-case access time is O(n).

帅的被狗咬 2024-07-20 04:48:22

最好的情况是 O(1)。 第一个元素可能是您正在寻找的项目。 最坏情况是 O(n),即在倾斜树中,平均情况是 O(lg n)。

Best case is O(1). the first element could be the item you are looking for. worst case is O(n) ie in a skewed tree and average case is O(lg n).

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