Tikz 中的树具有不需要的交叉节点

Tikz 中的树具有不需要的交叉节点

我正在尝试使用 Tikz 制作一棵简单的树,但是树右侧的正确位置(级别)有些问题。如何解决?

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[%
    sibling distance=10em,
    every node/.style = {%
        shape=rectangle,
        rounded corners,
        draw,
        align=center,
        top color=white,
        bottom color=blue!20
   }]]

\node {root}
    child { node {A (level 1)}
        child { node {A.A (level 2)}
            child { node {A.A.A (level 3)} }
            child { node {A.A.B (level 3)} }
        }
        child { node {A.B (level 2)} }
        child { node {A.C (level 2)} }
    }
    child { node {B (level 1)}
        child { node {B.A (level 2)} }
        child { node {B.B (level 2)} }
        child { node {B.C (level 2)} }
    %} % comment, as it wouldn't typeset otherwise
    };

\end{tikzpicture}
\end{document}

目前的情况是:错误

答案1

像这样;

在此处输入图片描述 为此,您需要在第 1 级增加兄弟距离,并在第 2 级稍微减少:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[%
level 1/.style = {sibling distance=24em},   % <-- added
level 2/.style = {sibling distance=8em},    % <-- added
level 3/.style = {sibling distance=9em},    % <-- added
    every node/.style = {%
        shape=rectangle,
        rounded corners,
        draw,
        align=center,
        top color=white,
        bottom color=blue!20
   }]]

\node {root}
    child { node {A (level 1)}
        child { node {A.A (level 2)}
            child { node {A.A.A (level 3)} }
            child { node {A.A.B (level 3)} }
        }
        child { node {A.B (level 2)} }
        child { node {A.C (level 2)} }
    }
    child { node[red] {B (level 1)}
        child { node {B.A (level 2)} }
        child { node {B.B (level 2)} }
        child { node {B.C (level 2)} }
    };
    \end{tikzpicture}
\end{document}

相关内容