I am trying build a tree bottom up in a beamer document, i.e. to draw it on screen starting from the most deeply embedded leaves. I've found a lot of Q&As and discussions on how to have trees unfold from the root, on this page and elsewhere, but what I'm trying to do is the reverse, and as far as I can tell many of the methods proposed don't work in this scenario - e.g., I can't seem to suppress a parent node while displaying the children in a standard tikz-tree.
Ideally, the method would use the size of the complete tree to compute positions all along so that the image doesn't wobble from slide to slide. I'm using qtree because it's what I grew up with, but I am very much open to solutions using other packages with different syntax.
What I've come up with are the following:
Drawing the entire tree (in
qtreeor otherwise), and only revealing the nodes and leaves closer to the root later. Downside: The unlabeled skeleton of the tree remains visible throughout.Copy-pasting different size chunks of the tree, and using overprint to display them on successive slides of the same frame. Downside: The size of the largest, complete tree is inaccessible when the smaller subtrees are drawn, so need an extra step of pushing the smaller trees to the right and bottom to get them anywhere near on top of each other (my snapshot below isn't actually quite there).
So what I am looking for is either a variant of (1) that makes the higher edges invisible alongside with the suppressed nodes, or a variant of (2) that automatically extracts the dimensions of the largest tree and uses them as input for the absolute positions of the smaller ones, whichever works.
Here's some working code for both approaches:
\documentclass[presentation]{beamer}
\usepackage{qtree, tikz}
\usepackage[english]{babel}
\begin{document}
\begin{frame}{\texttt{qtree}}
\Tree
[.\visible<6>{root}
\visible<6>{\alert<6>{leaf 1}}
[.\visible<5->{level1node}
\visible<5->{\alert<5>{leaf 2}}
[.\visible<4->{level2node}
\visible<4->{\alert<4>{leaf 3}}
[.\visible<3->{level3node}
\visible<3->{\alert<3>{leaf 4}}
[.\visible<2->{level4node}
\visible<2->{\alert<2>{leaf 5}}
[.{level5node}
\alert<1>{leaf 6}
{leaf 7}
]
]
]
]
]
]
\end{frame}
\begin{frame}{overprint version}
This is my tree:
\vskip0pt plus 1filll
\begin{flushright}
\begin{overprint}
\onslide<6>
\Tree
[.{root}
{\alert<6>{leaf 1}}
[.{level1node}
{\alert<5>{leaf 2}}
[.{level2node}
{\alert<4>{leaf 3}}
[.{level3node}
{\alert<3>{leaf 4}}
[.{level4node}
{\alert<2>{leaf 5}}
[.{level5node}
\alert<1>{leaf 6}
{leaf 7}
]
]
]
]
]
]
\onslide<5>
\Tree
[.{level1node}
{\alert<5>{leaf 2}}
[.{level2node}
{\alert<4>{leaf 3}}
[.{level3node}
{\alert<3>{leaf 4}}
[.{level4node}
{\alert<2>{leaf 5}}
[.{level5node}
\alert<1>{leaf 6}
{leaf 7}
]
]
]
]
]
\onslide<4>
\Tree
[.{level2node}
{\alert<4>{leaf 3}}
[.{level3node}
{\alert<3>{leaf 4}}
[.{level4node}
{\alert<2>{leaf 5}}
[.{level5node}
\alert<1>{leaf 6}
{leaf 7}
]
]
]
]
\onslide<3>
\Tree
[.{level3node}
{\alert<3>{leaf 4}}
[.{level4node}
{\alert<2>{leaf 5}}
[.{level5node}
\alert<1>{leaf 6}
{leaf 7}
]
]
]
\onslide<2>
\Tree
[.{level4node}
{\alert<2>{leaf 5}}
[.{level5node}
\alert<1>{leaf 6}
{leaf 7}
]
]
\onslide<1>
\Tree
[.{level5node}
\alert<1>{leaf 6}
{leaf 7}
]
\end{overprint}
\end{flushright}
\end{frame}
\end{document}
Edit:
On the basis of Lay's answer, I managed to create the following branching tikz tree which comes close, but I'm still not quite happy: the edge that connects the currently displayed subtree to the rest of the tree is shown too early - I get a funny tail towards the rest of the tree that doesn't yet exist. If I adapt the position of the tikzstyle-change, the edges are drawn too late:
\begin{frame}
\begin{tikzpicture}
\tikzstyle{lvl0}=[transparent]
\tikzstyle{lvl1}=[transparent]
\tikzstyle{lvl2}=[transparent]
\tikzstyle{lvl3}=[transparent]
\tikzstyle{lvl4}=[transparent]
\tikzstyle{lvl5}=[opaque]
\only<2->{\tikzstyle{lvl4}=[opaque]}
\only<3->{\tikzstyle{lvl3}=[opaque]}
\only<4->{\tikzstyle{lvl2}=[opaque]}
\only<5->{\tikzstyle{lvl1}=[opaque]}
\only<6->{\tikzstyle{lvl0}=[opaque]}
\node [lvl0] {root}
child [lvl0] { node {leaf 1} }
child [lvl1] { node {lvl1}
child { node [lvl1] {leaf 2} }
child [lvl2]{ node {lvl2}
child { node [lvl2] {leaf 3} }
child [lvl3] { node {lvl3}
child { node [lvl3] {leaf 4} }
child { [lvl4] node {lvl4}
child { node [lvl4] {leaf 5} }
child { [lvl5] node {lvl5}
child { node [lvl5] {leaf 6} }
child { node [lvl5] {leaf 7} }
}
}
}
}
};
\end{tikzpicture}
\end{frame}

forestpackage offers helpful keys likefor ancestorsorfor all previouswhich could be very helpful in combination with Using beamer overlays with forest generated trees. – Qrrbrbirlbel May 07 '13 at 01:13