Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

enter image description here

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}
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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}

output of code

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.