Tikz-pgf – How to Draw a Horizontal Tree with Branches on Both Sides

tikz-pgftikz-treestrees

I need to draw trees but they should extend (from the root) on both sides. TikZ seems a good solution but do not how to manage it.

Best Answer

The key is the grow option (See Section 18.5.2 Default Growth Function of the manual). A little example:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[level distance=10mm,sibling distance=10mm,every node/.style={fill=blue!30,circle,inner sep=5pt}
]
\node {0}
child[grow=left] {
child {node{10}} child {node{20}} child {node{30}}
}
child[grow=right] {
child {node{40}} child {node{50}} child {node{60}}
};
\end{tikzpicture}

\end{document}

enter image description here

Arrows can be added to some edge(s) using the edge from parent. If arrows need to be added to many edges, the best thing to do is to define a style (as Alan Munn suggested in a comment):

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[level distance=10mm,
  sibling distance=10mm,
  every node/.style={fill=blue!30,circle,inner sep=5pt},
  arrow/.style={edge from parent/.style={draw,-latex}}
]

\node {0}
child[grow=left] {
child {node{10}} child {node{20}} child[arrow] {node{30}}
}
child[grow=right] {
child {node{40}} child {node{50}} child[arrow] {node{60}}
};
\end{tikzpicture}

\end{document}

enter image description here

Related Question