I'm trying to create a Horizontal Probability Tree with Level Labels using tikz. (Not tikz-qtree). Right now, I have a horizontal tree. The labels are NOT aligned to the top of the diagram. I would like to have all the labels aligned without having to manually tweak positions.
Here is the MWE:
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=4.5cm, sibling distance=1.5cm]
\tikzstyle{level 3}=[level distance=3.5cm, sibling distance=2cm]
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [rectangle, draw=none, minimum width=3pt, inner sep=0pt]
\tikzstyle{ans} = [color=red]
\begin{tikzpicture}[level distance=5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1.2cm},
level 3/.style={level distance = 2cm},grow'=right]
\tikzstyle{every node}=[]
\node (Root) [] {Origin}
child [] {
node {Door A}
child { node {A}
child {node[end] {$0$} }
edge from parent
node[left] {$0$}
}
child [black] { node {B}
child {node[ans,end] {$\frac{1}{6}$} }
edge from parent
node[ans,left] {$\frac{1}{2}$}
}
child [black] { node {C}
child {node[ans,end] {$\frac{1}{6}$} }
edge from parent
node[ans,left] {$\frac{1}{2}$}
}
edge from parent
node[above] {$\frac{1}{3}$}
}
child {
node {Door B}
child { node {A}
child {node[ans,end] {$0$} }
edge from parent
node[ans,left] {$0$}
}
child { node {B}
child {node[end] {$0$} }
edge from parent
node[ans,left] {$0$}
}
child { node {C}
child {node[ans,end] {$\frac{1}{3}$} }
edge from parent
node[ans,left] {$1$}
}
edge from parent
node[ans,left] {$\frac{1}{3}$}
}
child {
node {Door C}
child { node {A}
child {node[ans,end] {$0$} }
edge from parent
node[ans,left] {$0$}
}
child { node {B}
child {node[ans,end] {$\frac{1}{3}$} }
edge from parent
node[ans,left] {$1$}
}
child { node {C}
child {node[end] {$0$} }
edge from parent
node[ans,left] {$0$}
}
edge from parent
node[ans,left] {$\frac{1}{3}$}
};
% How I'm applying labels to each level.
% Need to be able to dynamically align nodes at top level
\begin{scope}[every node/.style={above}]
\path (Root -| Root) ++(0,15mm) node {Test};
\path (Root-1 -| Root-1) ++(0,15mm) node {Contestant Chooses};
\path (Root-1-1 -| Root-3-3-1) ++(10mm,13mm) node {Host Reveals Door};
\path (Root-1-1-1 -| Root-3-3) ++(0,15mm) node {Probability};
\end{scope}
\end{tikzpicture}
I get this
and want this
Best Answer
You can use
intersections
coordinate system to align all level labels. First place the label for the top most leaf where you want:and later, use it as an horizontal reference for all other labels
I've used
positioning
library to fix first label position. The complete code isand the result (change labels aspect as you want)