简体   繁体   中英

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:

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?

They're actually two different problems. Huffman tree generation does not need to preserve key order, whereas BST generation does. 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).

For "optimal" BST generation, you want to minimize the weighted sum of all node depths (with the weight being the frequency of the node). In this case, p3 should be the parent of p2 and p4, and p2 should be the parent of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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