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'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

share|improve this question
    
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

3 Answers 3

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

share|improve this answer

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

share|improve this answer

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.

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.