How do I make an unbalanced binary tree?
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}
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}
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}
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}