Search code examples
latexpdflatextikz

Drawing an AVL Tree with tikzpicture in Latex


I hope someone can help me. I am trying to draw an AVL Tree with Tikzpicture. I must say I am not very familiar in how to modify Tikz. In row 3 both the childs lay over each other. How can I avoid this, so that they are next to each other not on each other? I have attached the code I was using for this drawing. Many thanks in advance.

Picture

\documentclass[11pt, a4paper]{book} % add parameters to the document
\usepackage{fullpage}
\usepackage[utf8]{inputenc}
\usepackage{tikz}  % Graphen zeichnen

\begin{document}
\begin{tikzpicture}[
    edge from parent path=
    {(\tikzparentnode.south) .. controls +(0,0) and +(0,0)
                             .. (\tikzchildnode.north)},
   every node/.style={draw,circle},
   label distance=-1mm
   ]
\node [label=330:$0$]{7}
  child {node[label=330:$0$] {2}
    child {node[label=330:$0$] {1}}
    child {node[label=330:$0$] {3}}}
  child {node[label=330:$0$] {24}
    child {node[label=330:$0$] {15}}
    child {node[label=330:$0$] {42}}
    };
\end{tikzpicture}
\end{document}

Solution

  • One possible solution could be to change the sibling distance like this:

    \documentclass[11pt, a4paper]{book} % add parameters to the document
    \usepackage{fullpage}
    \usepackage[utf8]{inputenc}
    \usepackage{tikz}  % Graphen zeichnen
    
    \begin{document}
    \begin{tikzpicture}[
        edge from parent path=
        {(\tikzparentnode.south) .. controls +(0,0) and +(0,0)
                                 .. (\tikzchildnode.north)},
       every node/.style={draw,circle},
       label distance=-1mm,
       level 1/.style={sibling distance=30mm},   
       level 2/.style={sibling distance=15mm}
       ]
    \node [label=330:$0$]{7}
      child {node[label=330:$0$] {2}
        child {node[label=330:$0$] {1}}
        child {node[label=330:$0$] {3}}}
      child {node[label=330:$0$] {24}
        child {node[label=330:$0$] {15}}
        child {node[label=330:$0$] {42}}
        };
    \end{tikzpicture}
    \end{document}
    

    enter image description here