如何使 TikZ 水平树中的叶节点的边水平?

如何使 TikZ 水平树中的叶节点的边水平?

我正在用 tikz-qtree 绘制一棵水平树。叶子节点的边缘是倾斜的,而我希望它们是水平的。我猜问题是由这些叶子节点之前的空白节点引起的。然而,当树向下生长时,边缘将完全垂直。

结果: 在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz,tikz-qtree}
\usetikzlibrary{shapes,trees}

\begin{document}
\begin{tikzpicture}[
            grow'=right,
            level distance = 2cm,
            level 2/.style={level distance=2cm,sibling distance=1cm},
            frontier/.style={distance from root=10cm}
            ]
            \Tree 
            [.\node[draw,rectangle,minimum size=0.8cm]{1};
                \edge node[above,sloped]{\small Build A};
                [.\node[draw,circle]{2};
                    \edge node[above,sloped]{\small Good(0.6)};
                    [.\node[draw,circle]{4};
                        [ \edge node[above]{\small Good(0.9)}; \node[rectangle]{\Large $\triangle$};]
                        [ \edge node[above]{\small Bad(0.1)}; \node[rectangle,inner sep=0pt]{\Large $\triangle$};] 
                    ]
                    \edge node[above,sloped]{\small Bad(0.4)};
                    [.\node[draw,circle]{5}; \edge node[above]{\small Bad(1)}; \node{\Large $\triangle$};]
                ]
                \edge node[above,sloped]{\small Build B};
                [.\node[draw,circle]{3};
                    \edge node[above,sloped]{\small Good(0.6)};
                    [.\node[rectangle,draw,minimum size=0.8cm]{6};
                        \edge node[above,sloped]{\small Expand};
                        [.\node[draw,circle]{8};
                            [ \edge node[above]{\small Good(0.9)}; \node[rectangle]{\Large $\triangle$};]
                            [ \edge node[above]{\small Bad(0.1)}; \node[rectangle]{\Large $\triangle$};] 
                        ]
                        \edge node[above,sloped]{\small No Expand};
                        [.\node[draw,circle]{9};
                            [ \edge node[above]{\small Good(0.9)}; \node[rectangle]{\Large $\triangle$};]
                            [ \edge node[above]{\small Bad(0.1)}; \node[rectangle]{\Large $\triangle$};] 
                        ]
                    ]
                    \edge node[above,sloped]{\small Bad(0.4)};
                    [.\node[draw,circle]{7}; \edge node[above]{\small Bad(1)}; \node{\Large $\triangle$};]
                ]
            ]
        \end{tikzpicture}
\end{document}

答案1

正如您所猜测的,您正在添加额外的括号,这会产生空节点,当增长方向为水平时,这不会立即起作用(请参阅使用 grow=left 时,tikz-qtree 中的叶节点与父节点不对齐)。这是由于 中默认设置锚点的方式tikz-qtree。我认为最好的解决方案是根本不使用它们。这显然会完全删除水平边缘。我还大幅清理了您的代码,为重复的内容和\node不需要的命令制作了一些样式。如果您有很多这样的树要做,我建议切换到forest使用相同基本输入方法(带标签的括号)但使用更可用的方法来设置单个节点和边缘的样式(无需显式\node\edge命令)。

\documentclass{article}
\usepackage{tikz,tikz-qtree}

\begin{document}
\begin{tikzpicture}[
            grow'=right,
            level distance = 2cm,
            level 2/.style={level distance=2cm,sibling distance=1cm},
            level 3+/.style={sibling distance=1cm},
            frontier/.style={distance from root=10cm},
            el/.style={sloped,font=\small},
            every leaf node/.style={font=\Large}
            ]
            \Tree 
            [.\node[draw,rectangle,minimum size=0.8cm]{1};
                \edge node[above,el]{Build A};
                [.\node[draw,circle]{2};
                    \edge node[above,el]{Good(0.6)};
                    [.\node[draw,circle]{4};
                         \edge node[above,el]{ Good(0.9)}; $\triangle$ 
                         \edge node[below,el]{ Bad(0.1)}; $\triangle$  
                    ]
                    \edge node[el,below]{Bad(0.4)};
                    [.\node[draw,circle]{5}; \edge node[above,el]{Bad(1)}; $\triangle$  ]
                ]
                \edge node[below,el]{Build B};
                [.\node[draw,circle]{3};
                    \edge node[above,el]{Good(0.6)};
                    [.\node[rectangle,draw,minimum size=0.8cm]{6};
                        \edge node[above,el]{ Expand};
                        [.\node[draw,circle]{8};
                             \edge node[above,el]{Good(0.9)}; $\triangle$ 
                             \edge node[below,el]{Bad(0.1)}; $\triangle$ 
                        ]
                        \edge node[above,el]{No Expand};
                        [.\node[draw,circle]{9};
                            \edge node[above,el]{Good(0.9)}; $\triangle$ 
                             \edge node[below,el]{Bad(0.1)}; $\triangle$  
                        ]
                    ]
                    \edge node[below,el]{Bad(0.4)};
                    [.\node[draw,circle]{7}; \edge node[above,el]{ Bad(1)}; $\triangle$ ]
                ]
            ]
        \end{tikzpicture}
\end{document}

代码输出

如果确实需要空节点,则可以将节点锚点设置为center

\documentclass{article}
\usepackage{tikz,tikz-qtree}

\begin{document}
\begin{tikzpicture}[
            grow'=right,
            every tree node/.style={anchor=center},
            level distance = 2cm,
            level 2/.style={level distance=2cm,sibling distance=1cm},
            level 3+/.style={sibling distance=1cm},
            frontier/.style={distance from root=10cm},
            el/.style={sloped,font=\small},
            every leaf node/.style={font=\Large},
                       ]
            \Tree 
            [.\node[draw,rectangle,minimum size=0.8cm]{1};
                \edge node[above,el]{Build A};
                [.\node[draw,circle]{2};
                    \edge node[above,el]{Good(0.6)};
                    [.\node[draw,circle]{4};
                        [ \edge node[above,el]{ Good(0.9)}; $\triangle$ ]
                        [ \edge node[below,el]{ Bad(0.1)}; $\triangle$  ]
                    ]
                    \edge node[el,below]{Bad(0.4)};
                    [.\node[draw,circle]{5}; \edge node[above,el]{Bad(1)}; $\triangle$  ]
                ]
                \edge node[below,el]{Build B};
                [.\node[draw,circle]{3};
                    \edge node[above,el]{Good(0.6)};
                    [.\node[rectangle,draw,minimum size=0.8cm]{6};
                        \edge node[above,el]{ Expand};
                        [.\node[draw,circle]{8};
                             [\edge node[above,el]{Good(0.9)}; $\triangle$ ]
                             [\edge node[below,el]{Bad(0.1)}; $\triangle$ ]
                        ]
                        \edge node[above,el]{No Expand};
                        [.\node[draw,circle]{9};
                            [\edge node[above,el]{Good(0.9)}; $\triangle$ ]
                            [ \edge node[below,el]{Bad(0.1)}; $\triangle$ ] 
                        ]
                    ]
                    \edge node[below,el]{Bad(0.4)};
                    [.\node[draw,circle]{7}; \edge node[above,el]{ Bad(1)}; $\triangle$ ]
                ]
            ]
        \end{tikzpicture}
\end{document}

第二个代码的输出

答案2

请查看它是否符合要求__我只打了一个标签,但应该足够了——其他标签遵循相同的语法

在此处输入图片描述

\documentclass{article}
\usepackage{tikz,tikz-qtree}
\usetikzlibrary{shapes,trees}
 \begin{tikzpicture}[level distance=2cm,
        level 1/.style={sibling distance=7cm},
        level 2/.style={sibling distance=4cm},
        level 3/.style={sibling distance=2cm},
        grow'=right,
        ]
        \tikzstyle{every node}=[draw,
        circle,
        draw=red!50!black,thick,
        fill=green!30]
        \node (Root){1}
        child {node{2}
                 child {node{4} 
                    child{coordinate[]{}
                        child {node[xshift=2cm]{{\color{red}\Large $\triangle$}}}} 
                    child{coordinate[]{}
                        child{node[xshift=2cm]{{\color{red}\Large $\triangle$}}} }
                      }
                child {node{5} 
                    child{coordinate{}
                        child {node[xshift=2cm]{{\color{red}\Large $\triangle$}}}}
                            }
                        edge from parent  node [draw=none,fill=none,sloped,above] {\tiny Build A}
                }
        child {node{3}
            child {node{6} 
                child {node{8} 
                    child{coordinate[]{}
                        child {node{\color{red}\Large $\triangle$}}} 
                    child{coordinate[]{}
                        child{node{\color{red}\Large $\triangle$}}}}
                child {node{9} 
                child{coordinate[]{}
                    child{node{\color{red}\Large $\triangle$}}}}
            }
            child { node{7} 
                child{coordinate{}
                    child {node[xshift=2cm]{{\color{red}\Large $\triangle$}}}}
            }
        }
 ;        
 \end{tikzpicture}
\end{document}

相关内容