# centering a tree using Tikz

\documentclass{article}
\usepackage{tikz}

\begin{document}
\usetikzlibrary{positioning,trees}
\begin{tikzpicture}[level/.style={sibling distance=85mm/#1}]
\node {T(n)}
child {node {$T\left(\left(\frac{1}{3}\right)^{1}\cdot\left(\frac{2}{3}\right)^{0}\cdot n\right)$}
child {node {$T\left(\left(\frac{1}{3}\right)^{2}\cdot\left(\frac{2}{3}\right)^{0}\cdot n\right)$}
child {node {$T\left(\left(\frac{1}{3}\right)^{2}\cdot\left(\frac{2}{3}\right)^{0}\cdot n\right)$}}
child {node {$T\left(\left(\frac{1}{3}\right)^{1}\cdot\left(\frac{2}{3}\right)^{1}\cdot n\right)$}}}
child {node {$T\left(\left(\frac{1}{3}\right)^{1}\cdot\left(\frac{2}{3}\right)^{1}\cdot n\right)$}}
}
child {node {$T\left(\left(\frac{1}{3}\right)^{0}\cdot\left(\frac{2}{3}\right)^{1}\cdot n\right)$}
child {node {$T\left(\left(\frac{1}{3}\right)^{2}\cdot\left(\frac{2}{3}\right)^{0}\cdot n\right)$}}
child {node {$T\left(\left(\frac{1}{3}\right)^{1}\cdot\left(\frac{2}{3}\right)^{1}\cdot n\right)$}}
};

\end{tikzpicture}
\end{document}


I'm trying to draw simple recursive tree using tree but the nodes go outside the page bounds. I already tried \begin{center}/\centering

You can set the scale and transform shape option to resize the whole content of the tikzpicture environment:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,trees}

\begin{document}

\begin{tikzpicture}[level/.style={sibling distance=85mm/#1}, scale=0.6, transform shape]
\node {T(n)}
child {node {$T\left(\left(\frac{1}{3}\right)^{1}\cdot\left(\frac{2}{3}\right)^{0}\cdot n\right)$}
child {node {$T\left(\left(\frac{1}{3}\right)^{2}\cdot\left(\frac{2}{3}\right)^{0}\cdot n\right)$}
child {node {$T\left(\left(\frac{1}{3}\right)^{2}\cdot\left(\frac{2}{3}\right)^{0}\cdot n\right)$}}
child {node {$T\left(\left(\frac{1}{3}\right)^{1}\cdot\left(\frac{2}{3}\right)^{1}\cdot n\right)$}}}
child {node {$T\left(\left(\frac{1}{3}\right)^{1}\cdot\left(\frac{2}{3}\right)^{1}\cdot n\right)$}}
}
child {node {$T\left(\left(\frac{1}{3}\right)^{0}\cdot\left(\frac{2}{3}\right)^{1}\cdot n\right)$}
child {node {$T\left(\left(\frac{1}{3}\right)^{2}\cdot\left(\frac{2}{3}\right)^{0}\cdot n\right)$}}
child {node {$T\left(\left(\frac{1}{3}\right)^{1}\cdot\left(\frac{2}{3}\right)^{1}\cdot n\right)$}}
};
\end{tikzpicture}

\end{document}


I'd use forest. Not only because it is more space efficient but also because you can create the tree programmatically. (Full credits go to Sašo Zivanović and cfr for providing us with the package and many examples of this kind.)

\documentclass{article}
\usepackage{geometry}
\usepackage[edges]{forest}
\begin{document}

\begin{forest}
for tree={edge={semithick,draw},
l sep+=1ex,s sep+=-1ex,
if level=0{}{delay={%
content/.wrap 2 pgfmath
args={$\pgfmathtruncatemacro{\myn}{min(2,#1)+1-#2} \pgfmathtruncatemacro{\mym}{#2-1} T\left(\left(\frac{1}{3}\right)^{\myn}\left(\frac{2}{3}\right)^{\mym}n\right)$}{level}{n}}}}
[$T(n)$
[
[
[]
[]
]
[
]
]
[
[
]
[
]
]
]
\end{forest}
\end{document}


As you can see, you do not have to type the formulae. And if you have another recursion, you can just change the formula.

• This is amazing :) +1 – user1146332 Apr 13 '20 at 21:35
• @user1146332 forest is. I just use it, and have learned a lot from cfr's examples on this site. Most likely she will be able to make this much more elegant. (I upvoted your answer, too, of course as it answers the question.) – user194703 Apr 13 '20 at 22:22
• Thank your for your reply. But you don't need to be humble. The pace, quality and consistency of your answers is just mind blowing. Very inspiring! – user1146332 Apr 13 '20 at 22:50