[Tex/LaTex] How to draw a proper decision tree

foresttikz-trees

I need to draw a decision tree for my LaTeX document (with TikZ/forest or any other package). I searched around for a while to find something similar, but found none. The decision tree produced should be like the one shown in the figure bellow.

Any help/hint would be very much appreciated.

enter image description here

This is what I managed to do so far, of course the shape of the tree does not have to be exactly the same as the one in picture.

\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}

Thank you in advance

Best Answer

Here's a solution with forest.

Output

enter image description here

Code

\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}
Related Question