This is more "proof of concept" than a fully working example; the point of it is as follows. Firstly, to show how to build up a tree recursively using a \foreach
loop. Secondly, it uses the tree
stuff already in TikZ. Thirdly, someone just mentioned the Htree to me and in figuring out how to draw that, I had an answer-in-search-of-a-question and it occurred to me that with a slight modification, the routine for drawing the H-tree could be adapted to a full binary tree. What's missing is the labelling facilities, though these could be added in without too much difficulty, I'm sure.
Here's the results, first the H-tree:
and then the binary tree:
And now the code:
\documentclass{standalone}
\usepackage{tikz}
\makeatletter
\tikzset{
htree leaves/.initial=2,
sibling angle/.initial=20,
htree level/.initial={}
}
\def\htree@growth{%
\pgftransformrotate{%
(\pgfkeysvalueof{/tikz/sibling angle})*(-.5-.5*\tikznumberofchildren+\tikznumberofcurrentchild)}%
\pgftransformxshift{\the\tikzleveldistance}%
\pgfkeysvalueof{/tikz/htree level}%
}
\tikzstyle{htree}=[
growth function=\htree@growth,
sibling angle=180,
htree level={
\tikzleveldistance=.707\tikzleveldistance
\pgfsetlinewidth{.707*\the\pgflinewidth}
}
]
\tikzstyle{btree}=[
growth function=\htree@growth,
sibling angle=60,
htree level={
\tikzleveldistance=.55\tikzleveldistance
\pgfsetlinewidth{.707*\the\pgflinewidth}
}
]
\long\def\ge@addto@macro#1#2{%
\begingroup
\toks@\expandafter\expandafter\expandafter{\expandafter#1#2}%
\xdef#1{\the\toks@}%
\endgroup}
\newcommand{\htree}[2][]{%
\def\htree@start{\noexpand\coordinate}
\def\htree@end{}
\foreach \l in {0,...,#2} {
\g@addto@macro\htree@start{child foreach \noexpand\x in {1,2} {\iffalse}\fi}
\g@addto@macro\htree@end{\iffalse{\fi}}
\global\let\htree@start\htree@start
\global\let\htree@end\htree@end
}
\edef\htree@cmd{\htree@start\htree@end;}
\begin{scope}[htree,#1]
\htree@cmd
\end{scope}
}
\makeatother
\begin{document}
\begin{tikzpicture}[
rotate=-90,
yscale=.5
]
\htree[
btree,
level distance=3cm,
line width=8pt,
]{7}
\end{tikzpicture}
\end{document}
It works by building up the appropriate {child foreach \x in {1,2}
list. We have to be a bit sneaky about groupings (there may be a sneakier way). The rest is just modified from the trees
library in TikZ. I added a hook in to the growth
routine so that it was easy to define a style for each level as "some modification of the previous style", instead of having to define styles for each level specifically. Using a different growth function could result in something a little more like the example given in ESultanik's answer - the main point of this answer was to build up the tree using a loop.
tree
can be made by the package ctan.org/tex-archive/graphics/tree .It is aimed at linguists, but I guess both computer scientists as well as mathematicians can be permitted to use it! With John's full denunciation of himself in the sample!qtree
, along withtikz-qtree
, instead of that; I demonstrated it in another answer. Its biggest advantage is that you don't have to preprocess your LaTeX source (ick); it also generalizes to TikZ if you want it to. Again aimed at linguists, but really should be more widely known and used. I don't think it's as automatic as what Amir wants, though.