Please could you tell me if the Probability tree posted on 2006-12-14 by Kjell Magne Fauske can be done with three branches instead of just two.
[Tex/LaTex] LaTeX probability tree
tikz-pgftikz-treestrees
Related Solutions
Indeed the forest
branches are a bit tricky to get right, I suggest the next time you try building the lower part first, then copy that and paste it to make the upper (applying the changes to fit your desired output).
Since you use a node for the dots, you cannot enter the text as you would normally. If you do this is the result:
So to work around this, you can use the label command, which works like this (\cdots is the command for the dots):
[ ,name=g, label={right:$\cdots$}]
So the label is placed at the right, since this forest grows east.
Output
Code
\documentclass[margin=10pt]{standalone}
\usepackage{amsmath}
\usepackage{forest}
\tikzset{
dot/.style={circle,draw,inner sep=1.2,fill=black},
}
\begin{document}
\begin{forest}
for tree={s sep=60pt,l sep=2cm,dot,grow=0}
%
[ ,name=root
[ ,name=below
[ ,name=a
[ ,name=c, label={right:$\cdots$}]
[ ,name=d, label={right:$\cdots$}]
[ ,name=e, label={right:$\cdots$}]
]
[ ,name=f
[ ,name=g, label={right:$\cdots$}]
[ ,name=h, label={right:$\cdots$}]
] ]
[ ,name=above
[ ,name=i
[ ,name=k, label={right:$\cdots$}]
[ ,name=l, label={right:$\cdots$}]
]
[ ,name=m
[ ,name=n, label={right:$\cdots$}]
[ ,name=o, label={right:$\cdots$}]
[ ,name=p, label={right:$\cdots$}]
] ]
]
\end{forest}
\end{document}
Basically, with this approach to drawing trees, you have to figure out suitable spacing for the tree. TikZ doesn't do it for you.
In this case, the bag
nodes need to be wider (or you need to break the lines when mathematical expressions are too long). To avoid the crossings of edges and mixing up of nodes, you need to increase the sibling distance for level 1 relative to the sibling distance for level 2.
For example:
\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{trees}
\begin{document}
\tikzset{%
level 1/.style={level distance=3.5cm, sibling distance=4.5cm},
level 2/.style={level distance=3.5cm, sibling distance=2cm},
bag/.style={text width=50pt, text centered},
end/.style={circle, minimum width=3pt, fill, inner sep=0pt},
}
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {Urn $3G, 4R, 2W$}
child {
node[bag] {$2G, 4R, 2W$}
child {
node[end, label=right: {$P(G_1\cap G_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}$}] {}
edge from parent
node[above] {$G$}
node[below] {$\frac{1}{4}$}
}
child {
node[end, label=right: {$P(G_1\cap R_2)=\frac{1}{3}\times\frac{1}{2}=\frac{1}{6}$}] {}
edge from parent
node[above] {$R$}
node[below] {$\frac{1}{2}$}
}
child {
node[end, label=right: {$P(G_1\cap W_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}$}] {}
edge from parent
node[above] {$W$}
node[below] {$\frac{1}{4}$}
}
edge from parent
node[above] {$G$}
node[below] {$\frac{1}{3}$}
}
child {
node[bag] {$3G, 3R, 2W$}
child {
node[end, label=right: {$P(R_1\cap G_2)=\frac{4}{9}\times\frac{3}{8}=\frac{2}{5}$}] {}
edge from parent
node[above] {$G$}
node[below] {$\frac{3}{8}$}
}
child {
node[end, label=right: {$P(R_1\cap R_2)=\frac{4}{9}\times\frac{3}{8}=\frac{4}{15}$}] {}
edge from parent
node[above] {$R$}
node[below] {$\frac{3}{8}$}
}
child {
node[end, label=right: {$P(R_1\cap W_2)=\frac{4}{9}\times\frac{1}{4}=\frac{4}{15}$}] {}
edge from parent
node[above] {$W$}
node[below] {$\frac{1}{4}$}
}
edge from parent
node[above] {$R$}
node[below] {$\frac{4}{9}$}
}
child {
node[bag] {$3G, 4R, 1W$}
child {
node[end, label=right: {$P(W_1\cap G_2)=\frac{2}{9}\times\frac{3}{8}=\frac{1}{12}$}] {}
edge from parent
node[above] {$G$}
node[below] {$\frac{3}{8}$}
}
child {
node[end, label=right: {$P(W_1\cap R_2)=\frac{2}{9}\times\frac{1}{2}=\frac{1}{9}$}] {}
edge from parent
node[above] {$R$}
node[below] {$\frac{1}{2}$}
}
child {
node[end, label=right: {$P(W_1\cap W_2)=\frac{2}{9}\times\frac{1}{8}=\frac{1}{36}$}] {}
edge from parent
node[above] {$W$}
node[below] {$\frac{1}{8}$}
}
edge from parent
node[above] {$W$}
node[below] {$\frac{2}{9}$}
};
\end{tikzpicture}
\end{document}
Despite your saying you're not interested in alternative solutions, I cannot draw a tree without offering a Forest solution. Well, not usually.
The nice think about Forest is that the tree can be specified very concisely. Although I've not done so here, it would not be difficult to create the content of the final layer of nodes programmatically by collecting the labels above and below the edges.
The style probability tree
can be reused once defined. When used, it allows you to say my label=<above>:<below>
to specify the edge labels. The final tier's labels are specified within the nodes (or could be automated, as mentioned above) and the circles are created using a suitable 'arrow' for the edge
. s sep
and l sep
are used to ensure the tree is spread out sufficiently to accommodate the edge labels (because Forest can do this bit automatically), but no measures are needed to ensure that the tree's main nodes and edges do not conflict (Forest does do this automatically).
\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{forest}
\begin{document}
\forestset{%
declare toks={my label}{},
probability tree/.style={%
for tree={%
math content,
grow'=0,
child anchor=parent,
l sep+=40pt,
+edge={every node/.append style={sloped, midway}},
delay={%
split option={my label}{:}{above edge, below edge},
}
},
where n children=0{%
+edge={-{Circle[width=3pt,length=3pt]}},
if n=1{
!u.s sep+=20pt,
}{}
}{%
text width=50pt,
text centered,
if={isodd(n_children)}{%
for n/.wrap pgfmath arg={{##1}{calign with current edge}}{int(.5*(n_children()+1))}
}{},
},
above edge/.style={%
+edge label={node [above] {$##1$}},
},
below edge/.style={%
+edge label={node [below] {$##1$}},
},
},
}
\begin{forest}
probability tree
[{Urn $3G, 4R, 2W$}, plain content
[{2G, 4R, 2W}, my label=G:\frac{1}{3}
[{P(G_1\cap G_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}}, my label=G:\frac{1}{4}
]
[{P(G_1\cap R_2)=\frac{1}{3}\times\frac{1}{2}=\frac{1}{6}},my label=R:\frac{1}{2}
]
[{P(G_1\cap W_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}},my label=W:\frac{1}{4}
]
]
[{3G, 3R, 2W}, my label=R:\frac{4}{9}
[{P(R_1\cap G_2)=\frac{4}{9}\times\frac{3}{8}=\frac{2}{5}},my label=G:\frac{3}{8}
]
[{P(R_1\cap R_2)=\frac{4}{9}\times\frac{3}{8}=\frac{4}{15}},my label=R:\frac{3}{8}
]
[{P(R_1\cap W_2)=\frac{4}{9}\times\frac{1}{4}=\frac{4}{15}}, my label=W:\frac{1}{4}
]
]
[{3G, 4R, 1W}, my label=W:\frac{2}{9}
[{P(W_1\cap G_2)=\frac{2}{9}\times\frac{3}{8}=\frac{1}{12}},my label=G:\frac{3}{8}
]
[{P(W_1\cap R_2)=\frac{2}{9}\times\frac{1}{2}=\frac{1}{9}},my label=R:\frac{1}{2}
]
[{P(W_1\cap W_2)=\frac{2}{9}\times\frac{1}{8}=\frac{1}{36}},my label=W:\frac{1}{8}
]
]
]
\end{forest}
\end{document}
Best Answer
node[bag]
structure.child { node[end] {}}
to anode[bag]
structure.You can merely copy and paste the right parts to achieve it.
Here is a simplified example of http://www.texample.net/tikz/examples/probability-tree/ where another of each sorts of branches has been added: