# Drawing a tree with text on the edge concisely using TikZ

I am new to TikZ and am trying to draw a tree with text only on the edge (action/line). I have managed to do it as seen below but I was hoping there was a more concise/legible way.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

\begin{figure}
\begin{center}

\tikzstyle{level 1}=[level distance=1cm, sibling distance=2.5cm]
\tikzstyle{level 2}=[level distance=1cm, sibling distance=2.5cm]
\tikzstyle{level 3}=[level distance=1cm, sibling distance=1cm]
\tikzstyle{level 4}=[level distance=1cm, sibling distance=2cm]

\tikzstyle{circleNode}=[circle,draw]
\tikzstyle{dotNode} = [circle, minimum width=3pt,fill, inner sep=0pt]

\begin{tikzpicture}
\node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
edge from parent
node[left]{j}
}
edge from parent
node[left]{$\tau$*}
}
edge from parent
node[left]{b}
}
edge from parent
node[left]{$\tau$*}
}
edge from parent
node[left]{b}
}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
child{
node [dotNode] {}
edge from parent
node[right]{k}
}
edge from parent
node[right]{$\tau$*}
}
edge from parent
node[right]{b}
}
edge from parent
node[right]{$\tau$*}
}
edge from parent
node[right]{b}
}
edge from parent
node[left]{$\tau$*}
}
edge from parent
node[left]{a}
}
edge from parent
node[left]{$\tau$*}
}
edge from parent
node[left]{a}
};

\end{tikzpicture}
\end{center}

\end{document}

-
Welcome to TeX.sx! –  Rico Jan 22 '13 at 10:10
Check out the keys auto and swap. That is a proper way to place the nodes. Also you could introduce a insert path style that inserts edge from parent node[<option>]{<content>} in a shorter form, e.g. [n={swap}{$\tau$}]. –  Qrrbrbirlbel Jan 22 '13 at 12:59
From what I've read I should use [auto] and [auto, swap] instead of [left] and [right]? I'm still looking at resources on how to use insert path properly as I've only found specific examples on different TikZ types, not on trees. –  ET13 Jan 22 '13 at 13:51

You could check out my tikz-based tree-drawing package forest. The gallery in the documentation includes a style for easy drawing of decision trees (inspired by question How to change the level distance in tikz-qtree for one level only? on this site).

I have just generalized the style a bit and posted it to the forest style repository. Using the style-package forest-decisiontree, you could draw your tree like this:

\documentclass{minimal}
\usepackage{forest-decisiontree}
\begin{document}
\begin{forest} decision tree={3em}{8ex}{2pt}
[[;a[;\tau^*[;a[;\tau^*
[;b[;\tau^*[;b[;\tau^*[;j]]]]]
[;b[;\tau^*,for tree={decision on right}
[;b[;\tau^*[;k]]]]]
]]]]]
\end{forest}
\end{document}

-
This is great, thanks! For anyone who wants to use this you also need forest.sty (ctan.org/tex-archive/graphics/pgf/contrib/forest) which you unpack using the instructions here (en.wikibooks.org/wiki/LaTeX/…). –  ET13 Jan 24 '13 at 7:04
One more thing please, is there a way to include color in some of the text (but not all of the node) example [;\tau \color{blue}{test} ] but that's not compiling. –  ET13 Jan 24 '13 at 9:35
I was a bit careless when designing the style, so the content got expanded too soon (macros \getfirst and \getsecond were the culprits). Fixed (by adding \unexpanded) and uploaded. Now your example should work. –  Sašo Živanović Jan 24 '13 at 16:13
Confirmed working, thanks for your help. –  ET13 Jan 25 '13 at 7:30