二进制节点在第 5 级重叠

二进制节点在第 5 级重叠

如何防止二叉树中的节点重叠?

\documentclass[border=5pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 1em, draw, circle},
  level/.style = {sibling distance = 80mm/#1}
  ]
  \node {7}
  child {node {6} 
        child {node {5}
                   child {node {4}
                       child {node {3}
                               child {node {2}}
                                child {node {1}}
                               }
                       child {node {2}}
                      }
               child {node {3}
                       child {node {2}}
                       child {node {1}}
                      }
        }
        child {node {4}
               child {node {3}
                       child {node {2}}
                       child {node {1}}
                       }
               child {node {2}}
              }
        }
  child {node {5}
         child {node {4}
                child {node {3}
                        child {node {2}}
                        child {node {1}}
                        }
                child {node {2}}
               }
         child {node {3}
                child {node {2}}
                child {node {1}}
                }
        };
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

您可以手动管理每个级别的兄弟距离:

二叉树

\documentclass[border=5pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 1em, draw, circle},
  level/.style = {sibling distance = 80mm/#1},
  level 3/.style = {sibling distance = 20mm},
  level 4/.style = {sibling distance = 10mm}
  ]
  \node {7}
  child {node {6} 
        child {node {5}
                   child {node {4}
                       child {node {3}
                               child {node {2}}
                                child {node {1}}
                               }
                       child {node {2}}
                      }
               child {node {3}
                       child {node {2}}
                       child {node {1}}
                      }
        }
        child {node {4}
               child {node {3}
                       child {node {2}}
                       child {node {1}}
                       }
               child {node {2}}
              }
        }
  child {node {5}
         child {node {4}
                child {node {3}
                        child {node {2}}
                        child {node {1}}
                        }
                child {node {2}}
               }
         child {node {3}
                child {node {2}}
                child {node {1}}
                }
        };
\end{tikzpicture}
\end{document}

答案2

forest计算节点之间的所有距离:

\documentclass{article}
\usepackage{forest}
    
\begin{document}
\begin{forest}
  [7, for tree={circle, draw, minimum size=2em}
    [6  [5  [4  [3  [2] [1]]
                        [2]]
            [3  [2] [1]]]
        [4  [3  [2] [1]]
            [2]]]
    [5  [4  [3  [2]
                [1]]
            [2]]
        [3  [2]
            [1]]]]
\end{forest}
\end{document}

在此处输入图片描述

相关内容