如何绘制正确的决策树

如何绘制正确的决策树

我需要为我的 LaTeX 文档绘制一个决策树(使用 TiZ/forest或任何其他包)。我搜索了一段时间,想找到类似的东西,但没有找到。生成的决策树应该如下图所示。

如能得到任何帮助/提示我将非常感激。

在此处输入图片描述

这是我目前所能做的,当然树的形状不必与图中的完全相同。

\documentclass[margin=10pt]{standalone}
\usepackage{tikz,forest}
\usetikzlibrary{arrows.meta}

\begin{document}
  \tikzset{
    decision/.style={diamond, minimum height=10pt, minimum width=10pt, inner sep=1pt},
    chance/.style={circle, minimum width=10pt, draw=blue, fill=none, thick, inner sep=0pt},
  }
\begin{forest}
  label L/.style={
    edge label={node[midway,left,font=\scriptsize]{#1}}
  },
  label R/.style={
    edge label={node[midway,right,font=\scriptsize]{#1}}
  },
  for tree={
    child anchor=north,
    for descendants={
      {edge=->}
    }
  },
  [$x_2$, decision, draw
    [$x_1$, decision, draw, label L=N,
      [1, rectangle, draw, label L=N, tier=bottom]
      [0, rectangle, draw, label R=Y, tier=bottom]
    ]
    [$x_3$, decision, draw, label R=Y,
      [$x_1$, decision, draw, label L=N,
        [1, rectangle, draw, label L=N, tier=bottom]
        [0, rectangle, draw, label R=Y, tier=bottom]
      ]
      [0, rectangle, draw, label R=Y, tier=bottom]
    ]
  ]
\end{forest}

\end{document}

先感谢您

答案1

这是一个解决方案forest

输出

在此处输入图片描述

代码

\documentclass[margin=10pt]{standalone}
\usepackage{tikz,forest}
\usetikzlibrary{arrows.meta}

\forestset{
    .style={
        for tree={
            base=bottom,
            child anchor=north,
            align=center,
            s sep+=1cm,
    straight edge/.style={
        edge path={\noexpand\path[\forestoption{edge},thick,-{Latex}] 
        (!u.parent anchor) -- (.child anchor);}
    },
    if n children={0}
        {tier=word, draw, thick, rectangle}
        {draw, diamond, thick, aspect=2},
    if n=1{%
        edge path={\noexpand\path[\forestoption{edge},thick,-{Latex}] 
        (!u.parent anchor) -| (.child anchor) node[pos=.2, above] {Y};}
        }{
        edge path={\noexpand\path[\forestoption{edge},thick,-{Latex}] 
        (!u.parent anchor) -| (.child anchor) node[pos=.2, above] {N};}
        }
        }
    }
}

\begin{document}
\begin{forest} 
[$x_2$, tikz={\draw[{Latex}-, thick] (.north) --++ (0,1);}
    [$x_1$
        [1] 
        [0] 
    ]   
    [$x_3$
        [$x_1$
            [1] 
            [0] 
        ]   
        [0] 
    ]   
] 
\end{forest}
\end{document}

答案2

\documentclass{standalone}
\usepackage[T1]{fontenc}                
\usepackage[utf8]{inputenc}             
\usepackage[english,italian]{babel}
\usepackage{tikz}
  \usetikzlibrary{shapes,arrows,fit,calc,positioning}
  \tikzset{box/.style={draw, diamond, thick, text centered, minimum height=0.5cm, minimum width=1cm}}
  \tikzset{line/.style={draw, thick, -latex'}}
\begin{document}
    \begin{tikzpicture}[auto]
        \node [box]                                    (x3)      {x\ped{3}};
        \node [box, below=0.5cm of x3, xshift=-3cm]    (x1sx)    {x\ped{1}};
        \node [box, below=0.5cm of x3, xshift=3cm]     (x2dx)    {x\ped{2}};
        \node [box, below=0.5cm of x1sx, xshift=-1cm]  (x2sx)    {x\ped{2}};
        \node [box, below=0.5cm of x2sx, xshift=1cm]   (A2sx)    {A\ped{2}};
        \node [box, below=0.5cm of x2sx, xshift=-1cm]  (A1sx)    {A\ped{1}};
        \node [box, right=1cm of A2sx]                 (A3sx)    {A\ped{3}};
        %
        \node [box, below=0.5cm of x2dx, xshift=1cm]   (x1dx)    {x\ped{1}};
        \node [box, below=0.5cm of x1dx, xshift=-1cm]  (A2dx)    {A\ped{2}};
        \node [box, below=0.5cm of x1dx, xshift=1cm]   (A3dx)    {A\ped{3}};
        \node [box, left=0.5cm of A2dx]                (A1dx)    {A\ped{1}};
        %
        \path [line] (x3) -|         (x2dx);
        \path [line] (x3) -|         (x1sx);
        \path [line] (x2dx) -|       (x1dx);
        \path [line] (x2dx) -|       (A1dx);
        \path [line] (x1dx) -|       (A2dx);
        \path [line] (x1dx) -|       (A3dx);
        \path [line] (x1sx) -|       (x2sx);
        \path [line] (x1sx) -|       (A3sx);
        \path [line] (x2sx) -|       (A1sx);
        \path [line] (x2sx) -|       (A2sx);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容