Trees in TikZ

Peter Smit asked on TeX.SX:

 
How to make a three level deep tree with TikZ?
 

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 right 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?

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}
path with decoration
Stefan Kottwitz

26. August 2010 by stefan
Categories: Uncategorized | Leave a comment

Leave a Reply