使用森林包的二项树

使用森林包的二项树

我需要绘制一棵三期树,每个阶段都有不同的元素,例如:

上面的路径:

(12)(12)--->()

下面的路径:

(13)(12)--->(123)

我怎样才能画出这样的树?

编辑:

类似于此,但是对于 3 周期树,使用森林包使其更加整洁:

    \documentclass[12pt, a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\tikzset{bullet/.style={circle,fill,inner sep=2pt}}


\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
      matrix of nodes,
      minimum size=0.1cm,
      column sep=3cm,
      row sep=0.1cm,nodes={text width=8em}
          ]
    {
          &   &  \\
          & {$()$} &   \\
     $(12)$ &   &  \\
          &  {$(123)$} &   \\
          &   &  \\
    };
    \node[bullet,right=0mm of tree-3-1.east](b-3-1){};
    \node[bullet,left=0mm of tree-2-2.west](b-2-2){};
    \node[bullet,left=0mm of tree-4-2.west](b-4-2){};
    \draw[->] (b-3-1) -- (b-2-2) node [midway,above] {$(12)$};
    \draw[->] (b-3-1) -- (b-4-2) node [midway,below] {$(13)$};
  \end{tikzpicture}
\end{document}

答案1

和 Alan Munn 和 cfr 一样,我也不明白这个问题。您可以使用森林手册第 52 页中的自动枚举功能,使您的代码更加通用,然后您需要做的就是添加一些基本的 pgf 函数。您可能需要"cat","mole","duck","marmot",...根据需要调整项目。

\documentclass[12pt, a4paper]{report}
\usepackage{forest}
\bracketset{action character=@}
\def\pexp#1#2{\pgfmathsetmacro{\x}{int(#1)}\pgfmathparse{\x>0?(\x>1?"#2^{\x}":"#2"):""}\pgfmathresult}%
\def\btreelabel#1#2{node[midway,sloped,font=\scriptsize,/forest,if n=1{/tikz/below}{/tikz/above}]{$\pexp{#1}{p}\pexp{#2}{(1-p)}$}}%
\def\btree#1#2#3{,grow=east,l*=2,edge label=\btreelabel{#1}{#2},if={#3>0}{append=[@\btree{#1}{#2+1}{#3-1}],append=[@\btree{#1+1}{#2}{#3-1}]}{}}%

\xdef\LstItems{"cat","mole","duck","marmot","koala","rhino","bear","hare","opossum"}
\pgfmathdeclarefunction{mitem}{1}{%
\begingroup%
\pgfmathparse{{\LstItems}[#1-1]}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%

\begin{document}
\begin{forest}
for tree={grow'=0},tempcounta=1,
before typesetting nodes={for descendants={content/.pgfmath={mitem(int(tempcounta))}, tempcounta+=1}}
[pft@\btree{0}{0}{2}]
\end{forest}
\end{document}

在此处输入图片描述

附录:回答你的问题的第 2 版。

\documentclass[12pt, a4paper]{report}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={grow'=east,l sep=4em,s sep=3em,circle,inner sep=2pt,fill}
[,label=left:{$(12)$}
 [,label=right:{$()$},edge label={node[midway,sloped,above]{(12)}}]
 [,label=right:{$(123)$},edge label={node[midway,sloped,below]{(13)}}]
]
\end{forest}
\end{document}

在此处输入图片描述

最后附录

\documentclass[12pt, a4paper]{report}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={grow'=east,l sep=4em,s sep=3em,circle,inner sep=2pt,fill}
[,label=left:{$(12)$}
 [,label=above:{$()$},edge label={node[midway,sloped,above]{$(12)$}}
  [,label=right:$X$,edge label={node[midway,sloped,above]{$x$}}]
  [,label=right:$Y$,edge label={node[midway,sloped,below]{$y$}}]
 ]
 [,label=below:{$(123)$},edge label={node[midway,sloped,below]{(13)}}
  [,label=right:$U$,edge label={node[midway,sloped,above]{$u$}}]
  [,label=right:$V$,edge label={node[midway,sloped,below]{$v$}}]
 ]
]
\end{forest}
\end{document}

在此处输入图片描述

答案2

基于回答(@marmoth 的编辑使其得到了显著改进,再次感谢!):

\documentclass[12pt, margin=3mm]{standalone}
\usepackage{forest}

\begin{document}
\forestset{
     declare toks={elo}{font=\footnotesize, inner sep=2pt, midway, sloped}, % Edge Label Options
     anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
     dot/.style={tikz+={\fill (.child anchor) circle[radius=3pt];}},
     decision edge label/.style 2 args= {
        edge label/.expanded={node[anchor=#1,\forestoption{elo}]{$#2$}}
     },
     decision/.style={if n=1
     {decision edge label={south}{#1}}
     {decision edge label={north}{#1}}
     },
     decision tree/.style={
     for tree={grow'=east,
     s sep=1em,l=13ex,
     if n children=0{anchors=west}{
            if n=1{anchors=south east}{anchors=north east}
                                   },
     },
     anchors=east, 
     dot, for descendants=dot,
     delay={for descendants={split option={content}{;}{content,decision}}},
      }
    }

    \begin{forest} decision tree
[(12)
    [(X);(12),
        [A;a]
        [B;b]
     ]
     [(123);(13),
         [C;c]
         [D;d]
     ]
]
    \end{forest}
\end{document}

在此处输入图片描述

相关内容