5

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.

| improve this question | | | | |
5

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}
| improve this answer | | | | |
  • The |- syntax is a solution to many problems. Thank for pointing at it. – M. Toya Mar 31 '16 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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