30

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.

Lekensteyn
  • 532
  • 1
  • 4
  • 10

3 Answers3

38

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

Gonzalo Medina
  • 505,128
  • 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 '16 at 15:33
  • 1
    @joshreesjones Also stumbled upon this problem. You probably already solved this by now, but for anyone also having trouble with the layout: You might want to check this answer: http://tex.stackexchange.com/a/2345/68846 – Quxflux Jul 31 '16 at 13:51
  • I'm five years too late, but for anyone else, the simplest answer is \begin{tikzpicture}[tree layout]. It's a better solution than the one in the link above, as it does not require manual distance specifications. – Syzygy Apr 08 '21 at 16:16
17

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

Alan Munn
  • 218,180
4

There is also Forest, of course. This permits defining a couple of simple styles enabling a more concise tree specification.

\begin{forest}
  gappy tree
  [
    [, c phantom]
    [
      []
      [, c phantom]
    ]
  ]
\end{forest}

gappy tree

Complete code:

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{%
  gappy tree/.style={
    for tree={
      circle,
      draw,
      s sep'+=10pt,
      fit=band,
    },
  },
  c phantom/.style={draw=none, no edge},
}
\begin{document}
\begin{forest}
  gappy tree
  [
    [, c phantom]
    [
      []
      [, c phantom]
    ]
  ]
\end{forest}
\end{document}
cfr
  • 198,882