Encode "abracadabra" in as few bits as possible. Count how often each character appears: a appears 5 times, b appears 2 times, c appears 1 time, d appears 1 time, r appears 2 times. Each count starts as a root of its own. Merge the two lightest roots under a new parent, over and over, until one tree is left. The path down to a character is then its code.
Huffman over 5 characters of "abracadabra".
There are 5 roots: c:1, d:1, b:2, r:2, a:5. The two lightest are c:1 and d:1, so they merge. Burying a root one level deeper costs one extra bit for every character under it, which is why the lightest go first.
Merging c:1 and d:1.
The new parent weighs 1 + 1 = 2 and takes their place in the queue. The edge to the left child is labelled 0 and the edge to the right child 1. Those labels, read from the top down, are the codes.
New parent of weight 2.
There are 4 roots: 2, b:2, r:2, a:5. The two lightest are b:2 and r:2, so they merge. Burying a root one level deeper costs one extra bit for every character under it, which is why the lightest go first.
Merging b:2 and r:2.
The new parent weighs 2 + 2 = 4 and takes their place in the queue. The edge to the left child is labelled 0 and the edge to the right child 1. Those labels, read from the top down, are the codes.
New parent of weight 4.
There are 3 roots: 2, 4, a:5. The two lightest are 2 and 4, so they merge. Burying a root one level deeper costs one extra bit for every character under it, which is why the lightest go first.
Merging 2 and 4.
The new parent weighs 2 + 4 = 6 and takes their place in the queue. The edge to the left child is labelled 0 and the edge to the right child 1. Those labels, read from the top down, are the codes.
New parent of weight 6.
There are 2 roots: a:5, 6. The two lightest are a:5 and 6, so they merge. Burying a root one level deeper costs one extra bit for every character under it, which is why the lightest go first.
Merging a:5 and 6.
The new parent weighs 5 + 6 = 11 and takes their place in the queue. The edge to the left child is labelled 0 and the edge to the right child 1. Those labels, read from the top down, are the codes.
New parent of weight 11.
Walk from the root down to a, reading 0 for every left edge and 1 for every right: the code is 0, 1 bit for a character that appears 5 times, so a costs 5 bits in total.
The code for a is 0.
Walk from the root down to b, reading 0 for every left edge and 1 for every right: the code is 110, 3 bits for a character that appears 2 times, so b costs 6 bits in total.
The code for b is 110.
Walk from the root down to c, reading 0 for every left edge and 1 for every right: the code is 100, 3 bits for a character that appears 1 time, so c costs 3 bits in total.
The code for c is 100.
Walk from the root down to d, reading 0 for every left edge and 1 for every right: the code is 101, 3 bits for a character that appears 1 time, so d costs 3 bits in total.
The code for d is 101.
Walk from the root down to r, reading 0 for every left edge and 1 for every right: the code is 111, 3 bits for a character that appears 2 times, so r costs 6 bits in total.
The code for r is 111.
The codes are a=0, b=110, c=100, d=101, r=111. "abracadabra" becomes 23 bits, against 33 with a fixed 3-bit code for 5 characters: a saving of 10. No code is a prefix of another, because every character sits at a leaf, so a decoder can read the bits straight through without separators and never has to guess where one code ends.
23 bits against 33 fixed-width.