3

I'm drawing trees in graph theory. Some of the nodes are overlapped. Can anyone help?

 % Red-black tree
    % Author: Madit
    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{arrows}

    \tikzset{
      treenode/.style = {align=center, inner sep=0pt, text centered,
        font=\sffamily},
      arn_n/.style = {treenode, circle,black, draw=black, text width=1.5em}% arbre rouge noir, noeud noir
      %arn_r/.style = {treenode, circle, black, draw=black, 
        %text width=1.5em},% arbre rouge noir, noeud rouge
      %arn_x/.style = {treenode, circle, black, draw=black, 
       % text width=1.5em}% arbre rouge noir, nil
    }

    \begin{document}
    %1
    \begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
      level distance = 1.5cm}] 
    \node [arn_n] {4}
        child{ node [arn_n] {3} 
                child{ node [arn_n] {1} 
                    child{ node [arn_n] {5} } %for a named pointer
                                child{ node [arn_n] {}}
                }
                child{ node [arn_n] {2}
                                %child{ node [arn_n] {18}}
                                %child{ node [arn_n] {}}
                } 
                child{ node [arn_n] {2}
                                %child{ node [arn_n] {18}}
                                %child{ node [arn_n] {}}
                }                            
        }
       child{ node [arn_n] {3} 
                child{ node [arn_n] {1} 
                    %child{ node [arn_n] {5} } %for a named pointer
                                %child{ node [arn_n] {}}
                }
                child{ node [arn_n] {2}
                                %child{ node [arn_n] {18}}
                                %child{ node [arn_n] {}}
                } 
                child{ node [arn_n] {2}
                                %child{ node [arn_n] {18}}
                                %child{ node [arn_n] {}}
                }                            
        }
        child{ node [arn_n] {3} 
                child{ node [arn_n] {1} 
                    %child{ node [arn_n] {5} } %for a named pointer
                                %child{ node [arn_n] {}}
                }
                child{ node [arn_n] {2}
                                %child{ node [arn_n] {18}}
                                %child{ node [arn_n] {}}
                } 
                child{ node [arn_n] {2}
                                %child{ node [arn_n] {18}}
                                %child{ node [arn_n] {}}
                }                            
        }
    ; 
    \end{tikzpicture}


    \end{document}

enter image description here

1
  • Adjust sibling distance for level 2. Sibling distance for level 1 is 5cm, and for level two is 2.5cm, so right most child and left most child are placed at same place.
    – Ignasi
    Nov 11 '13 at 12:00
7

With forest you don't have to worry computing sibling distances, it will do it for you.

\documentclass[tikz]{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
    for tree={circle, draw, minimum width=1cm,anchor=center,fit=rectangle}
[4 [3 [1 [5] []] [2] [2]]                     
   [3 [1 [5] []] [2] [2]]                     
   [3 [1 [5] []] [2] [2]]]
\end{forest}
\end{document}

enter image description here

6

You could play with the new graph drawing library in the CVS version of PGF, although it must be compiled with luatex:

\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}
\begin{document}

\begin{tikzpicture}[>=stealth, every node/.style={circle, draw, minimum size=0.75cm}]

\graph [tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
    {
        4 -> { 
          3 -> { 1 -> { 5, " " }, 2,2 },
          3 -> { 1, 2, 2 },
          3 -> { 1, 2, 2 }
        } 
    };

\end{tikzpicture}

\end{document}

enter image description here

5
  • 1
    How might go about adding labels to the edges of the graph?
    – oleks
    Jun 18 '15 at 10:33
  • I'm compiling with LuaTeX but keep getting the error ! I can't find file tikzlibrarygraphdrawing.code.tex'.
    – Olumide
    Feb 24 '16 at 23:17
  • @Olumide you probably need to update your PGF distribution. Mar 2 '16 at 7:41
  • Is there any option to add labels to the edges, as @oleks already asked? Sep 5 '17 at 14:10
  • @oleks take a look at the tikzlibrary quotes as described here: tex.stackexchange.com/a/310233/11820 Sep 5 '17 at 14:49
2

Change the tikzpicture option into

enter image description here

[->,>=stealth',level distance = 2cm,
level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=1cm},
level 3/.style={sibling distance=1cm}] 

should get it right. You need to adjust the sibling distance. Here I use as above.

0

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.