# How to control spacing in a TikZ tree?

I want to draw a tree and I don't know how to enforce the spacing between them. Some are overlapping.

This is my code:

\begin{tikzpicture}
\tikzstyle{every node}=[circle,draw]
\node {A}
child { node {B}
child { node {E} }
child { node {F} child { node {K} } child { node {L} } child { node {M}}}
child { node {G} }}
child { node {C}
child {node {H}}}
child { node {D} child {node {I}} child {node {J} child {node {N}} child {node {O}}}};
\end{tikzpicture}


The nodes G and H are overlapping.

I've tried using the parameter node distance but it doesn't work.

-

Refer to Section 18.4 Specifying Options for Trees and Children of the pgfmanual. Using sibling distance, you can solve the problem (use the values that best suit your needs):

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[every node/.style={circle,draw},level 1/.style={sibling distance=30mm},level 2/.style={sibling distance=10mm}
]
\node {A}
child { node {B}
child { node {E} }
child { node {F} child { node {K} } child { node {L} } child { node {M}}}
child { node {G} }}
child { node {C}
child {node {H}}}
child { node {D} child {node {I}} child {node {J} child {node {N}} child {node {O}}}}
;
\end{tikzpicture}

\end{document}


-
Thank you very much! – andrasi May 29 '11 at 18:19

If you don't want to manually adjust the spacing for each tree, you could use the tikz-qtree package. This is aimed at linguistics but can be used in other cases. (The spacing commands below are setting the minimum distances, not the actual distances. The package automatically guarantees that nodes won't overlap with a pretty straightforward layout algorithm that you can probably infer from this example.)

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}[every tree node/.style={draw,circle},sibling
distance=10pt, level distance=40pt]
\tikzset{edge from parent/.style={draw, edge from parent path=
{(\tikzparentnode) -- (\tikzchildnode)}}}
\Tree [.A [.B E [.F K L M ] G ] [.C H ] [.D I [.J N O ] ] ]
\end{tikzpicture}

\end{document}


Version with lines going from south to north anchors:

Edit: I'm finally at the right computer to update this example to show how to deal with @Alan's question. I left the old image for comparison above (to get this, remove the \tikzset command from the above code), but here's the one with lines emerging from the center of the nodes:

-
One thing that bothers some people with this tree is that the branches extend from the bottom of the circles rather than the implicit centre. (As in the tree in the other answer.) I couldn't figure out a way to change that with tikz-qtree. – Alan Munn May 30 '11 at 21:24
@Alan, you can reset to the default by doing the following: \tikzset{edge from parent/.style={draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}}} – kgr May 30 '11 at 22:08
Thanks. I'll add this to a previous answer of mine that produced a similar tree. – Alan Munn May 30 '11 at 22:24

In case you want to only move a single node you can also use:

child{node [left=1.5cm] {b}


To move the node 1.5cm to the left.

-

Here's a simple forest solution:

\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\begin{document}

\begin{forest}
for tree={
circle,
draw
}
[A
[B
[E]
[F
[K]
[L]
[M]
]
[G]
]
[C
[H]
]
[D
[I]
[J
[N]
[O]
]
]
]
\end{forest}

\end{document}


If you prefer the C node to be aligned with the root, you can tell forest to do this:

    [C, calign with current


-