Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

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

share|improve this answer
    
Sorry one more thing, how can I edit the edges and apply a style for the arrows? –  Andry Apr 2 '12 at 1: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 2 '12 at 1: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 2 '12 at 1: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 2 '12 at 1: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 2 '12 at 1:52
show 2 more comments

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

share|improve this answer
    
Interesting solution however... :) –  Andry Apr 2 '12 at 1: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 2 '12 at 1:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.