# How to style a tree using tikz?

I want to style a tree into this form :

but my code is in generate this:

\documentclass{article}

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

\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}
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}


How can I make the line begins from the center of the circle instead of the edge?

-
Sorry, but have you seen the example on the pgfmanual (version October 25, 2010) in page 218? I guess you derived the wondered image there, so please have look to the code: the style of the nodes (circular and red colored) is defined in the options of the tikzpicture environment. –  Claudio Fiandrino Nov 17 '12 at 8:45
@ClaudioFiandrino, I copied the code from \begin{tikzpicture} to \end{tikzpicture} and paste it into my tex file, but the line still begins from the edge, the result is different from the book, just like my picture, this is why I ask this problem here. –  Yishu Fang Nov 17 '12 at 8:54
When I copy it into a document I get exactly that result: please, could you provide the code you used? I mean the .tex file starting with \documentclass and ending with \end{document}. –  Claudio Fiandrino Nov 17 '12 at 9:01
@ClaudioFiandrino, code added. –  Yishu Fang Nov 17 '12 at 9:14
It provides to me exactly the result shown in the pgfmanual. –  Claudio Fiandrino Nov 17 '12 at 9:19

As Alan Munn mentions in his comment, the tikz-qtree package sets the anchor point of branch path to the bottom of the node, which is the most appropriate for linguistic trees; the following example shows this (the first tree) and also contains a modified version (the second tree) recovering the desired result by adding

edge from parent path={(\tikzparentnode) to (\tikzchildnode)}


The code:

\documentclass{article}

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

\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}
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}

\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}},
edge from parent path={(\tikzparentnode) to (\tikzchildnode)}
]
\node {31}
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}


The result:

-
tikz-qtree` sets the anchor point of branch path to the bottom of the node, which is the most appropriate for linguistic trees. –  Alan Munn Nov 17 '12 at 13:49
@AlanMunn Thank you. I've added your comment to my answer. –  Gonzalo Medina Nov 17 '12 at 14:10