# How to draw custom nodes attached to tikz-qtree?

I want to draw a tree which is similar to the Merge sort recursion tree in this Tikz sample from TeXample.net:

However, I don't want to draw all the + and = signs, I just want two columns: one from the left and one from the right. I'm using tikz-qtree package. I wonder is there a way to add custom node to this tree structure?

This is my current tree:

\documentclass[10pt,letterpaper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{cancel}
\usepackage{tikz-qtree}

\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}}}
\begin{tikzpicture}[sibling distance=.5cm]
\matrix{
\Tree
[.n
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
]
&
\\};
\end{tikzpicture}

\end{document}


Edit My current solution is to use tikz-nodes, but it looks a little odd:

\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}}}
\begin{tikzpicture}[sibling distance=0.5cm,level distance=1.5cm,node distance=1.5cm,auto,on grid,initial text=]
\matrix{
\Tree
[.n
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
]
&
\node (0)               {$= n$};
\node (1) [below=of 0]  {$= 3 \cdot \dfrac{n}{2}$};
\node (2) [below=of 1]  {$= 3^2 \cdot \dfrac{n}{2^2}$};
\\};
\end{tikzpicture}

-

A simpler way to do this is to just use two trees and shift the second one (and change its branch properties) using TikZ's scope mechanism:

\documentclass[10pt,letterpaper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{cancel}
\usepackage{tikz-qtree}

\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}}}
\begin{tikzpicture}[sibling distance=.5cm]
\Tree
[.n
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
[.$\dfrac{n}{2}$ [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] [.$\dfrac{n}{4}$ ] ]
]

\begin{scope}[xshift=2.5in]
\tikzset{edge from parent/.style={edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},every tree node/.style={text width=4em,align=left}}
\Tree [.{$=n$}
[.{$= 3 \cdot \dfrac{n}{2}$}
[.{$= 3^2 \cdot \dfrac{n}{2^2}$} ]]]
\end{scope}
\end{tikzpicture}

\end{document}


-
Very nice solution, great thanks although it has been unanswered for a while. – Chan Jul 8 '12 at 7:08
@Chan I usually catch most tikz-qtree questions, but this one must have come at a very busy time. – Alan Munn Jul 8 '12 at 12:49
Your hard work showed. It was very kind of you to inform me that though. – Chan Jul 8 '12 at 23:38

Here's a way of drawing the tree with the experimental wrapper for forest, justtrees.sty (v 0.05). If anybody would like a copy, let me know you are prepared to be a guinea pig!

This also shows how to encapsulate the right column in red, just in case you felt like doing that. (I'm not clear if this was desired or not but it is easy to remove that bit if you don't want it.)

The idea of this solution is that you specify the annotations as part of the main tree itself by just adding just={<annotation>} and the label will be added and automatically aligned with the current level of the tree.

For example:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{amsmath,amssymb}
\usepackage{justtrees}% v 0.05
\begin{document}
\begin{justtree}
{%
for tree={
just format={xshift=1pt},
math content,
parent anchor=,
child anchor=,
},
}
[n, just=$n$
[\dfrac{n}{2}, just=$3\cdot\dfrac{n}{2}$
[\dfrac{n}{4}, just=$3^2\cdot\dfrac{n}{2^2}$, tikz+={\draw [red] (right just 1.north west) rectangle (right just 3.south east);}]
%     [\dfrac{n}{4}, just=$3^2\cdot\dfrac{n}{2^2}$]% uncomment this line and comment the previous one to eliminate the red box.
[\dfrac{n}{4}]
[\dfrac{n}{4}]
]
[\dfrac{n}{2}
[\dfrac{n}{4}]
[\dfrac{n}{4}]
[\dfrac{n}{4}]
]
[\dfrac{n}{2}
[\dfrac{n}{4}]
[\dfrac{n}{4}]
[\dfrac{n}{4}]
]
]
\end{justtree}
\end{document}


-