What a decision tree shows
A decision tree shows a set of choices as branches. It starts at a root question, and each branch leads to another question or to an outcome. Each branch is labeled with the answer that takes you down it.
Use a decision tree when each answer leads to a different set of follow-up questions. For a sequence of process steps, a flowchart usually fits better. The block diagrams and flowcharts guide covers that case.
Draw a decision tree with the trees library
The trees library of TikZ draws a tree from nested child blocks. Each node is a box, and the library draws the connecting lines. This example uses a simplified statistics choice:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[
every node/.style={draw, rectangle, rounded corners, align=center},
level 1/.style={sibling distance=6cm, level distance=2cm},
level 2/.style={sibling distance=4cm, level distance=2cm}
]
\node {Is the data\\ normally distributed?}
child { node {Yes}
child { node {Paired samples:\\ paired t-test} }
child { node {Independent samples:\\ two-sample t-test} }
}
child { node {No}
child { node {Rank-based test} }
};
\end{tikzpicture}
\end{document}
The level 1 and level 2 styles set how far apart the siblings are at each depth. The \\ breaks a long label onto two lines, which works because align=center is set.
Label the branches
To put a label on a branch, add edge from parent node after the node it leads to. The label node should not get a box, so it sets draw=none. Without that option, the every node style would box the label too:
\begin{tikzpicture}[
every node/.style={draw, rectangle, rounded corners, align=center},
level 1/.style={sibling distance=6cm, level distance=2cm},
level 2/.style={sibling distance=4cm, level distance=2cm}
]
\node {Is the data\\ normally distributed?}
child { node {Yes}
edge from parent node[left, draw=none] {yes}
child { node {Paired samples:\\ paired t-test} }
child { node {Independent samples:\\ two-sample t-test} }
}
child { node {No}
edge from parent node[right, draw=none] {no}
child { node {Rank-based test} }
};
\end{tikzpicture}
node[left] places the label to the left of the branch, and node[right] places it to the right.
Draw a larger tree with forest
Writing a big tree with child blocks gets hard to read. The forest package uses bracketed nesting instead, where each bracket is one node:
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={draw, rectangle, rounded corners, align=center, edge={->}}
[Data normally distributed
[Yes
[Paired: paired t-test]
[Independent: two-sample t-test]
]
[No
[Rank-based test]
]
]
\end{forest}
\end{document}
for tree sets the style for every node, and edge={->} adds an arrow to each branch. In forest, a comma after a node’s text starts a list of options. If a label needs a comma, wrap the label in braces.
Keep wide trees readable
- Limit the depth. Three or four levels is usually enough for a readable decision tree. Split a deeper tree into two diagrams.
- Keep labels short. Put long conditions in a numbered footnote or in the text.
- Adjust the spacing. Increase
sibling distanceif boxes overlap, andlevel distanceif the rows are too close. - Change the direction. In forest,
grow=eastdraws the tree from left to right, which suits a wide page.
Common problems
- Boxes overlap. Increase the
sibling distancefor the level where they crowd together. - Line breaks do not work. Add
align=centerto the node style. Without it,\\may not break the text. - Forest reports an error on a label. Look for a comma in the label, and wrap the label in braces.
- The label box appears on a branch. Add
draw=noneto theedge from parent nodeoptions.
Frequently asked questions
Do I need the forest package?
Not for a small tree. The trees library is enough for a few levels. Forest is easier to write for large trees, because each node is one bracket.
How is a decision tree different from a flowchart?
A decision tree branches on conditions and ends at outcomes. A flowchart shows a process, with steps that can loop back. Pick the one that matches what the reader needs to follow.