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.

How do I draw just a tree with one child using tikzpicture?

As shown in the picture, I want to have a tree like (1).

3 trees

But I have only been succesful in getting a picture like (2) and (3) using:

\begin{tikzpicture}
\node[circle,draw](z){$30$}
% comment the below for (3):
child{}
child{
node[circle,draw]{40}}
;
\end{tikzpicture}

So, is there a modifier that I don't know of? I've tried to find it in the manual of 7xx pages, but I failed on finding something useful.

share|improve this question
add comment

2 Answers

up vote 19 down vote accepted

You can use missing children (Section 18.5.3 Missing Children in the pgf manual):

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\node[circle,draw](z){$30$}
  child[missing]{}
  child{
    node[circle,draw]{40} child{node[circle,draw] {20}} child[missing] };
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
You're awesome, thanks! –  Lekensteyn Mar 9 '12 at 22:49
add comment

You can also do this with tikz-qtree, which has a simpler syntax, especially for large trees and if most of your nodes are balanced, and only a few aren't.

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\tikzset{every tree node/.style={minimum width=2em,draw,circle},
         blank/.style={draw=none},
         edge from parent/.style=
         {draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
         level distance=1.5cm}
\begin{tikzpicture}
\Tree
[.50     
    [.60 ]
    [.30  
    \edge[blank]; \node[blank]{};
    \edge[]; [.40
             \edge[]; {20}
             \edge[blank]; \node[blank]{};
         ]
    ]
]
\end{tikzpicture}
\end{document}

output of code

share|improve this answer
    
I haven't installed tikz-qtree, but this is a nice, clear example +1 –  Lekensteyn Mar 10 '12 at 10:19
    
Is there any way to simplify the \edge[blank]; \node[blank]{}; phrase? I seem to be using it a lot! –  Richard Dec 18 '12 at 22:59
1  
@Richard This isn't so easy. See Macro for empty nodes in tikz-qtree. –  Alan Munn Dec 18 '12 at 23:52
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.