绘制 TikZ 图片时出现重叠

绘制 TikZ 图片时出现重叠

我正在用图论画树。有些节点重叠了。有人能帮忙吗?

 % 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

forest您不必担心计算兄弟距离,它会为您完成。

\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

答案2

您可以使用 PGF 的 CVS 版本中的新图形绘制库,尽管它必须编译为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

答案3

将 tikzpicture 选项更改为

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}] 

应该可以。您需要调整兄弟距离。这里我使用上面的方法。

相关内容