[Tex/LaTex] Using TikZ to create a tree with root node at the bottom

tikz-pgftikz-trees

Is it possible to draw a tree with the root node at the bottom? The image below demonstrates what I'd like, but is upside down.

Tree diagram

I've looked into similar questions, most notably

How do I draw a B+ tree in latex?

But they haven't been entirely helpful.

Best Answer

You can use the grow key, described on page 218 of the pgfmanual:

This key is used to define the direction in which the tree will grow. The direction can either be an angle in degrees or one of the following special text strings: down, up, left, right, north, south, east, west, north east, north west, south east, and south west. All of these have “their obvious meaning,” so, say, south west is the same as the angle -135◦.

In your case, you can use grow=up; a little example:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
[level distance=10mm,
every node/.style={fill=red!60,circle,inner sep=1pt},
level 1/.style={sibling distance=20mm,nodes={fill=red!45}},
level 2/.style={sibling distance=10mm,nodes={fill=red!30}},
level 3/.style={sibling distance=5mm,nodes={fill=red!25}}]
\node  {31} [grow=up]
child {node {30}
child {node {20}
child {node {5}}
child {node {4}}
}
child {node {10}
child {node {9}}
child {node {1}}
}
}
child {node {20}
child {node {19}
child {node {1}}
child[missing]
}
child {node {18}}
};
\end{tikzpicture}

\end{document}

enter image description here

Depending on the desired order for the nodes, you could use grow' instead:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}

\begin{tikzpicture}
[level distance=10mm,
every node/.style={fill=red!60,circle,inner sep=1pt},
level 1/.style={sibling distance=20mm,nodes={fill=red!45}},
level 2/.style={sibling distance=10mm,nodes={fill=red!30}},
level 3/.style={sibling distance=5mm,nodes={fill=red!25}}]
\node  {31} [grow'=up]
child {node {30}
child {node {20}
child {node {5}}
child {node {4}}
}
child {node {10}
child {node {9}}
child {node {1}}
}
}
child {node {20}
child {node {19}
child {node {1}}
child[missing]
}
child {node {18}}
};
\end{tikzpicture}

\end{document}

enter image description here

Related Question