TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 21 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
    
When I have a binary tree where the first left node has a right child and the first right node has a left child, they are rendered in the same spot. Is there an easy way to fix this? – joshreesjones Feb 20 at 15:33

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
    
Is there a way to increase the horizontal spacing between nodes to make the tree look wider (I'm new to LaTeX)? – joshreesjones Feb 26 at 21:07

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.