9

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.

Werner
  • 603,163
Andry
  • 1,287

2 Answers2

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}

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

Gonzalo Medina
  • 505,128
  • Sorry one more thing, how can I edit the edges and apply a style for the arrows? – Andry Apr 02 '12 at 01:28
  • @Andry: TikZ offers 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:30
  • Well I just want edges to have a arrow, in your perfect example from 0 to 60 I want an arrow in 60's direction... However thankyou for your kindness. – Andry Apr 02 '12 at 01:42
  • 1
    @Andry: you can use edge from parent to 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:48
  • @GonzaloMedina This would be better made into its own style attached to the main tikzpicture: arrow/.style={edge from parent/.style={draw,-latex}} and then for each node that requires an arrow, just use child[arrow]. – Alan Munn Apr 02 '12 at 01:52
  • @Gonzalo: U r my savior. Thankyou very much for your kindness, professional attitude and patience. :) – Andry Apr 02 '12 at 01:52
  • @Andry: you're welcome! Alan Munns's comment regarding defining a style for the arrows will also be useful for you. – Gonzalo Medina Apr 02 '12 at 01:58
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}

output of code

Alan Munn
  • 218,180
  • Interesting solution however... :) – Andry Apr 02 '12 at 01:52
  • As I said, when your trees get complicated, the tikz-qtree method 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