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.
Asked
Active
Viewed 1.4k times
2 Answers
11
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}

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}

Gonzalo Medina
- 505,128
9
Depending on the complexity of your tree it might be simpler to put two trees together. Here's an example using tikz-qtree to draw the trees.
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees} % this is to allow the fork right path
\begin{document}
\begin{tikzpicture}[level distance=1.25in,sibling distance=.25in,scale=.75]
\tikzset{edge from parent/.style=
{thick, draw,
edge from parent fork right},every tree node/.style={draw,minimum width=1in,text width=1in, align=center},grow'=right}
\Tree
[. parent
[.{nice child0}
[.{grandchild0-0 } ]
[.{grandchild0-1 } ]
[.{grandchild0-2 } ]
[.{grandchild0-3 with a really long name } ]
]
[.child1
[.{grandchild1-0 } ]
[.{grandchild1-1 } ]
[.{grandchild1-2 } ]
]
[.child2 ]
[.child3 ]
]
\begin{scope}
\tikzset{edge from parent/.style=
{thick, draw,
edge from parent fork left},every tree node/.style={draw,minimum width=1in,text width=1in, align=center},grow'=left}
\Tree
[.\node[draw=none]{};
[.{nice child0}
[.{grandchild0-0 } ]
[.{grandchild0-1 } ]
[.{grandchild0-2 } ]
[.{grandchild0-3 with a really long name } ]
]
[.child1
[.{grandchild1-0 } ]
[.{grandchild1-1 } ]
[.{grandchild1-2 } ]
]
[.child2 ]
[.child3 ]
]
\end{scope}
\end{tikzpicture}
\end{document}

Alan Munn
- 218,180
-
-
As I said, when your trees get complicated, the
tikz-qtreemethod of input becomes helpful. It wasn't clear from your question exactly what kind of tree you wanted. – Alan Munn Apr 02 '12 at 01:54
TikZoffers so many possibilities for what you want that you will have to narrow a bit the scope of your question. How do you want the edges to look like and what kind of style do you want for the arrows? – Gonzalo Medina Apr 02 '12 at 01:30edge from parentto change the attributes for some of the edge(s): try this:\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} edge from parent[-latex]} }; \end{tikzpicture}– Gonzalo Medina Apr 02 '12 at 01:48tikzpicture:arrow/.style={edge from parent/.style={draw,-latex}}and then for each node that requires an arrow, just usechild[arrow]. – Alan Munn Apr 02 '12 at 01:52