12

Is there some good tool for drawing binary trees with labels that are rendered by latex? I would need to have the tree node placement done automatically for me, because there are too many labels to calculate their placing manually.

To be more specific. I can easily control the output format of my data. What I can't do is output into a format, where I have to calculate the coordinates for each node. I'd like to output the data into a format for a tool that can calculate the graph layout.

EDIT: I noticed that the solution below doesn't seem to accept math in the labels. For example the code below won't work, but if I drop the dollars around the label "x", then it will. This is a problem, because my labels are 2x2-matrices.

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows.meta}
\usegdlibrary{trees}
\begin{document}
    \begin{tikzpicture}[>=Stealth]
        \graph[binary tree layout]
        {
            root->{$x$->{}}
        };
    \end{tikzpicture}
\end{document}
Adriaan
  • 3,675
  • 2
  • 21
  • 41
  • Similar problem is solved here or here using TkiZ. – Václav Pavlík Sep 27 '14 at 08:14
  • @VáclavPavlík "too many labels" suggests that OP's tree may be considerably larger than in the answers cited. Just how well does tikz or tikz-qtree scale to large trees? – Brent.Longborough Sep 27 '14 at 08:45
  • I have almost 100 nodes and the data changes fairly often, so I just mainly want to edit some labels and remove/add some links and then recompile. If I have to specify any coordinates, then I'll rather draw it with paintbrush. – Edvard Fagerholm Sep 27 '14 at 08:46
  • @EdvardFagerholm Then could you bring us a full MWE? Is very hard to guess the solution blindly. – Aradnix Sep 27 '14 at 09:09
  • I can't show you a specific example, because for that I would already need to specify an output format. I've made the question more precise if it helps. – Edvard Fagerholm Sep 27 '14 at 09:24
  • To be more precise, I could of course calculate the coordinates, but this would require implementing one of the fairly complicated graph layout algorithms. This is what I don't want. – Edvard Fagerholm Sep 27 '14 at 09:25
  • 2
    For your edit, you need "$x$". All of these and much more is given in the PGF manual. – percusse Sep 27 '14 at 13:49

1 Answers1

16

The most recent release of PGF has a number of graph drawing algorithms (requiring lualatex) including a version of the Reingold–Tilford method and can easily handle large numbers of nodes.

In the simplest case a tree can be specified like this:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows.meta}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[>=Stealth]
\graph[binary tree layout]{
  a -> {   
    b -> { 
      c -> { 
        d -> { e, f }, 
        g 
      }, 
    h -> { i, j }
    },
    k -> {
      l -> {
        m -> { n, o },
        p -> { q, r }
      }, 
      s -> {
        v -> {w, x},
        y -> {z}
      }
    }
  }
};
\end{tikzpicture}
\end{document}

enter image description here

It is also possible to create "graph macros" which mean the graph specification can be created more-or-less automatically, even using lua:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphdrawing,graphs.standard,arrows.meta}
\usegdlibrary{trees}
\begin{document}
\tikzgraphsset{%
  levels/.store in=\tikzgraphlevel,
  levels=1,
  declare={full_binary_tree}{[
    /utils/exec={
      \edef\treenodes{%
\directlua{%
  function treenodes(l)
    if l == 0 then
      return "/"
    else
      return "/ [layer distance=" .. l*10 .. "]-- {" .. treenodes(l-1) .. ", " .. treenodes(l-1) .. "}"
    end
  end
  tex.print(treenodes(\tikzgraphlevel) .. ";")
}%
      }
    },
    parse/.expand once=\treenodes 
  ]}
}
\begin{tikzpicture}
\graph[binary tree layout, grow=down, sibling distance=5pt, significant sep=0pt, nodes={fill=red, draw=none, circle, inner sep=2.5pt, outer sep=0pt}]{
   full_binary_tree [levels=7];
};
\end{tikzpicture}
\end{document} 

enter image description here

Mark Wibrow
  • 70,437