使用 tikz 绘制莫尔斯树

使用 tikz 绘制莫尔斯树

我喜欢复制维基百科上的莫尔斯树莫尔斯树使用 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}

问题

  • 如何防止 R 和 U 互相覆盖
  • 如何防止圆圈本身变成虚线/点线

答案1

使用forest基于的包tikz比使用纯包更简单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}

在此处输入图片描述

树很宽,所以我考虑将树旋转 90 度。为此,您只需定义树的生长方向并据此确定箭头方向:

%   tree styles
    anchor=west, grow=0,

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

这使:

在此处输入图片描述

相关内容