# How can I align captions with each level of a tree drawn with tikz-qtree?

I have drawn a binary tree using the TikZ-qtree package.

1) I now would like to align captions on the right of tree with each of its level---and I would like the placement to be automatic (ideally I only have to specify that I want it at Xpx from the right border of the tree, and the vertical positioning is taken care of by itself).

2) I would like to be able to draw several brackets in the tree ranging some of the (contiguous) nodes. Ideally, again, I would only specify the nodes, and not have to take care about positioning at all!!

Here is the code I have at the moment:

\documentclass{article}

\usepackage{tikz, pgf}
\usepackage{tikz-qtree}

\def\tn#1{{\bf #1}}

\begin{document}
\begin{tikzpicture}
\Tree [.0
[.0
[.0
[.{\tn 0} ]
[.{\tn 1} ] ]
[.1
[.{\tn 2} ]
[.{\tn 3} ] ] ]
[.1
[.2
[.{\tn 4} ]
[.0
[.{\tn 0} ]
[.{\tn 1} ] ] ]
[.3
[.1
[.{\tn 2} ]
[.{\tn 3} ] ]
[.2
[.{\tn 4} ]
[.0 \edge[roof]; {... } ] ] ] ] ]
\end{tikzpicture}
\end{document}


And here is an illustration of what I aim to do:

Any help much appreciated.

-

For the captions, you can draw a second tree to the right of the first tree with the branch style set to [draw=none]. This will ensure that the captions line up.

For underlining sets of nodes, you need to make the starting and ending nodes using the \node command, and then draw the underline. For this part, I've used some very useful code supplied by Jake, which implements in TikZ the equivalent of the PSTricks \ncbar command.

I've also removed your \tn macro, which is not needed, since you can define a separate style for each terminal (leaf) node in tikz-qtree.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tikz-qtree}
% The following code is due to Jake
% and implements in tikz the equivalent of PSTrick's \ncbar command
% http://chat.stackexchange.com/transcript/message/4527875#4527875
\tikzset{
ncbar/.style={
to path=(\tikztostart)
-- ($(\tikztostart)!#1!90:(\tikztotarget)$)
-- ($(\tikztotarget)!#1!-90:(\tikztostart)$)
-- (\tikztotarget)
},
ncbar/.default=0.25cm
}
% end Jake's code

\begin{document}
\begin{tikzpicture}[every leaf node/.style={font=\bfseries}]
\Tree [.0
[.0
[.0
\node (0) {0};
1  ]
[.1
2
3 ] ]
[.1
[.2
\node (4) {4};
[.0
0
1 ] ]
[.3
[.1
2
3 ]
[.2
4
[.0 \edge[roof]; {... } ] ] ] ] ]
\draw[ncbar=-.4cm] (0) to (4);
\begin{scope}[xshift=2in]
\tikzset{edge from parent/.append style={draw=none},
every tree node/.append style={font=\sffamily\bfseries}
}
\Tree [[.{V=2} [.{V=4} [.{V=8} [.{V=5} ] ]]]]
\end{scope}
\end{tikzpicture}
\end{document}


-
Both solutions are perfect. Thank you very much, this helps a lot. :) –  Jérémie May 9 '12 at 10:00