# [Tex/LaTex] Probability tree using TikZ for Bernoulli experiments

tikz-pgftikz-trees

Consider the tree below. Is there an easy way to label every left edge q, every right edge p, every left node f and every right node r, without doing it manually for every level?

Second question: Is there a nice way to generalize this Bernoulli process tree example for n-levels?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}

\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=6cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=3cm]
\tikzstyle{level 3}=[level distance=3.5cm, sibling distance=2cm]

\tikzstyle{bag} = [text centered,circle,draw,inner sep=0.3em]

\begin{tikzpicture} %
child { %
node[bag] {} %
child{ %
node[bag]{}
child { %
node[bag, label=below: %
{$a$}] {} %
} %
child { %
node[bag, label=below: %
{$b$}] {} %
} %
}%
child{ %
node[bag]{}
child { %
node[bag, label=below: %
{$c$}] {} %
} %
child { %
node[bag, label=below: %
{$d$}] {} %
} %
}%
}
child { %
node[bag] {} %
child{ %
node[bag]{}
child { %
node[bag, label=below: %
{$e$}] {} %
} %
child { %
node[bag, label=below: %
{$f$}] {} %
} %
}%
child{ %
node[bag]{}
child { %
node[bag, label=below: %
{$g$}] {} %
} %
child { %
node[bag, label=below: %
{$h$}] {} %
} %
}%
}; %             %
\end{tikzpicture}

\end{document}


I work on something like that but I need to work on this code (no time actually) You can look at \pgfqkeys{/berntree} to see all the options. The limit is 5 but it would be nice to use a recurrent method to build this tree ! I made some modifications now you can modify the styles : node success stylean node miss style. To add some labels at the end of each branch , you can use the last nodes ( it's not very easy) and for the label on the edges you can use edge style. The code is not very fine because the arrows are inside some nodes and it would be better to give the same size for each nodes. Version 4 : edge style/.style = {->,>=latex',shorten <= 6pt}for the edge's problem and see the final loops for the labels. I fixed the size's nodes

\documentclass{scrartcl}
\usepackage{pgf,tikz}
\usetikzlibrary{trees,arrows,calc}

\makeatletter
\newcount\tkz@Berndepth
\newdimen\tkz@BernLEN
\tkz@BernLEN=24em
\def\tkzBernTreeSet#{\pgfqkeys{/berntree}}

\pgfqkeys{/berntree}{%
success/.code      = \def\tkz@bern@success{#1},
miss/.code         = \def\tkz@bern@miss{#1},
p/.code            = \def\tkz@bern@pbsuccess{#1},
q/.code            = \def\tkz@bern@pbmiss{#1},
node success style/.style  = {inner sep=2pt,outer sep=3pt},
node miss style/.style  = {inner sep=2pt,outer sep=3pt},
edge style/.style  = {->,>=latex',shorten <= 6pt},
root style/.style  = {draw,circle},
success/.initial   = S,
miss/.initial      = E,
p/.initial         = $p$,
q/.initial         = $1-p$,
gap/.code          = \def\tkz@bern@gap{#1},
length/.code       = \def\tkz@bern@length{#1}
}

\def\tkz@brntree#1#2{%
\node[/berntree/root style] {};
\begin{scope}[level distance=\tkz@bern@length,
level 1/.style={sibling distance=#2}]
\node[] (root) at (#1) {}
[grow=right]
child[/berntree/edge style] {%
node[/berntree/node miss style](tkz@E\the\tkz@Berndepth) {\tkz@bern@miss}
edge from parent node[fill=white] {\tkz@bern@pbmiss}}
child [/berntree/edge style] {%
node[/berntree/node success style] (tkz@S\the\tkz@Berndepth) {\tkz@bern@success}
edge from parent node[fill=white] {\tkz@bern@pbsuccess}
};
\end{scope}}%

\def\tkzBernTree{\pgfutil@ifnextchar[{\tkz@BernTree}{\tkz@BernTree[]}}
\def\tkz@BernTree[#1]#2{%
\begingroup
\pgfqkeys{/berntree}{%
success = S,
miss= E,
node success style/.style  = {inner sep=2pt,outer sep=3pt,draw,minimum width=1.5em,minimum height=1.5em},
node miss style/.style  = {inner sep=2pt,outer sep=3pt,circle,draw,minimum width=1.5em},
p=$p$,
q=$q$,
gap=8cm,
length=3cm}
\pgfqkeys{/berntree}{#1}
\tkz@BernLEN=\tkz@bern@gap\relax
\tkz@Berndepth 0\relax
\node (tkz@S0) at (0,0){};
\def\tkz@bn@level{#2}
\ifcase\tkz@bn@level%
\or%
\tkz@brntree{tkz@S0}{\tkz@BernLEN}
\or%
\tkz@brntree{tkz@S0}{\tkz@BernLEN}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {1}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\or%
\tkz@brntree{tkz@S0}{\tkz@BernLEN}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {1}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {2,3}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\or%
\tkz@brntree{tkz@S0}{\tkz@BernLEN}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {1}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {2,3}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {4,5,6,7}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\or%
\tkz@brntree{tkz@S0}{\tkz@BernLEN}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {1}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {2,3}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {4,...,7}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\divide \tkz@BernLEN by 2 %
\foreach \nd in {8,...,15}{
\tkz@brntree{tkz@S\nd}{\tkz@BernLEN}
\tkz@brntree{tkz@E\nd}{\tkz@BernLEN}}
\fi
\endgroup
}
\makeatother
\begin{document}

\begin{tikzpicture}[yscale=1.2]
\tkzBernTree[root style/.style  = {fill,circle,outer sep =1pt,inner sep=2pt}]{4}
\makeatletter
\foreach \num/\lab in {8/a,9/c,10/e,11/g,12/i,13/k,14/m,15/o}{%
\node at ($(tkz@S\num)+(2,0)$){$\lab$};
}
\foreach \num/\lab in {8/b,9/d,10/f,11/h,12/j,13/l,14/n,15/p}{%
\node at ($(tkz@E\num)+(2,0)$){$\lab$};
}
\makeatother
\end{tikzpicture}

\end{document}