Skip to main content

The Morse tree: a variable-length code a century before Huffman

· 6 min read
UR3PKI
Software Engineer

Morse code is usually presented as a table: a letter, and beside it the dots and dashes. In that form it looks like an arbitrary list, and all you can do with it is memorise it.

It is in fact a binary tree, and in that form you can see the engineering decision taken in the 1840s: the frequency of a letter sets its depth. E is one dot, T one dash, because those are the commonest letters in English. Q, J and Z sit all the way down on the fourth level. Alfred Vail arrived at this not by theory but by counting: he counted the letters in the type cases of a local newspaper.

A variable-length code, allocated by symbol frequency. A hundred-odd years before Shannon and Huffman explained why that works.

The tree

Left is a dot, right is a dash. The four letter levels are completely filled — there are no free nodes. The digits are longer than any letter and sit a level below.

Morse tree

The track to the left is a dot, the one to the right a dash; beside each is a silkscreen mark: · one time unit, three. The diameter of a pad is the frequency of that letter in English text. Press any letter to hear it on its own. Empty pads are nodes the English alphabet does not use. The bottom row is the digits: they are longer than any letter and so sit a level deeper.

INTERNATIONAL MORSE CODEREV. GERKE · 1848SILENCEETIANMSURWDKGOHVFÜLÄPJBXCYZQÖCH5432167890dot · 1 time unitdash · 3 units26 LETTERS · 10 DIGITS · CYBERDEV.SPACE

How long it lasts

the same transmission in time: 117 units, 9,4 s at 15 WPM. It shows what the tree does not — a dash costs three times what a dot costs — and that is exactly why «optimality» here is measured in time rather than in the number of symbols.

CQDEUR3PKI

read back:

Two things are immediately visible here that the table hides.

Frequent letters sit close to the root. Not roughly but systematically: the diameter of a pad here is the letter's frequency, and the largest ones have gathered at the top.

A dash costs three times a dot. So the depth of a node and the cost of a letter are different things: O (---) sits on the third level, as does S (...), but it takes more than twice as long to send — eleven units of time against five. The timing diagram under the tree shows this directly.

How well Vail aimed

Letter frequency and the cost of its code

bars — frequency in English text, %; the line — the duration of the code in time units (right axis). The commoner the letter, the cheaper its code — and the more obvious the exceptions

04812ETAOINSHRDLCUMWFGYPBVKJXQZ04812

The correlation between a letter's frequency and the duration of its code — Spearman's rank correlation — comes out strong and negative. For work done by counting letters in type cases, that is almost indecently good.

The exceptions stand out on the chart. The loudest is O: the fourth commonest letter in English has the longest code the third level can offer.

Why this is not a Huffman code after all

Here it gets interesting, because the analogy “Morse is Huffman” is pleasing and wrong, for two separate reasons.

Morse is not a prefix code. E is . and I is .. — the code for one letter is the start of the code for another. A prefix code forbids that, and it is precisely the prohibition that lets a stream be decoded without separators. Huffman always produces a code where symbols sit only at the leaves; in Morse a symbol sits at every node, internal ones included.

That is why Morse cannot be sent as a continuous stream: between letters there must be a gap three units long. So the alphabet is not binary at all but ternary — dot, dash and gap — and it is the gap that carries what the shape of the tree carries in Huffman.

And optimality is not measured the same way. Huffman minimises the number of symbols, on the assumption that every symbol costs the same. In telegraphy a dash costs three times a dot, so what has to be minimised is time, not the count of marks. That is a different problem — coding with unequal symbol costs — and it was not solved until 1961 (Karp, via integer linear programming), with workable approximations later still.

In short: Morse is not Huffman and was never meant to be. But the question “how close is it?” remains a fair one, and it can be answered exactly.

What the allocation error costs

Let us compare fairly: take the same twenty-six codes and hand them out to the letters in the best possible way — the cheapest code to the commonest letter, and so on down. That this really is the optimum follows from the rearrangement inequality: no other allocation gives a shorter mean time.

6,07time units per letter on average — that is what Morse costs
7,0 %the loss against the best assignment of those same 26 codes
−0,78Spearman correlation between letter frequency and the cost of its code

Average letter time, in units

the same codes, a different assignment

Morse as it is6,07the best assignment5,68
LetterCodeCostsShould beLoss
O---1150,45
A.-530,16
L.-..970,08
Y-.--1390,08
C-.-.1190,06

Seven percent. So an allocation made in the 1840s by eye and by ear loses to the exact optimum for the same set of codes by less than a tenth.

The table shows where the time goes, and O heads it by a wide margin: eleven units where there should have been five.

But Vail cannot be blamed for it. The code we have just taken apart is not his. American Morse had different codes for a good dozen letters and used gaps inside symbols (O was sent there as two dots with a wide space between them). In 1848 Friedrich Gerke reworked it for the Hamburg telegraph line — he removed the internal gaps and evened out the lengths. In 1851 the German-Austrian Telegraph Union adopted that revision, and the international alphabet grew out of it.

So the frequency optimisation was Vail's, while the particular allocation we are measuring is Gerke's — and his aim was regularity, not the shortest time. Seven percent is what that regularity costs.

And a practical conclusion, the opposite of what you would expect

The tree explains why the alphabet is the way it is. But you must not learn Morse from it.

Anyone who traces a finger down the tree while a transmission runs in the headphones is putting counting where hearing should be: heard → counted → recalled. That intermediate step hits a ceiling around ten words a minute, and unlearning it afterwards is harder than learning properly from scratch. The method that works is Koch with Farnsworth: characters at 20–25 WPM from the very start, only the gaps stretched, and no table in front of you.

The tree is a good answer to “how does this work” and a poor answer to “how do I learn it”. They are different questions, and confusing them costs six months.


An open question I am leaving for later: everything counted above is for English letter frequencies. What a code optimised for someone else's language costs a Ukrainian operator, and what the same table looks like for the Cyrillic Morse alphabet, is a subject for another time — and one to begin by checking the Cyrillic table against a primary source rather than against memory.