TikZ-Trees – How to Draw Morse Tree with TikZ

tikz-trees

I like to replicate this Morse tree from Wikipedia morse tree with tikz

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[nodes={draw, circle}, thick]
\node{Start}
    child { 
        node {E} 
        edge from parent [dotted]
        child { 
           node {I}
           edge from parent [dotted]
           child { 
             node {S}
             edge from parent [dotted]
             }
           child { 
             node {U}
             edge from parent [dashed]
           }
        }
        child { 
           node {A}
           edge from parent [dashed]
           child { 
             node {R}
             edge from parent [dotted]
             }
           child { 
             node {W}
             edge from parent [dashed]
           }
           }
    }
    child { 
        node {T}
        edge from parent [dashed]    
    };
 
\end{tikzpicture}

\end{document}

problems

  • how to prevent R and U cover each other
  • how to prevent the circles themselves being dashed / dotted

Best Answer

With package forest, which is based on tikz, is simpler than with pure tikz:

\documentclass[border=3.141592]{standalone}
\usepackage{forest}

\begin{document}
    \begin{forest}
for tree = {
%   nodes style
    circle, draw, minimum size=1.5em, inner sep=1pt, font=\small,
%   tree styles
    anchor=north,
    l sep=11mm,
    s sep=1mm,
%
    if n=1{edge={semithick, dotted}} % odd/left edges
          {edge={semithick,dashed}}  % even/right edges
            }
[start, name=s
    [e, name=s
        [I
            [S
                [H
                    [5, tier=L6]
                    [4]
                ]
                [V
                    []
                    [3]
                ]
            ]
            [U
                [F
                    []
                    []
                ]
                [
                    []
                    [2]
                ]
            ]% end of U
        ]% end of I
        [,phantom, tier=L6]
        [A
            [R
                [L
                    []
                    []
                ]
                [L
                    [+]
                    []
                ]
            ]
            [W
                [P
                    []
                    []
                ]
               [J
                    []
                    [1]
                ]
            ]% end of W
        ]% end of A
    ]% end of E
    [,phantom, tier=L6]
%%%%
    [T, name=t
        [N
            [D
                [B
                    [6]
                    [=]
                ]
                [X
                    [/]
                    []
                ]
            ]% end of D
            [K
                [C
                    []
                    []
                ]
                [Y
                    []
                    []
                ]
            ]%end of K
        ]% end of N
        [,phantom, tier=L6]
        [M
            [G
                [Z
                    [7]
                    []
                ]
               [Q
                    []
                    []
                ]
            ]% end of G
            [O
                [ 
                    [8]
                    []
                ]
               [
                    [9]
                    [0]
                ]
            ]% end of O
        ]% end of M
    ]% end of T
]
\draw[-stealth, dotted] (s-|e) -- node[above] {Dot}  ++ (-4em,0);
\draw[-stealth, dashed] (s-|t) -- node[above] {Dash} ++ (+4em,0);
    \end{forest}
\end{document}

enter image description here

Tree is quit wide, so my consider to rotate tree for 90 degree. For this you only need to define direction of tree growth and accordingly determined arrows direction:

%   tree styles
    anchor=west, grow=0,

and

\draw[-stealth, dotted] (s |- e) -- node[above, rotate=90] {Dot}  ++ (0,-4em);
\draw[-stealth, dashed] (s |- t) -- node[above, sloped] {Dash} ++ (0,+4em);

which gives:

enter image description here

Related Question