Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

B-Tree

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

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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.

sample code output

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}
share|improve this answer
    
Great explanation of the solution. Thanks! –  Manuel Faux May 16 '13 at 17:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.