[Tex/LaTex] Probability tree is colliding

diagramstikz-treestrees

I need to make a probability tree with 3 outcomes per event. Here is the code I am using:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

% 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]

\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}

Which produces output

How can I make this tree less "squeezed"?

I would rather not use an alternative, I've been using this same tree for other situations with just 2 outcomes per event.

Best Answer

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}

modified tree is less squished

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}

Forest tree