3

I want to draw a horizontal decision tree. Can anyone help me to modify my code?

I'm new here. So if my question is not clear, pls let me know :)

Thanks a lot!

enter image description here

\documentclass[11pt]{article}
\usepackage{forest}

\forestset{
     declare toks={elo}{}, % Edge Label Options
     anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
     dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
     dot/.default=2pt,
     decision edge label/.style n args=3{
     edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}}
     },
     decision/.style={if n=1
     {decision edge label={left}{east}{#1}}
     {decision edge label={right}{west}{#1}}
     },
     decision tree/.style={
     for tree={
     s sep=0.5em,l=8ex,
     if n children=0{anchors=north}{
     if n=1{anchors=south east}{anchors=south west}},
     math content,
     },
     anchors=south, outer sep=2pt,
     dot=3pt,for descendants=dot,
     delay={for descendants={split option={content}{;}{content,decision}}},
     }
    }

\begin{document}

\begin{forest} decision tree
     [N,plain content
     [I;{p_1=0.5},plain content,elo={yshift=4pt}
     [{5,1};a]
     [II;b,plain content
     [{1,2};m]
     [{2,3};n]
     ]
     ]
     [II;{p_2=0.5},plain content,elo={yshift=4pt}
     [;c
     [{1,0};z]
     [{2,2};t]
     ]
     [;d
     [{3,1};z]
     [{0,0};t]
     ]
     ] {\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,above]{I};}
     ]
    \end{forest}

\end{document} 
  • @JouleV Thanks for your comment! I have tried to add rotate=90, but it didn't work. I think I put it in a wrong place.( ゚∀ ゚). – Ellis Mar 11 at 11:17
  • 2
    for tree = {grow=east and than change anchors and edge labels positions accordingly. – Zarko Mar 11 at 11:18
  • @Zarko Would you mind adding an answer? – user156344 Mar 11 at 11:33
  • i look to your code, but so far it seems that i'm lost in it. i will try to rewrite it asap. – Zarko Mar 11 at 11:36
  • @Ellis, any news? does receive answer help you? – Zarko May 1 at 22:54
3

Unfortunately my knowledge of the forest is too weak to be able to process your MWE. Therefore, as a starting point, how to draw a horizontal decision tree, let me show more simple forest tree code:

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

\begin{document}
\begin{forest}
/tikz/every label/.append style={inner sep=1pt,font=\footnotesize},
  for tree={
    grow'=east,       % <---
  % node style
    circle,
    minimum size=3pt,
    inner sep=0pt,
    outer sep=0pt,
    if n children=0{}{fill,fit=band},  
  % distances of nodes
      l sep=13mm,           
      s sep=0mm,            
  % edges
    edge={draw},
    edge path'={(!u.parent anchor) -- (.child anchor)},
    tier/.option=level,
  % labels on edges
   delay={edge label/.wrap value={node[midway, above, sloped, inner sep=1pt,
                                       font=\scriptsize]{#1}}
          },  % <---  
  }, % end for tree
[ , label=west:$N$
    [ , label=II, edge label={$p=0.5$}
        [ , edge label=$d$
           [{0,0}, edge label={$t$}]
           [{3,1}, edge label={$z$}]
        ]
        [ , edge label={$c$}
          [{2,2}, edge label={$t$}]
          [{1,0}, edge label={$z$}]
        ]
    ]{\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,label=left:I] {};}
    [ , label=I, edge label={$p=0.5$}
        [ , label=II, edge label={$b$}
          [{2,3}, edge label={$n$}]
          [{1,2}, edge label={$m$}]
        ]
        [{5,1}, edge label=$a$]
    ]
]
\end{forest}
\end{document}

which gives:

enter image description here

I hope, that extend of my tree code to your sophisticated solution will not make to much trouble to you. In the first spare time i will try to figured out your sophisticated MWE.


\documentclass[11pt]{article}
\usepackage{forest}

\forestset{
     declare toks={elo}{}, % Edge Label Options
     anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
     dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
     dot/.default=2pt,
     decision edge label/.style n args=3{
     edge label/.expanded={node[midway,#1=-2pt,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}}
     },
     decision/.style={if n=1
     {decision edge label={above left}{south east}{#1}}
     {decision edge label={below left}{north east}{#1}}
     },
     decision tree/.style={
     for tree={grow'=east,
     s sep=0.5em,l=8ex,
     if n children=0{anchors=west}{
     if n=1{anchors=south}{anchors=north}},
     math content,
     },
     anchors=south, outer sep=2pt,
     dot=3pt,for descendants=dot,
     delay={for descendants={split option={content}{;}{content,decision}}},
     }
    }

\begin{document}

\begin{forest} decision tree
     [N,plain content
     [I;{p_1=0.5},plain content,elo={yshift=4pt}
     [{5,1};a]
     [II;b,plain content
     [{1,2};m]
     [{2,3};n]
     ]
     ]
     [II;{p_2=0.5},plain content,elo={yshift=4pt}
     [;c
     [{1,0};z]
     [{2,2};t]
     ]
     [;d
     [{3,1};z]
     [{0,0};t]
     ]
     ] {\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,left]{I};}
     ]
    \end{forest}

\end{document} 

enter image description here

  • @marmot, thank you for edit. now is much, much better. now i should find the time asap to figured out your improvements :-) – Zarko Mar 11 at 16:03

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.