更新

更新

我正在使用 Forest 包,并尝试为奇数行和偶数行以及标签绘制灰色背景。

我已经有一个使用 tikz“手动”编写的此图表版本。但它是水平布局的(我希望它是垂直布局,就像在代码中一样),并且我还想进行一些更改。我试图使用 Forest 从头开始​​做这件事...

树结果

我卡在代码中试图绘制背景......(我似乎能够在树下画画......)

\documentclass{standalone}

\usepackage{forest}

\begin{document}

\begin{forest}
for tree={draw, circle, l sep+=0.5em, inner sep=0.05em}
[,fill
    [{$-$}, edge label={node[midway,left]{$0.70$}}
        [$-$, edge label={node[midway,left]{$0.78$}}
            [$-$, edge label={node[midway,left]{$0.80$}}
                [$-$, edge label={node[midway,left]{$0.82$}}]
                [$+$, edge label={node[midway,right]{$0.18$}}]
            ]
            [$+$, edge label={node[midway,right]{$0.20$}}
                [$-$, edge label={node[midway,left]{$0.49$}}]
                [$+$, edge label={node[midway,right]{$0.51$}}]
            ]
        ]
        [$+$, edge label={node[midway,right]{$0.22$}}
            [$-$, edge label={node[midway,left]{$0.50$}}
                [$-$, edge label={node[midway,left]{$0.70$}}]
                [$+$, edge label={node[midway,right]{$0.30$}}]
            ]
            [$+$, edge label={node[midway,right]{$0.50$}}
                [$-$, edge label={node[midway,left]{$0.57$}}]
                [$+$, edge label={node[midway,right]{$0.43$}}]
            ]
        ]
    ]
    [{$+$}, edge label={node[midway,right]{$0.30$}}
        [$-$, edge label={node[midway,left]{$0.50$}}
            [$-$, edge label={node[midway,left]{$0.71$}}
                [$-$, edge label={node[midway,left]{$0.75$}}]
                [$+$, edge label={node[midway,right]{$0.25$}}]
            ]
            [$+$, edge label={node[midway,right]{$0.29$}}
                [$-$, edge label={node[midway,left]{$0.53$}}]
                [$+$, edge label={node[midway,right]{$0.47$}}]
            ]
        ]
        [$+$, edge label={node[midway,right]{$0.50$}}
            [$-$, edge label={node[midway,left]{$0.50$}}
                [$-$, edge label={node[midway,left]{$0.72$}}]
                [$+$, edge label={node[midway,right]{$0.28$}}]
            ]
            [$+$, edge label={node[midway,right]{$0.50$}}
                [$-$, edge label={node[midway,left]{$0.44$}}]
                [$+$, edge label={node[midway,right]{$0.56$}}]
            ]
        ]
    ]
]
\draw[draw=none,fill=black!2] (-5,-2) rectangle (5,-3);
\end{forest}

\end{document}

答案1

您可以使用backgrounds库来为树添加阴影,并fit使用库来帮助它适应节点。(您仍然需要手动调整以考虑标签,但fit会处理垂直问题,因为您知道树中使用的内部分隔符。)

我还扩展了最后的节点,以便标签不会相互排版。

只是为了说明的目的,我用深灰色给背景加了阴影,因为 2% 的阴影几乎是不可见的。

\documentclass{standalone}

\usepackage{forest}
\usetikzlibrary{backgrounds,fit}

\begin{document}

\begin{forest}
  for tree={draw, circle, l sep+=0.5em, where level={3}{s sep+=1em}{}, inner sep=0.05em}
  [,fill
      [{$-$}, edge label={node[midway,left]{$0.70$}}
          [$-$, edge label={node[midway,left]{$0.78$}}, name=a
              [$-$, edge label={node[midway,left]{$0.80$}}, name=c
                  [$-$, edge label={node[midway,left]{$0.82$}}]
                  [$+$, edge label={node[midway,right]{$0.18$}}]
              ]
              [$+$, edge label={node[midway,right]{$0.20$}}
                  [$-$, edge label={node[midway,left]{$0.49$}}]
                  [$+$, edge label={node[midway,right]{$0.51$}}]
              ]
          ]
          [$+$, edge label={node[midway,right]{$0.22$}}
              [$-$, edge label={node[midway,left]{$0.50$}}
                  [$-$, edge label={node[midway,left]{$0.70$}}]
                  [$+$, edge label={node[midway,right]{$0.30$}}]
              ]
              [$+$, edge label={node[midway,right]{$0.50$}}
                  [$-$, edge label={node[midway,left]{$0.57$}}]
                  [$+$, edge label={node[midway,right]{$0.43$}}]
              ]
          ]
      ]
      [{$+$}, edge label={node[midway,right]{$0.30$}}
          [$-$, edge label={node[midway,left]{$0.50$}}
              [$-$, edge label={node[midway,left]{$0.71$}}
                  [$-$, edge label={node[midway,left]{$0.75$}}]
                  [$+$, edge label={node[midway,right]{$0.25$}}]
              ]
              [$+$, edge label={node[midway,right]{$0.29$}}
                  [$-$, edge label={node[midway,left]{$0.53$}}]
                  [$+$, edge label={node[midway,right]{$0.47$}}]
              ]
          ]
          [$+$, edge label={node[midway,right]{$0.50$}}
              [$-$, edge label={node[midway,left]{$0.50$}}
                  [$-$, edge label={node[midway,left]{$0.72$}}]
                  [$+$, edge label={node[midway,right]{$0.28$}}]
              ]
              [$+$, edge label={node[midway,right]{$0.50$}}, name=b
                  [$-$, edge label={node[midway,left]{$0.44$}}]
                  [$+$, edge label={node[midway,right]{$0.56$}}]
              ]
          ]
      ]
  ]
  \begin{scope}[on background layer]
    \node [fill=black!25, fit={(a.south) (c) (b)}, inner ysep=.05em, inner xsep=.75em, outer sep=0pt] {};
  \end{scope}
\end{forest}

\end{document}

树荫

更新

要添加标签,您可以positioning按如下方式使用该库:

\documentclass{standalone}

\usepackage{forest}
\usetikzlibrary{backgrounds,fit,positioning}

\begin{document}

\begin{forest}
  for tree={draw, circle, l sep+=0.5em, where level={3}{s sep+=1em}{}, inner sep=0.05em}
  [,fill
      [{$-$}, edge label={node[midway,left]{$0.70$}}, name=e
          [$-$, edge label={node[midway,left]{$0.78$}}, name=a
              [$-$, edge label={node[midway,left]{$0.80$}}, name=c
                  [$-$, edge label={node[midway,left]{$0.82$}}, name=d]
                  [$+$, edge label={node[midway,right]{$0.18$}}]
              ]
              [$+$, edge label={node[midway,right]{$0.20$}}
                  [$-$, edge label={node[midway,left]{$0.49$}}]
                  [$+$, edge label={node[midway,right]{$0.51$}}]
              ]
          ]
          [$+$, edge label={node[midway,right]{$0.22$}}
              [$-$, edge label={node[midway,left]{$0.50$}}
                  [$-$, edge label={node[midway,left]{$0.70$}}]
                  [$+$, edge label={node[midway,right]{$0.30$}}]
              ]
              [$+$, edge label={node[midway,right]{$0.50$}}
                  [$-$, edge label={node[midway,left]{$0.57$}}]
                  [$+$, edge label={node[midway,right]{$0.43$}}]
              ]
          ]
      ]
      [{$+$}, edge label={node[midway,right]{$0.30$}}
          [$-$, edge label={node[midway,left]{$0.50$}}
              [$-$, edge label={node[midway,left]{$0.71$}}
                  [$-$, edge label={node[midway,left]{$0.75$}}]
                  [$+$, edge label={node[midway,right]{$0.25$}}]
              ]
              [$+$, edge label={node[midway,right]{$0.29$}}
                  [$-$, edge label={node[midway,left]{$0.53$}}]
                  [$+$, edge label={node[midway,right]{$0.47$}}]
              ]
          ]
          [$+$, edge label={node[midway,right]{$0.50$}}
              [$-$, edge label={node[midway,left]{$0.50$}}
                  [$-$, edge label={node[midway,left]{$0.72$}}]
                  [$+$, edge label={node[midway,right]{$0.28$}}]
              ]
              [$+$, edge label={node[midway,right]{$0.50$}}, name=b
                  [$-$, edge label={node[midway,left]{$0.44$}}]
                  [$+$, edge label={node[midway,right]{$0.56$}}]
              ]
          ]
      ]
  ]
  \begin{scope}[on background layer]
    \node [fill=black!25, fit={(a.south) (c) (b)}, inner ysep=.05em, inner xsep=.75em, outer sep=0pt] {};
  \end{scope}
  \node (p) [left=5pt of d] {memory 4};
  \node at (p |- c) {memory 3};
  \node at (p |- a) {memory 2};
  \node at (p |- e) {memory 1};
\end{forest}

\end{document}

标记

如果您希望标记树的节点垂直运行,请使用该rotate选项。(我不确定您是否想要这个。)

相关内容