1

I am working on a paper in LaTeX and I'm trying to insert a simple decision tree diagram using TikZ. However, I am not good with TikZ and I'm having two small problems with my tree.

enter image description here

This is my tree right now, and I am having trouble with it in two ways:

  1. I would like to get rid of the extra horizontal line before the top R=0.
  2. I would like for the nodes to be inside of circles.

Again, I'm not good with TikZ, and can't seem to get it to work. My code is below.

{
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

%TODO: fix top node
\begin{figure}[h]
\centering
\begin{tikzpicture}[grow=right, sloped, scale=0.8]
\node[bag] {$L$}
    child {
        node[bag] {$D$}        
            child {
                node[bag] {$P$}
                    child {
                        node[bag] {$C$}
                            child {
                                node[end, label=right:
                                {$\mathbf{R=1}$}] {}
                                edge from parent
                                %node[above] {$E$}
                                node[below]  {$C=0$}
                            }
                            child {
                                node[end, label=right:
                                {$\mathbf{R=0}$}] {}
                                edge from parent
                                %node[above] {$E$}
                                node[below]  {$C=1$}
                            }
                        edge from parent
                        %node[above] {$E$}
                        node[below]  {$P=0$}
                    }
                    child {
                        node[end, label=right:
                        {$\mathbf{R=0}$}] {}
                        edge from parent
                        %node[above] {$E$}
                        node[below]  {$P=1$}
                    }
                edge from parent
                %node[above] {$W$}
                node[below]  {$D=0$}
            }
            child {
                node[end, label=right:
                    {$\mathbf{R=0}$}] {}
                edge from parent
                %node[above] {$E$}
                node[below]  {$D=1$}
            }
            edge from parent 
            %node[above] {$W$}
            node[below]  {$L=0$}
    }
    child {
           child {
            node[end, label=right:
            {$\mathbf{R=0}$}] {}
            edge from parent
            %node[above] {$E$}
            %node[below]  {$C=0$}
           }
        edge from parent         
            %node[above] {$B$}
            node[below]  {$L=1$}
    };
\end{tikzpicture}
\end{figure}
}

If anyone who is better at TikZ than I am could help me out with this, it would be greatly appreciated.

  • 1. Bump the first R=0 node up on level. (Remove one child from the tree.) — 2. Add draw, circle to the bag style. (Which nodes did you mean?) Notes: Should \tikzset or \tikzstyle be used to define TikZ styles?. Also, you might want to look at the forest package or the new-ish graphs and graphsdrawing library of TikZ for an easier way to create trees. – Qrrbrbirlbel Apr 29 '15 at 18:46
  • Thanks for the comment. I went ahead and used the forest package, and the results can be seen in my answer. I found that much easier to understand, especially after reading this very helpful answer. – i love fish Apr 29 '15 at 19:10
1

I figured it out, with help from https://tex.stackexchange.com/a/240766/77231.

Here is my new picture:

tree

And my new code:

\documentclass[tikz]{standalone}
\usepackage{forest}
\begin{document}
\tikzset{
    decision/.style={circle, minimum height=10pt, minimum width=10pt, draw=black, fill=none, thick, inner sep=0pt},
    chance/.style={circle, minimum width=10pt, draw=black, fill=none, thick, inner sep=0pt},
  }
 \begin{forest}
    my label/.style={
      edge label={node[auto,sloped,pos=.75,anchor=south]{#1}}
    },
    for tree={
      grow=0,
      child anchor=west,
      anchor=west,
      text ragged,
      inner sep=1mm,
      edge={thick, draw=black},
      l sep+=15mm,
      s sep+=15mm,
      if n children=0{
        before typesetting nodes={
          label/.wrap pgfmath arg={right:#1}{content()},
          content={},
          chance,
        },
      }{},
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
      }
    }
    [, decision
      [, chance, my label={$L=0$}
        [, chance, my label={$D=0$}
          [, chance, my label={$P=0$}
            [{$\mathbf{R=1}$}, my label={$C=0$}
            ]
            [{$\mathbf{R=0}$}, my label={$C=1$}
            ]
          ]
          [{$\mathbf{R=0}$}, my label={$P=1$}
          ]
        ]
        [{$\mathbf{R=0}$}, my label={$D=1$}
        ]
      ]
      [{$\mathbf{R=0}$}, my label={$L=1$}
      ]
    ]
  \end{forest}
\end{document}

I think this is much neater and better. Thanks again to cfr's answer to https://tex.stackexchange.com/a/240766/77231!

  • 1
    (+1) Glad my answer helped you answer yourself - great! However, it is usually good to post a complete example i.e. starting with \begin{document} as this is more useful to more people. – cfr Apr 29 '15 at 20:21
  • Also, you can accept your own answer if it solves your problem satisfactorily ;). – cfr Apr 29 '15 at 20:26
  • Thanks for the edit and the advice. It won't let me accept my own answer for two days, but I will once I can. Thanks again! – i love fish Apr 30 '15 at 0:21

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.