2

I am trying to draw a probability tree, but I am overlapping two nodes, on the right, I should end up with 4 nodes, all at the same distance from the empty node, one above the other. But as I mentioned before, I am overlapping 2 of these, leaving only 3 visible. If anyone knows how I can solve it, I appreciate the help, thank you very much in advance!

\begin{tikzpicture}[grow=right,sibling distance=8em, level distance=3cm,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=blue!20}]]
  \node {}
    child { node {$A^c$}
      child { node {$B^c$}
        edge from parent node[below] { $P(B^c|A^c)$ } }
      child { node {$B$}
        edge from parent node[above] { $P(B|A^c)$ } }
      edge from parent node[above] { $P(A^c)$ } }
    child { node {$A$}
      child { node {$B^c$}
        edge from parent node[below] { $P(B^c|A)$ } }
      child { node {$B$}
        edge from parent node[above] { $P(A)$ } }
      edge from parent node[above] { $P(B|A)$ } };
\end{tikzpicture}
  • 2
    Simple answer: use forest. There are lots of examples on the site of these kinds of trees: https://tex.stackexchange.com/q/257800, https://tex.stackexchange.com/q/308575, https://tex.stackexchange.com/q/364996, https://tex.stackexchange.com/q/193848, https://tex.stackexchange.com/q/314282 etc. – Alan Munn May 11 '23 at 18:59

2 Answers2

1

You need to define different sibling distance for each level of tree. For example:

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

\begin{document} \begin{tikzpicture}[ grow=right, level distance=3cm, level 1/.style = {sibling distance=12em}, level 2/.style = {sibling distance=8em}, % every node/.style = {shape=rectangle, rounded corners, draw, align=center, top color=white, bottom color=blue!20} ] \node {} child { node {$A^c$} child { node {$B^c$} edge from parent node[below] { $P(B^c|A^c)$ } } child { node {$B$} edge from parent node[above] { $P(B|A^c)$ } } edge from parent node[above] { $P(A^c)$ } } child { node {$A$} child { node {$B^c$} edge from parent node[below] { $P(B^c|A)$ } } child { node {$B$} edge from parent node[above] { $P(A)$ } } edge from parent node[above] { $P(B|A)$ } }; \end{tikzpicture} \end{document}

enter image description here

Addendum:
A version of decision tree drawn by help of the forest package and with less fency edge labels. However they are aligned with them and automatically positioned above/below edges:

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

\begin{document} \begin{forest} for tree={ % nodes draw, rounded corners=2pt, where level=0{circle}{minimum width=2em}, top color=white, bottom color=blue!20, math content, % tree grow'=east, edge=semithick, child anchor=west, l sep=22mm, s sep=6mm, where level=1{s sep=2mm}{},% insert different s sep tier/.option = level, /tikz/ELS/.style = {% Edge Label Style pos=0.5, sloped, node font=\footnotesize, inner sep=2pt, anchor=#1}, EL/.style = {if n=1{% Edge Label, automatic positioned edge label={node[ELS=south]{$#1$}}} {edge label={node[ELS=north]{$#1$}}}} }, [ [A, EL=P(B|A) [B, EL=P(A)] [B^c, EL=P(B^c|A)] ] [A^c, EL=P(A^c) [B, EL=P(B|A^c)] [B^c, EL=P(B^c|A^c)] ] ] \end{forest} \end{document}

enter image description here

Zarko
  • 296,517
1

The child syntax is rather old and not very advances. In this simple case you can get away with setting different sibling distances but usually you want to go with more advanced solutions like the forest package.

Another option would be the graphsdrawing library (and its trees module) which requires LuaLaTeX. With the graphs library and its syntax, specifying the elements of the tree also reduces the typing.

Code

% !TeX TS-program = lualatex
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs, graphdrawing, quotes}
\usegdlibrary{trees}
\tikzset{math node/.style={execute at begin node=$, execute at end node=$}}
\begin{document}
\tikz\graph[
  tree layout, grow' = 0, level distance = 2cm,
  fresh nodes, math nodes,
  nodes = {
    shape = rectangle, rounded corners,
    draw, align = center,
    top color = white, bottom color = blue!20},
  edge quotes = {sloped, auto, node font = \footnotesize, math node},
  ]{
    root[as =, rounded corners = +.3333em] -- {
      A   [> "P(B|A)"] -- {
        B   [> "P(      A)" ],
        B^c [> "P(B^c | A)"']
      },
      A^c [> "P(A^c)"'] -- {
        B   [> "P(B  |A^c)" ],
        B^c [> "P(B^c|A^c)"']
      }
    }
  };
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821