I did this project once, but I used a 2 x 2 grid rather than a tree.
0 cat
1 bat
2 cot
3 cog
4 dog
etc.
I'm not sure how you would do it with a tree, but I assigned each word an index. For each pair, I kept track of the number of hops and the "first hop" using a struct. -1 means that the two words can't be bridged. You start by going through and initializing everything to -1, -1. (First number is the number of hops, next number is the "first hop" index.
0 1 2 3 4
0 (-1,-1) (-1,-1), (-1, -1), (-1, -1), (-1, -1)
1 (-1,-1) (-1, -1), (-1, -1), (-1, -1), (-1, -1)
2 (-1,-1) (-1, -1), (-1, -1), (-1, -1), (-1, -1)
3 (-1,-1) (-1, -1), (-1, -1), (-1, -1), (-1, -1)
4 (-1,-1) (-1, -1), (-1, -1), (-1, -1), (-1, -1)
Trivial words are the ones on the diagonal (they're already there).
0 1 2 3 4
0 ( 0, 0) (-1,-1), (-1,-1), (-1,-1), (-1,-1)
1 (-1,-1) ( 0, 0), (-1,-1), (-1,-1), (-1,-1)
2 (-1,-1) (-1,-1), ( 0, 0), (-1,-1), (-1,-1)
3 (-1,-1) (-1,-1), (-1,-1), ( 0, 0), (-1,-1)
4 (-1,-1) (-1,-1), (-1,-1), (-1,-1), ( 0, 0)
Next do the ones that are one hop apart (like "cot" and "cog" - indexes 2 and 3) and ("cog" and "dog" - indexes 3 and 4).
0 1 2 3 4
0 ( 0, 0) (-1,-1), (-1,-1), (-1,-1), (-1,-1)
1 (-1,-1) ( 0, 0), (-1,-1), (-1,-1), (-1,-1)
2 (-1,-1) (-1,-1), ( 0, 0), ( 1, 3), (-1,-1)
3 (-1,-1) (-1,-1), ( 1, 2), ( 0, 0), ( 1, 4)
4 (-1,-1) (-1,-1), (-1,-1), ( 1, 3), ( 0, 0)
Do it for the rest of the single hops too. First number is the number of hops, second number is the "first hop". Once you have those single hops all filled in (they aren't all filled in above), you can connect 2 to 4 in the next iteration by looking down row 2 to find the single hops and seeing (1,3). Now look down row 3 and you see (1,4). That means that 2 can connect to 4 through 3. the number of hops from 2 to 4 is at most the number of hops from 2 to 3 (1) plus the number of hops from 3 to 4 (1). 1 + 1 = 2, so you can fill in that cell with (2,3). Two hops, first hop is 3.
0 1 2 3 4
0 ( 0, 0) (-1,-1), (-1,-1), (-1,-1), (-1,-1)
1 (-1,-1) ( 0, 0), (-1,-1), (-1,-1), (-1,-1)
2 (-1,-1) (-1,-1), ( 0, 0), ( 1, 3), ( 2, 3)
3 (-1,-1) (-1,-1), ( 1, 2), ( 0, 0), ( 1, 4)
4 (-1,-1) (-1,-1), (-1,-1), ( 1, 3), ( 0, 0)
Red cell is derived from the two green cells.
Anyway that's how I did it. I don't know how you'd do it with a tree.