简体   繁体   English

最佳搜索树:计算搜索树的成本并显示它不是最优的

[英]Optimal Search Tree : Calculate the cost of the search tree and show that it is not optimal

Consider the following binary search tree, along with the following frequencies of lookups: 考虑以下二叉搜索树,以及以下查找频率:

       13                          Key       | 13 | 11 | 26 | 1 | 12 | 28                                                                                
     /    \                        -----------------------------------------                                                
   11      26                      Frequency | 26 |  5 | 25 | 1 |  3 | 15                                         
  /  \      \                                                 
 1   12      28                                               

I was given this question: 我得到了这个问题:

Calculate the cost of the search tree above for the given search frequencies and show that the search tree isn't optimal for the given search frequencies. 针对给定的搜索频率计算上述搜索树的成本,并显示搜索树对于给定的搜索频率不是最佳的。

I calculated the cost, but my teacher say I did so incorrectly, but did not explain why. 我计算了成本,但我的老师说我做错了,但没有解释原因。

So what we need to do for calculate cost is check where the first node is. 因此,我们需要为计算成本做的是检查第一个节点的位置。 13 is in level 1 and the frequency of 13 is 26 . 13是1级,频率13是26 So we do 26*1=26 所以我们做26*1=26

The nodes 11 and 26 are in level 2, the nodes 1, 12 and 28 are in level 3. 节点11和26处于级别2,节点1,12和28处于级别3。

In the end we have for cost: 26*1 + 5*2 + 25*2 + 1*3 + 3*3 + 15*3 . 最后我们有成本: 26*1 + 5*2 + 25*2 + 1*3 + 3*3 + 15*3 My teacher says that this calculation is incorrect, but didn't explain why. 我的老师说这个计算不正确,但没有解释原因。

Also, how do you show that a tree isn't optimal? 另外,您如何证明树不是最佳的? Here is definition I have from our script: 这是我从脚本中得到的定义:

Let K be a set of keys and R a workload. K是一组密钥, R是工作负载。 A search tree T over K is optimal for R iff P(T) = min{P(T') | T' is search tree for K} 对于R iff P(T) = min{P(T') | T' is search tree for K}K搜索树T是最佳的 P(T) = min{P(T') | T' is search tree for K}

@templatetypedef Thank you very much for take your time and help!! @templatetypedef非常感谢您抽出宝贵的时间和帮助! Your answer is very nice for me I understand many things from it. 你的回答对我很好,我从中理解了很多东西。 Here is tree I found it is more optimal than this tree from task: 这是树,我发现它比任务中的这棵树更优化:

        26
       /  \
      13   28
     /
    11
   /  \
  1    12

Tree above have cost of 143 and this one have 138 . 上面的树有143成本,这个有138 So this one is really more optimal and task is solved :) 所以这个真的更优化,任务解决了:)

Fundamentally, you're approaching the question of calculating the total lookup time in a BST correctly. 从根本上说,您正在接近正确计算BST中的总查找时间的问题。 You're taking each node in the tree, using the depth to determine the number of comparisons necessary to perform a lookup that ends at that node, multiplying those values by the number of lookups, and summing the results. 您将使用树中的每个节点,使用深度来确定执行以该节点结束的查找所需的比较次数,将这些值乘以查找次数,并对结果求和。 I didn't meticulously double-check your exact calculations and so it's possible that you missed something, though. 我并没有仔细地仔细检查你的确切计算,所以你可能错过了一些东西。

Your second question was about determining whether a binary search tree is optimal for a given set of lookups. 您的第二个问题是确定二叉搜索树对于给定的查找集是否是最佳的。 You've given the rigorous mathematical definition, but in this case I think it might be a bit easier to explain this at a higher level. 您已经给出了严格的数学定义,但在这种情况下,我认为在更高层次上解释这一点可能会更容易一些。

The calculation you did earlier here is a way of starting with a BST and information about what lookups will be performed, then computing a number corresponding to the number of comparisons that will end up being made in the course of performing those lookups. 您之前在此处进行的计算是一种从BST开始的方式以及有关将执行哪些查找的信息,然后计算与在执行这些查找过程中最终将进行的比较次数相对应的数字。 That number essentially tells you how fast those lookups are going to be - higher numbers mean that the lookups take longer, and lower numbers mean that the lookups will take less time. 这个数字实际上告诉你这些查找的速度有多快 - 更高的数字意味着查找需要更长的时间,更低的数字意味着查找将花费更少的时间。

Now, imagine that you want to make a BST that will take the least total amount of time to perform the lookups in question. 现在,假设您想制作一个BST,它将花费最少的总时间来执行相关的查找。 In other words, you want the "best" BST for the given set of keys and lookup frequencies. 换句话说,您希望给定的一组键和查找频率具有“最佳”BST。 That BST would be one with the lowest total lookup cost, where that cost is calculated using the approach you worked through earlier on. 该BST将是总查找成本最低的那个,其中该成本是使用您之前处理的方法计算的。 The terminology for a BST with that property - that it has the best lookup speed for those frequencies among all the possible BSTs that you can make - is an optimal BST. 具有该属性的BST的术语 - 它对于您可以制作的所有可能BST中的那些频率具有最佳查找速度 - 是最佳BST。

The question here is to show that the tree you have isn't optimal. 这里的问题是表明你拥有的树不是最佳的。 That means that you need to show that this isn't the best possible tree you can make. 这意味着您需要证明这不是您可以制作的最佳树。 One way to do this would be to find an even better tree. 一种方法是找到更好的树。 So can you find another BST with the same keys where the total lookup time is lower than the one you were given? 那么你能找到另一个具有相同键的BST,其总查找时间低于你给出的那个吗?

Good luck! 祝好运!

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

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