使用 Tikz 的决策树 - 不填充并向下生长而不是向西生长

使用 Tikz 的决策树 - 不填充并向下生长而不是向西生长

我正在尝试构建这棵决策树,我取出了填充选项

\tikzstyle{choice} = [circle, minimum width=8pt, fill, inner sep=0pt]

也让树长下来

\tikzstyle{level 1}=[level distance=1.5cm, sibling distance=2.5cm]
\tikzstyle{level 2}=[level distance=1cm, sibling distance=2cm]
\tikzstyle{level 3}=[level distance=1cm, sibling distance=1.5cm]
\tikzstyle{bag} = [circle, text width=4em, text centered, draw]
\tikzstyle{decision} = [rectangle, minimum height=8pt, minimum width=8pt, fill, inner sep=0pt]
\tikzstyle{choice} = [circle, minimum width=8pt, fill, inner sep=0pt]
\tikzstyle{end} = [circle, text width=1em, text centered, draw, inner sep=0pt]

\begin{tikzpicture}[grow=right, child anchor=west]
\tiny
\node[bag]{XOR}
child {
    node[decision]{TEST}
    child {
        node[bag]{A= 0}
            child{
                node[choice]{}
                child {
                    node[bag]{B=0}
                    child {
                        node[end]{0}
                    }
                }
                child {
                    node[bag]{B=1}
                    child {
                        node[end]{1}
                    }
                }
            }
    }
    child {
        node[bag]{A=1}
            child{
                node[choice]{}
                child {
                    node[bag]{B=0}
                    child {
                        node[end]{1}
                    }
                }
                child {
                    node[bag]{B=1}
                    child {
                        node[end]{0}
                    }
                }
            }
        }
    };

\end{tikzpicture}

答案1

如果你不给出任何选择,树自然会向下生长。我已经改变了

\tikzstyle{decision} = [rectangle, minimum height=8pt, minimum width=8pt, fill, inner sep=0pt]

\tikzstyle{choice} = [circle, minimum width=8pt, draw, inner sep=0pt]

删除fill。你还必须使用适当的距离

\tikzstyle{level 2}=[level distance=1cm, sibling distance=5cm]

以使事物不重叠。

你的代码变成

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzstyle{level 1}=[level distance=1.5cm, sibling distance=2.5cm]
\tikzstyle{level 2}=[level distance=1cm, sibling distance=5cm]
\tikzstyle{level 3}=[level distance=1cm, sibling distance=2cm]
\tikzstyle{bag} = [circle, text width=4em, text centered, draw]
\tikzstyle{decision} = [rectangle, minimum height=8pt, minimum width=8pt, fill, inner sep=0pt]
\tikzstyle{choice} = [circle, minimum width=8pt, draw, inner sep=0pt]
\tikzstyle{end} = [circle, text width=1em, text centered, draw, inner sep=0pt]

\begin{tikzpicture}%%[grow=left, child anchor=east]
\tiny
\node[bag]{XOR}
child {
    node[decision]{TEST}
    child {
        node[bag]{A= 0}
            child{
                node[choice]{}
                child {
                    node[bag]{B=0}
                    child {
                        node[end]{0}
                    }
                }
                child {
                    node[bag]{B=1}
                    child {
                        node[end]{1}
                    }
                }
            }
    }
    child {
        node[bag]{A=1}
            child{
                node[choice]{}
                child {
                    node[bag]{B=0}
                    child {
                        node[end]{1}
                    }
                }
                child {
                    node[bag]{B=1}
                    child {
                        node[end]{0}
                    }
                }
            }
        }
    };

\end{tikzpicture}
\end{document}

在此处输入图片描述

希望有帮助!

相关内容