简体   繁体   English

二叉树搜索失败

[英]Unsuccessful binary tree search

我知道对于一次成功的搜索,使用二叉树在包含n个键的所有输入上的平均搜索时间在Big O(lg n)中,但是对于不成功的研究,该结果是否成立?

Strictly speaking you may not attain O(log n) even for a successful search if your binary tree is unbalanced. 严格来讲,如果您的二叉树不平衡,即使成功搜索也可能无法获得O(log n)。

For a balanced binary tree it's easy to see that the result will hold even if it's unsuccessful, as at any generic step you are allowed to rule out significant fraction (around a half) of the remaining tree. 对于平衡的二叉树,很容易看到即使结果不成功,结果仍然成立,因为在任何通用步骤中,您都可以排除剩余树的很大一部分(大约一半)。

Yes, the binary tree structure allows you to essentially throw away half the data set at each check. 是的,二叉树结构实际上允许您在每次检查时丢弃一半的数据集。

Just think about how many of the nodes you actually have to visit in order to make that determination that it's not in there. 只需考虑一下您实际上必须访问多少个节点才能确定它不在其中。

Example: 例:

     5
     /\
    3  8
   /\  /\
  1 4  6 9

Say you want 2. Start at the root, go left, throwing away 8 and its children. 假设您想要2。从根开始,向左走,扔掉8及其子代。 Smaller than 3 so go left throwing away 4. Larger than 1 so it's not there. 小于3,因此向左扔掉4。大于1,因此不存在。

In this case you visited only 5, 3, and 1. You can think of it as the same as inserting a new node. 在这种情况下,您仅访问了5、3和1。您可以将其视为与插入新节点相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM