# How to prove that for a full binary tree $T$ there's a series of frequencies such that Huffman alg. will produce $T$?

How to prove that for a full (each node that's not a leaf has 2 nodes) binary tree $T$ of $n$ leaves there's a series of frequencies $f_1,f_2,...,f_n$ such that if we use Huffman algorithm on the series we'll get $T$?

First in order to get frequencies from $T$ we can assign the probability of $\frac{1}{2^d}$ to each node where $d$ is the depth of a given node. The root node will have the probability of $1$ which means $100$%. After one traversal of $T$ we could fill the array with the frequencies of the leaves of $T$ (only leaves correspond to the the actual values to be encoded).

I'm not sure how to prove that if we apply Huffman algorithm to the frequencies one of the trees will happen to be $T$. It seems like if we build a tree from the frequencies each time such that the new tree is different (if possible) from the previous tree we'll eventually build $T$. Also we know that Huffman tree is always a full tree.

The Huffman algorithm is as follows (as described in Algorithm Design by Jon Kleinberg, Eva Tardos):

Huffman(S) {
if |S|=2 {
return tree with root and 2 leaves
} else {
let y and z be lowest-frequency letters in S
S’ = S
remove y and z from S’
insert new letter ω in S’ with f_ω=f_y+f_z
T’ = Huffman(S’)
T=add two children y and z to leaf ω from T’
return T
}
}

• What do you mean by "one of the Huffman trees"? What is a Huffman tree of a series? The details matter here! – Yuval Filmus Nov 20 '17 at 12:32
• @YuvalFilmus What I meant is that for any full binary tree there's a series of frequencies such that using Huffman algorithm on the frequencies will produce the original tree. Let me know if this makes sense. – Yos Nov 20 '17 at 12:35
• There are many possible variants of Huffman's algorithm, so we need to know which variant you are using. Also, the formulation of the question suggests that you're using a nondeterministic variant of Huffman's algorithm which could produce many different trees for the same distribution. – Yuval Filmus Nov 20 '17 at 12:36
• The Huffman algorithm I was referring to is the one described on page 172 (chpater 4) of the book Algorithm Design by Jon Kleinberg, Eva Tardos. – Yos Nov 20 '17 at 12:38
• Some of us don't have Kleinberg and Tardos's book. If you want those of us to help you with this exercise, you will have to describe this variant of the algorithm in your post. – Yuval Filmus Nov 20 '17 at 12:40

The proof is by induction. The case $n = 1$ is clear. For $n > 1$, choose two leaves at maximal depths, and merge them. Huffman's algorithm could have chosen these two leaves at the first step and merged them. Now apply the inductive hypothesis to complete the proof.
• If there are $m>1$ elements of minimum probability, then there are $m(m-1)$ options (or $\binom{m}{2}$ if you don't care about the order). If there is one element of minimum and $m$ of second minimum probability, then there are $2m$ options (or $m$ if you don't care about the order). – Yuval Filmus Nov 20 '17 at 14:24
• You can check that $P(m,2) = m(m-1)$. – Yuval Filmus Nov 20 '17 at 14:35
{"ft_lang_label":"__label__en","ft_lang_prob":0.92425466,"math_prob":0.9912283,"size":1288,"snap":"2019-51-2020-05","text_gpt3_token_len":344,"char_repetition_ratio":0.13006231,"word_repetition_ratio":0.0,"special_character_ratio":0.27251554,"punctuation_ratio":0.05597015,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9992023,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-12T10:10:50Z\",\"WARC-Record-ID\":\"<urn:uuid:b1c85d29-a163-4435-b236-10723a7349f2>\",\"Content-Length\":\"145769\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0b90b0f8-c7fb-4809-9678-ed61b43d5c15>\",\"WARC-Concurrent-To\":\"<urn:uuid:216990b5-4e7c-45fc-b132-c678570968a2>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/84193/how-to-prove-that-for-a-full-binary-tree-t-theres-a-series-of-frequencies-suc\",\"WARC-Payload-Digest\":\"sha1:AKMZ3TVTOXSX4FP24GBT6EXUIOR3W4U5\",\"WARC-Block-Digest\":\"sha1:S4XA2QE77QKNRXJAM5UCB5K5HAP6AHC2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540542644.69_warc_CC-MAIN-20191212074623-20191212102623-00029.warc.gz\"}"}