1

I'm trying to figure out how to draw (forests of) potentially undecorated binary trees in Latex. So far, I've looked at the "forest" package, which I couldn't get to work with what I want. I'm hoping to draw binary trees that look like the following:

Tree with one root and 3 leaves

I'd also like to be able to draw forests of binary trees:

Forest with 3 trees

And finally, I'd like to be able to compose a forest of n trees with a tree of n leaves by vertical concatenation:

Composition of forest and tree

I appreciate that the last option might not exist as a built-in thing into a package, but figure I'd ask just in case.

Thanks!

Edit: Just in case anyone comes across this in the future -- the accepted solution works without error for me after updating my MacTex distribution and then compiling with XeLaTeX.

1 Answers1

1

Here is a macro \bintree that takes a tree as an argument using the syntax of the forest package.

enter image description here

is produced by the code:

\[
t=\bintree{[[[[][]][]]]} \hspace{2cm} 
f=\bintree{[[[[]]]]}\qquad\bintree{[[[[][]]]]}\qquad\bintree{[[[][[][]]]]}
\]

The macro uses a style defined in a \forestset. Here is the code:

\documentclass{article}

\usepackage{forest}

\forestset{bintree/.style={for tree={calign=fixed edge angles, grow'=north, edge=very thick, if n children=0{tier=x}{}}, delay={where content={}{shape=coordinate, for current and siblings={anchor=north}}{}}}}

\newcommand{\bintree}[1]{\vcenter{\hbox{\scalebox{.5}{\tiny\begin{forest}bintree #1 \end{forest}}}}}

\begin{document}

[ t=\bintree{[[[[][]][]]]} \hspace{2cm} f=\bintree{[[[[]]]]}\qquad\bintree{[[[[][]]]]}\qquad\bintree{[[[][[][]]]]} ]

\end{document}

The composition is not automated, but can be accomplished by copying and pasting the trees into the leaves. Here is the code for t above:

\bintree{[[[[<paste tree here>][<paste tree here>]][<paste tree here>]]]}

And with the components of f pasted in:

The composition $f\circ t=\bintree{[[[[[[[[]]]]][[[[[][]]]]]][[[[][[][]]]]]]]}$

enter image description here

Sandy G
  • 42,558
  • Wow, thank you so much! The code is working great for me to draw trees $t$ and the forest $f$. I'm just having a problem with the composition, I keep getting an error that says "Package PGF Math Error: You asked me to calculate '1/0.0', but I cannot divide any number by zero". I honestly have no idea what is going on there, but it only happens when I try to do the composition. Any idea what might be going on? – MathFrak96 May 31 '22 at 19:36
  • @MathFrak96: Are you using an old version of PGF? There used to be a bug related to this but if was fixed (I think) a few years ago. See this question for some discussion. – Sandy G Jun 03 '22 at 12:20
  • Good point. I updated my MacTex distribution and now I don't get the same error. However, I get an error that says "Dimension too large". The tree composition looks all wonky now (stuff is overlapping). Any idea on how to fix that? – MathFrak96 Jun 17 '22 at 23:01
  • For context, the composition now looks like this. – MathFrak96 Jun 17 '22 at 23:10