Decision tree nodes overlapping with Tikz
I use yshift
option to change the position of the nodes, to avoid overlapping.
I give two choice
nodes some names (a
and b
) and draw a dashed line as usual.
Code:
\documentclass[tikz]{standalone}
\tikzset{
choice/.style = {shape=rectangle, draw, align=center, color=black, fill=black, font=\normalsize},
chance/.style = {shape=circle, draw, align=center, color=black, fill=black, font=\normalsize},
root/.style = {choice, font=\normalsize, color=black},
outcome/.style = {shape=rectangle, draw=white, align=center, font=\tiny, parent anchor=left},
}
\begin{document}
\begin{tikzpicture}
[
grow = right,
sibling distance = 6em,
level distance = 8em,
edge from parent/.style = {draw, -latex},
every node/.style = {font=\normalsize},
sloped
]
\node [root] {}
child {node [outcome, label=right:{\$30}] {}
edge from parent node [below] {}}
child {node [choice] {}
child {node [chance] {}
child {node [choice] (a) {}
child {node [outcome, label=right:{\$10}]{}}
child {node[yshift=-2em,outcome, label=right:{\$100}]{}}
edge from parent node [below] {.5}}
child {node [choice] (b) {}
child {node [yshift=2em,outcome, label=right:{\$45}]{}}
child {node [outcome, label=right:{\$55}]{}}
edge from parent node [above] {.5}}
edge from parent node [below] {}}
child {node [outcome, label=right:{\$25}]{}
edge from parent node [above] {}}
edge from parent node [above] {}};
\draw[dashed] (a)--(b);
\end{tikzpicture}
\end{document}
I would like to recommend forest, where the syntax is simpler and you do not have to worry about these things.
\documentclass{article}
\usepackage[edges]{forest}
\tikzset{
choice/.style = {shape=rectangle, draw, align=center, color=black, fill=black},
chance/.style = {shape=circle, draw, align=center, color=black, fill=black},
root/.style = {choice, font=\normalsize, color=black},
outcome/.style = {shape=rectangle, draw=white, align=center, font=\tiny, parent anchor=left},
}
\begin{document}
\begin{forest}
for tree={grow'=east,edge={-latex},l+=1cm}
[,root
[,choice
[\$25]
[,chance
[,choice,edge label={node[midway,above,sloped]{.5}}
[\$55]
[\$45]
]
[,choice,edge label={node[midway,below,sloped]{.5}}
[\$100]
[\$10]
]
]
]
[\$30]
]
\end{forest}
\end{document}