具有标记边且末端无节点的森林树

具有标记边且末端无节点的森林树

... 一次又一次... :) 我画了这棵树

在此处输入图片描述

CorelDraw!用于呈现卷积编码规则,现在我喜欢借助 Forest 包重新绘制和现代化这些规则。我坚持以下几点:

  • 跳过绘制树末端的节点
  • 将边缘标签放置在更好/更短的路径上
  • 使树的布局在所有树层上对称

在此处输入图片描述

我恳请您帮助我解决以下问题,并为树中的节点添加名称以供日后使用,例如绘制具有分支条件的输入线、附加一些图例和辅助图像或为callout节点(气球)添加一些解释。

到目前为止我设法编写了以下代码:

\documentclass[tikz,
               margin=3mm,
               preview]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{forest}
%---------------------------------------------------------------%
    \begin{document}
   \begin{forest}
    for tree={% style of tree nodes
      circle, font=\small\sffamily,
             fill = blue!60!black,
     minimum size = 4mm, 
        inner sep = 0.5pt,
             text = white,
              % style of tree (edges, distances, direction)
             edge = {draw, semithick},
  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) |-
      (.child anchor) -- % intermediate point
      (.child anchor)\forestoption{edge label};
  },
             grow = east,
            s sep = 2mm,    % sibling distance
            l sep = 12mm,   % level distance
               }
[a
    [b,edge label={node[xshift=-9mm,fill=white]{11}}
        [d,edge label={node[xshift=-9mm,fill=white]{01}}
            [d,edge label={node[xshift=-9mm,fill=white]{10}}
                [,edge label={node[xshift=-9mm,fill=white]{10}}]
                [,edge label={node[xshift=-9mm,fill=white]{01}}]
            ]
            [c,edge label={node[xshift=-9mm,fill=white]{01}}
                [,edge label={node[xshift=-9mm,fill=white]{00}}]
                [,edge label={node[xshift=-9mm,fill=white]{11}}]
            ]
        ]
        [c,edge label={node[xshift=-9mm,fill=white]{10}}
            [b,edge label={node[xshift=-9mm,fill=white]{00}}
                [,edge label={node[xshift=-9mm,fill=white]{10}}]
                [,edge label={node[xshift=-9mm,fill=white]{01}}]
            ]
            [a,edge label={node[xshift=-9mm,fill=white]{11}}
                [,edge label={node[xshift=-9mm,fill=white]{11}}]
                [,edge label={node[xshift=-9mm,fill=white]{00}}]
            ]
        ]
    ]
    [a,edge label={node[xshift=-9mm,fill=white]{00}}
        [b,edge label={node[xshift=-9mm,fill=white]{11}}
            [d,edge label={node[xshift=-9mm,fill=white]{01}}
                [,edge label={node[xshift=-9mm,fill=white]{10}}]
                [,edge label={node[xshift=-9mm,fill=white]{01}}]
            ]
            [c,edge label={node[xshift=-9mm,fill=white]{10}}
                [,edge label={node[xshift=-9mm,fill=white]{00}}]
                [,edge label={node[xshift=-9mm,fill=white]{11}}]
            ]
        ]
        [a,edge label={node[xshift=-9mm,fill=white]{00}}
            [b,edge label={node[xshift=-9mm,fill=white]{11}}
                [,edge label={node[xshift=-9mm,fill=white]{01}}]
                [,edge label={node[xshift=-9mm,fill=white]{10}}]
            ]
            [a,edge label={node[xshift=-9mm,fill=white]{00}}
                [,edge label={node[xshift=-9mm,fill=white]{11}}]
                [,edge label={node[xshift=-9mm,fill=white]{00}}]
            ]
        ]
    ]
];
    \end{forest}
\end{document}

答案1

如果不知道标签应该是什么样子,就很难说标签应该是什么样子。您可以定义一个变量来保存它并使用它,定义一种样式,或者使用各种自动标签选项之一(如果有模式的话)。

我认为,其余部分相当简单。

  • 通过测试终端节点是否有子节点来区分终端节点和其他节点。

  • 用于name命名节点以供日后引用。

  • 使用不可见的终端节点来实现间距和对称。

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\forestset{
  declare toks={way label}{},
}
\begin{document}
\begin{forest}
  for tree={% style of tree nodes
    semithick,
    font=\small\sffamily,
    edge = {draw, semithick},
    grow = 0,
    s sep = 2mm,    % minimum sibling distance
    l sep = 12mm,   % minimum level distance
    edge path'={
      (!u) |- (.child anchor)\forestoption{edge label}
    },
    circle,
    minimum size=4mm,
    inner sep=.5pt,
    anchor=center,
  },
  where n children=0{}{
    draw,
    fill = blue!60!black,
    text = white,
  },
  before typesetting nodes={
    where way label={}{}{
      edge label={
        node [pos=.75, fill=white, font=\sffamily] {\forestoption{way label}}
      },
    },
  }
  [a
    [b, way label=11
      [d
        [d
          []
          []
        ]
        [c
          []
          []
        ]
      ]
      [c
        [b
          []
          []
        ]
        [a
          []
          []
        ]
      ]
    ]
    [a
      [b
        [d, name=zarko
          []
          []
        ]
        [c
          []
          []
        ]
      ]
      [a
        [b
          []
          []
        ]
        [a, name=bob
          [, way label=11
          ]
          [, for current and ancestors={way label=00}
          ]
        ]
      ]
    ]
  ]
  \draw [<->, shorten >=20pt, shorten <=20pt] (zarko) -- (current bounding box.east |- zarko) -- ++(5pt,0) |- (bob);
\end{forest}
\end{document}

调整树

相关内容