Drawing (upward facing, potentially unlabelled) binary trees and forests in LaTeX

diagramsforeststacking-symbolstrees

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.

Best Answer

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