简体   繁体   English

创建最佳二叉搜索树,与创建霍夫曼树一样?

[英]Creating an optimal binary search tree, same as creating a huffman tree?

Studying for a final here soon and I was wondering if creating an optimal binary search tree as asked in the question below is the same as creating a huffman tree given the symbols and frequencies. 很快在这里学习决赛,我想知道是否按照下面的问题创建一个最佳的二叉搜索树是否与给定符号和频率的霍夫曼树相同。

Compute an optimal binary search tree with keys K1 < K2 < K3 < K4 for the probabilities: 为以下概率计算键为K1 <K2 <K3 <K4的最优二叉搜索树:

p1 = .1  p2 = .2   p3 = .3  p4 = .1   
q0 = .15 q1 = .05  q2 = 0   q3 = .1

So here we would pair the lowest two probabilities and create a internal node of probability = n1 + n2 then pair the next lowest two probabilites, and so on? 所以在这里,我们将配对两个最低概率,并创建一个概率为n1 + n2的内部节点,然后配对下一个最低的两个概率,依此类推?

They're actually two different problems. 它们实际上是两个不同的问题。 Huffman tree generation does not need to preserve key order, whereas BST generation does. 霍夫曼树的生成不需要保留键顺序,而BST生成则不需要。 Furthermore, Huffman tree generation requires extra nodes to "join" other nodes, which is not the case in BSTs (you join nodes with already existing nodes). 此外,霍夫曼树的生成需要额外的节点来“联接”其他节点,而在BST中则不是这种情况(将节点与已经存在的节点联接在一起)。

For "optimal" BST generation, you want to minimize the weighted sum of all node depths (with the weight being the frequency of the node). 对于“最佳” BST生成,您希望最小化所有节点深度的加权总和(权重是节点的频率)。 In this case, p3 should be the parent of p2 and p4, and p2 should be the parent of p1. 在这种情况下,p3应该是p2和p4的父级,而p2应该是p1的父级。 This generates a "weighted sum" of: 这将产生“加权和”:

Node Probability Depth Product Parent
K1   .1          2     .2      K2
K2   .2          1     .2      K3
K3   .3          0     0       None (root)
K4   .1          1     .1      K3
                       .5 (total, smaller than all other configurations)

It's unclear what you mean with q0 to q3 , but if those were also nodes in the desired BST, you would still try to optimize for the weighted sum of depths. 不清楚您对q0q3含义,但是如果这些也是所需BST中的节点,您仍将尝试针对深度的加权总和进行优化。

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

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