# Vertical path to node in TikZ trees

How can I set the path between two specific nodes to "vertical" rather than "direct"?

Currently my tree looks like this:

\begin{tikzpicture}[level distance=1.5cm,
every node/.style={draw=black,very thick,minimum width=10mm,inner sep=2mm},
every path/.style={thick},
level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2cm},
level 3/.style={sibling distance=0.5cm,level distance=10mm},
leaf/.style={fill=black,minimum width=2mm,minimum height=2mm,inner sep=0mm}]
\node{10}
child{node{8}
child{node{5}
child{node[leaf]{}}
child{node[leaf]{}}
}
child{node{9}
child{node[leaf]{}}
child{node[leaf]{}}
}
}
child{node{20}
child{node{12}
child{node[leaf]{}}
child{node[leaf]{}}
}
child{node{21 29}
child[edge from parent]{node[leaf]{}}
child{node[leaf]{}}
child{node[leaf]{}}
}
};
\end{tikzpicture}


I would like to have the paths between the last row and the leafs to be vertical, rather than diagonal.

-

You can change the way edges are drawn with the edge from parent path key. Change the level 3/.style line to:

level 3/.style={
sibling distance=0.5cm,
level distance=10mm,
edge from parent path={
(\tikzchildnode.north) -- (\tikzchildnode.north |- \tikzparentnode.south)
}
},


If A and B are coordinates, then A |- B is the intersection of a vertical line through A and a horizontal line through B. So (A) -- (A |- B) draws a line to this intersection.

If you're going to do this a lot, make your own style:

\tikzset{edge from parent vertical/.style={
edge from parent path={
(\tikzchildnode.north) -- (\tikzchildnode.north |- \tikzparentnode.south)
}
}}


then you can change the line to:

level 3/.style={sibling distance=0.5cm,level distance=10mm,edge from parent vertical}

-
Great explanation of the solution. Thanks! – Manuel Faux May 16 '13 at 17:40
The |- syntax is a solution to many problems. Thank for pointing at it. – Alfred M. Mar 31 at 12:55