0

I am new to tikz and want to draw a recombining / merging binomail tree with the package forest. I originally used the solution found in Tikz-tree merging/recombining nodes, but since I also use non-merging trees with forrest, I would prefer a single solution. Thus, is it possible to merge both nodes labeled 5 below?

enter image description here

\documentclass{article}

\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{automata}
\usetikzlibrary{calc,arrows.meta,positioning}

\newcommand{\iddots}{\reflectbox{$\ddots$}}

\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{forest}
for tree={
     semithick,
    minimum size=1.3em, inner sep=0pt,
    math content,
% tree
    l sep =12mm,
    s sep = 6mm,
/tikz/arr/.style = { -{Triangle[angle=45:2pt 3]}, shorten >=1pt},
/tikz/ELS/.style = {% Edge Label Style
              pos=0.5, node
              font=\footnotesize, text=blue, anchor=#1},
EL/.style={if n=1{edge label={node[ELS=east]{$#1$}}}
                 {edge label={node[ELS=west]{$#1$}}}}
%%%% diagram body
[1, label=$\tilde{S}^1$, tier=0
    [2, EL=1-p
        [4, EL=1-p]
        [5, EL=p]
    ]
    [3, EL=p
        [5, EL=1-p]
        [6, EL=p]
    ]
]
\end{forest}


\end{document}
2

Like this?

enter image description here

With use of fixed edge angles option, trick with negative s sep equal to nodes width.

Edit: with improved edges labels style:

\documentclass[border=3.141592]{standalone}
%\documentclass{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{forest}
for tree={
% nodes
    circle, draw, semithick,
    minimum size=1.2em, inner sep=0pt, outer sep=0pt, %<--- 
    math content,
% tree with fixed angles of edges
   l sep = 11mm,              % <---
  calign = fixed edge angles, % <---
  calign angle = 30,          % <---
   s sep=-1.2em,              % <---
for current and siblings = {anchor=center},
/tikz/arr/.style = {draw=cyan!60, -{Triangle[angle=45:2pt 3]}, 
                    shorten <=\pgflinewidth, shorten >=1pt},
    edge = arr,
%% edge labels
/tikz/ELS/.style = {% Edge Label Style
    pos=0.5, node font=\scriptsize, inner sep=0pt,
    text height=2ex, text depth=0.5ex, text=cyan, anchor=south #1},
EL/.style={if n=1{edge label={node[ELS=east]{$#1$}}}
                 {edge label={node[ELS=west]{$#1$}}}}
        }% end of "for tree"
%%%% diagram body
[1, label=$\tilde{S}^1$
    [2, EL=1-p
        [4, EL=q]
        [5,EL=1-q]
    ]
    [3, EL=p
        [, EL=q]
        [6, EL=1-q]
    ]
]
    \end{forest}
\end{document}

Addendum: or the following form:

enter image description here

\documentclass[border=3.141592]{standalone}
%\documentclass{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{forest}
for tree={
% nodes
    circle, draw, semithick,
    minimum size=1.2em, inner sep=0pt,
    anchor=north,   % <--- new
    math content,
% tree
    l sep =12mm,
    s sep = 3mm,
/tikz/arr/.style = {draw=cyan!60, shorten >=1pt, 
                    -{Triangle[angle=45:2pt 3]}},
     edge = arr,
%% edge labels
/tikz/ELS/.style = {% Edge Label Style
    pos=0.5, node font=\tiny, inner sep=0pt,    % <--- changed
    text height=2ex, text depth=0.5ex, text=cyan, anchor=south #1},
EL/.style={if n=1{edge label={node[ELS=east]{$#1$}}}
                 {edge label={node[ELS=west]{$#1$}}}}
        }% end of "for tree"
%%%% diagram body
[1, label=$\tilde{S}^1$
    [2, EL=1-p, calign = last, name=n2  % <--- added calign 
        [4, EL=q]
        [,phantom]          % <--- changed
    ]
    [,phantom[5, name=n5]]  % <--- new
    [3, EL=p, calign = first, name=n3  % <--- added calign 
        [,phantom]          % <--- changed
         [6, EL=1-q]
    ]
]
\draw[arr] (n2) -- (n5) node[ELS=west] {$1-q$};
\draw[arr] (n3) -- (n5) node[ELS=east] {$q$};
    \end{forest}
\end{document}
2
  • yeah, exactly like this. Since you use fixed angles, does that mean that we can not scale the tree (e.g. make a new generationi?). Why use s sep=-1.2em?
    – Tamar
    Apr 22 at 12:30
  • @Tamar, about scaling: try it. Let me know if it doesn't work. Trick with s sep is used that bot nodes containing 5 (actually one is empty) perfectly overlap.
    – Zarko
    Apr 22 at 12:37

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.