2

In a tree made with TikZ, I would like to label edges of the tree. I saw examples here on StackExchange of using edge from parent node{label}, but this only seems to work for the very last edge of the tree, leading down to the leaves. Here's an example of what I mean (code below the pictures).

First I create my tree in TikZ and label an edge with edge from parent node{...}, and this works fine for the edge connecting to a leaf:

Labeling an edge

However, if I try to do the same thing to label the edge between B and BB, the following happens:

Label didn't work

Here is the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=3cm},
    level 3/.style={sibling distance=1.5cm},
    edge from parent/.style={draw}]
  \node{$\bullet$}
  child {node {\texttt{B}} 
    child {node{\texttt{BB}}
      child {node{\texttt{BBB}} edge from parent node[left]{$\frac{3}{4}$}}
      child {node{\texttt{BBG}}}
    }
    child {node{\texttt{BG}}
      child {node{\texttt{BGB}}}
      child {node{\texttt{BGG}}}
    }
  }
  child {node {\texttt{G}}
    child {node{\texttt{GB}}
      child {node{\texttt{GBB}}}
      child {node{\texttt{GBG}}}
    }
    child {node{\texttt{GG}}
      child {node{\texttt{GGB}}}
      child {node{\texttt{GGG}}}
    }
  };
\end{tikzpicture}

\begin{tikzpicture}[level distance=1.5cm,
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=3cm},
    level 3/.style={sibling distance=1.5cm},
    edge from parent/.style={draw}]
  \node{$\bullet$}
  child {node {\texttt{B}} 
    child {node{\texttt{BB}} edge from parent node[left]{$\frac{2}{3}$}
      child {node{\texttt{BBB}} edge from parent node[left]{$\frac{3}{4}$}}
      child {node{\texttt{BBG}}}
    }
    child {node{\texttt{BG}}
      child {node{\texttt{BGB}}}
      child {node{\texttt{BGG}}}
    }
  }
  child {node {\texttt{G}}
    child {node{\texttt{GB}}
      child {node{\texttt{GBB}}}
      child {node{\texttt{GBG}}}
    }
    child {node{\texttt{GG}}
      child {node{\texttt{GGB}}}
      child {node{\texttt{GGG}}}
    }
  };
\end{tikzpicture}
\end{document}
4

The edge from parent node must be added after all of the child nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=3cm},
    level 3/.style={sibling distance=1.5cm},
    edge from parent/.style={draw}]
  \node{$\bullet$}
  child {node {\texttt{B}} 
    child {node{\texttt{BB}}
      child {node{\texttt{BBB}}
        edge from parent node[left]{$\frac{3}{4}$}
      }
      child {node{\texttt{BBG}}}
      edge from parent node[left,xshift=-4pt]{$\frac{2}{3}$}%<- moved
    }
    child {node{\texttt{BG}}
      child {node{\texttt{BGB}}}
      child {node{\texttt{BGG}}}
    }
  }
  child {node {\texttt{G}}
    child {node{\texttt{GB}}
      child {node{\texttt{GBB}}}
      child {node{\texttt{GBG}}}
    }
    child {node{\texttt{GG}}
      child {node{\texttt{GGB}}}
      child {node{\texttt{GGG}}}
    }
  };
\end{tikzpicture}
\end{document}

enter image description here

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.