7

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

  • 1
    Please show us the code for some basic forest or tikz-tree solution and we will be happy to help you with any issue, you find. Like this, it is just a "do it for me" post. – LaRiFaRi Jan 27 '16 at 11:24
  • I added the code of what I have so far. – Trex Jan 27 '16 at 12:26
7

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}
  • looks very nice. How to add an edge to the root node (as shown in the original figure I posted) ? – Trex Jan 27 '16 at 14:28
  • @Trex I thought that was not to be placed. Where is the root node? Is it a single node above? – Alenanno Jan 27 '16 at 14:30
  • The root node is the top node, node $x_2$ in your sample. There should be an edge pointing downwards to it. – Trex Jan 27 '16 at 14:34
  • @Trex An edge without another node? I mean, just the edge? – Alenanno Jan 27 '16 at 14:38
  • yeah, just like in the original figure – Trex Jan 27 '16 at 14:40
3
\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}

enter image description here

  • Thanks, it looks nice. However, is it possible to do it with forest? Furthermore, how can I add an edge to the root node, as shown in the original picture? – Trex Jan 27 '16 at 14:05
  • I'm trying to figure out how to add Y and N. I'll edit my message if I'm able to come up with a solution. – Marco Jan 27 '16 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.