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.

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?

share|improve this question

5 Answers 5

up vote 15 down vote accepted

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

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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

share|improve this answer

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.