19

I am busy making a full binary tree of three levels deep.

The code I am having now is (using the trees library):

\begin{tikzpicture}
  \node {root}
    child {node {left}
      child {node {lleft}}
      child {node {rleft}}
    }
    child {node {right}
    child {node {lright}}
      child {node {rright}}
    };

\end{tikzpicture}

The problem is that rleft and rright are printed over each other.

Preferably I would like tikz to figure this out by itself, for example if I give a minimum distance between nodes on the same level. Is this possible? (My final nodes will not have text but will be fixed size shapes)

Off course it can be that the solution is: "Don't use 'trees'". In that case, what is the best way to do this?

18

tikz-qtree is an awesome package for more convenient tree drawing in tikz.

22

You could specify options for each level, for instance silbling distance but also level distance.

Example:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
  level 1/.style={sibling distance=3cm},
  level 2/.style={sibling distance=1.5cm}]
  \node {root}
    child {node {left}
      child {node {lleft}}
      child {node {rleft}}
    }
    child {node {right}
    child {node {lright}}
      child {node {rright}}
    };
\end{tikzpicture}
\end{document}

Output:

alt text

3

I suspect that, for this case, the qtree package mentioned by maxschlepzig is the right choice. However, for more complicated structures you might find it worthwhile to compute the tree using graphviz and then using dot2tex to convert it to TikZ for the actual rendering.

3

I suppose forest deserves to be mentioned as well.

Code

\documentclass[border=2pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={l+=1cm} % increase level distance
  [root
    [left[lleft][lright]]
    [right[rleft][rright]]
  ]
\end{forest}
\end{document}

Output

enter image description here

2

A recommended solution with PSTricks just for fun.

\documentclass[preview,border=12pt]{standalone}
\usepackage{pst-tree}
\psset{treemode=D,nodesep=3pt}
\begin{document}
\psTree{\Tr{root}}
    \psTree{\Tr{left}}
        \Tr{lleft}
        \Tr{rleft}
    \endpsTree
    \psTree{\Tr{right}}
        \Tr{lright}
        \Tr{rright}
    \endpsTree
\endpsTree
\end{document}

enter image description here

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.