algorithm - 最坏情况二分查找?

标签 algorithm tree binary-search-tree

问题是:

In order to distinguish ordinary emails from spam, an algorithm with several features has been designed. Every feature provides information about the message, for instance number of suspicious words, the length of the message, degree of matching with spam templates etc. Every feature is a discrete variable with two values, for example, low/high, short/long, and similar/dissimilar. A tree with 255 nodes has been employed to make the decision whether to reject an email. How many operations/steps/time units are required at most to process each email?

我认为这将是一个完美的二叉树,因此 2^n - 1 = 255,因此 n = 8。但是,我开始考虑以下“最坏情况”场景:

...O
../\
.O..O
..../\
...O..O
and so forth.

这会使用二分搜索递归关系吗? T(n)=T(n/2)+1?

最佳答案

我认为你的答案是正确的,杰伊。根据问题,我将树画成这样:

>                     o
>                    /\
> feature 1:        o  o
>                  /\  /\
> feature 2:      o  o o o
> 
                   ...

所以你从一个根值开始。然后你询问电子邮件是否已成功满足该特征,因此它分为 2 个节点,Y 或 N。对于 Y(左子树),你询问电子邮件是否满足第二个特征,Y 或 N,并且这又分成 2 个节点,并且在 N 侧(右子树)上重复相同的操作。对所有功能重复此操作。

我们知道完美二叉树的大欧米茄(最坏情况)是 log(n) [base 2]。因此 log(255) [base 2] 大约为 8,这一定是所需的最大步数。

关于algorithm - 最坏情况二分查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22442190/

相关文章:

java - 确定已满的最高级别 - 二叉搜索树

c++ - 指向二叉树中新节点的指针

algorithm - 具有欧氏距离的 A* 算法

performance - 给定一个矩形内的点,确定最接近该点的边

C++ AST Visitor - 存储表达式值的问题

具有挑战性的递归问题 - 表示进程的树节点

javascript - 在某个时间范围内有效的对象的搜索列表

algorithm - 删除连续子数组以保留平均最小值

c - 函数的时间复杂度和空间

haskell - 递归类型是haskell,它不重复相同的构造函数