# Depth of a leaf in a prefix tree (Huffman Coding)

So, regarding Huffman Coding we know that the optimal way of "storing" it is using a full binary tree (where all nodes have either 0 or 2 children). All leaves are the symbols $i$ of the alphabet we want to encode (all $i$ have a frequency of appearance$f_i$) and if we consider the freq. $f_i$ of each leaf (freq. of that character) then the freq. of a node is the sum of the frequencies of its children, therefore the root has $f_i = 100$ or $f_i = 1$.

Visual example (taken from Introduction to Algorithms) because I'm not really good at explaining this stuff:

Now I want to prove that the depth of a leaf (a character) in this prefix tree is $O(\log_{2}{(1/f_I))}$, where $f_i$ is the freq. of the character $i$.

I've tried by induction, where the base case would be for $\log_{2}{(1/1) = 0 }$ and indeed the depth of the root is 0. But I don't know how to proceed.

I've been thinking about how in a complete binary tree the depth of a leaf is $\log_{2}{n}$ with n being the number of nodes in that level (I'm not 100% sure about this) and how de depth of a character i in the prefix tree = length of the binary code (for example depth of d is 3 and its coding is 111) but I can't see how I could use those two things (if they are even useful).

What do you think about it? I don't know how to approach it :(

Btw thanks for your time and have a nice day!

• You have to use Huffman's algorithm somehow... – Yuval Filmus Oct 14 '17 at 21:28

Consider a symbol $\sigma$ whose frequency is $f$. During the course of Huffman's algorithm, $\sigma$ is matched with other nodes, and the depth of $\sigma$ is the number of times this happens.
At the first time that $\sigma$ is matched, it is either the symbol with smallest frequency or with second smallest frequency. In the former case, it is merged into a new symbol whose frequency is at least $2f$. In the latter case, after $\sigma$ is merged with the symbol of smallest frequency, all symbols have frequency at least $f$, and so the next time that it is merged, the merged symbol will have frequency at least $2f$.
Summarizing, it takes at most two mergings to double the frequency of a symbol. If its original frequency is $f$, then this shows that the depth of $f$ is at most $2\lceil \log_2(1/f) \rceil = O(\log(1/f))$.
• Since the depth of $f$ is its code length, this then ties Huffman to Shannon's theory? – Hendrik Jan Oct 14 '17 at 22:02
• First of all, thanks for you answer, but I still don't see where do you get the $\log_{2}{(1/f)}$ from. The depth of the leaf is the same as the number of times it was merged, right? And the lesser the freq. the higher the number of times it was merged, therefore there's an inverse relation between freq and depth (that¡s why it's 1/f) but I don't understand where does the logarithm come from. Sry :( – clvr_bottle Oct 15 '17 at 8:21
• Big O notation only cares about the value for large inputs (in this case, $1/f$ needs to be large). – Yuval Filmus Oct 15 '17 at 8:23