如何使用 TikZ 向树形图中的节点连接器添加标签

如何使用 TikZ 向树形图中的节点连接器添加标签

我想将标签“H”和“L”添加到 X 和 Y 连接器,就像 Z 节点下方的那些一样。这是我目前的代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
  arn_x/.style = {treenode, circle, white, draw=black,fill=red,
     text width=1.5em, very thick},
  }

\begin{document}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
  level distance = 1.5cm}] 
\node [arn_x] {X}
    child{ node [arn_x] {Y}      
                child{ node [arn_x] {Z} 
                child{ node  {LLL} edge from parent node[above left]
                         {$L$}} 
                            child{ node  {LLH} edge from parent node[above right]
                        {$H$}   }
            }
            child{ node [arn_x] {Z}
                                child{ node  {LHL} edge from parent node[above left]
                                {$L$}}                      
                            child{ node  {LHH} edge from parent node[above right]
                                {$H$}   }
            }                            
    }
     child{ node [arn_x] {Y} 
        child{ node [arn_x] {Z} 
            child{ node  {HLL} edge from parent node[above left]
                {$L$}} 
            child{ node  {HLH} edge from parent node[above right]
                {$H$}   }
        }
        child{ node [arn_x] {Z}
            child{ node  {HHL} edge from parent node[above left]
                {$L$}}                      
            child{ node  {HHH} edge from parent node[above right]
                {$H$}   }
        }                            
    }
; 
\end{tikzpicture}
\end{document}

答案1

是的,这有点令人困惑。您需要“较晚”地添加节点。请参阅 pgfmanual 中第 21.3 节之前的描述。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
  arn_x/.style = {treenode, circle, white, draw=black,fill=red,
     text width=1.5em, very thick},
  }

\begin{document}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
  level distance = 1.5cm}] 
\node [arn_x] {X}
    child{ node [arn_x] {Y}      
                child{ node [arn_x] {Z} 
                child{ node  {LLL} edge from parent node[above left]
                         {$L$}} 
                            child{ node  {LLH} edge from parent node[above right]
                        {$H$}   } edge from parent node[above left] {$L$}
            } 
            child{ node [arn_x] {Z}
                                child{ node  {LHL} edge from parent node[above left]
                                {$L$}}                      
                            child{ node  {LHH} edge from parent node[above right]
                                {$H$}   } edge from parent node[above right] {$H$}
            } edge from parent node[above left] {$L$}                            
    }
     child{ node [arn_x] {Y} 
        child{ node [arn_x] {Z} 
            child{ node  {HLL} edge from parent node[above left]
                {$L$}} 
            child{ node  {HLH} edge from parent node[above right]
                {$H$}   } edge from parent node[above left] {$L$}
        } 
        child{ node [arn_x] {Z}
            child{ node  {HHL} edge from parent node[above left]
                {$L$}}                      
            child{ node  {HHH} edge from parent node[above right]
                {$H$}   } edge from parent node[above right] {$H$}
        } edge from parent node[above right] {$H$}                           
    }
; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容