如何使用 tikz 为树上的树枝/边缘着色?

如何使用 tikz 为树上的树枝/边缘着色?

在此处输入图片描述

我已经使用 Latex 创建了一个树形图tikz。但是我不确定如何突出显示/添加分支颜色。例如,查看我所说明的树,我想突出显示(红色)从343357357的路径的边缘z = 375。任何帮助都将不胜感激。谢谢。

\documentclass[a4paper,landscape] {article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc,shapes,backgrounds}
\usepackage{amsmath, amssymb}



\tikzset{
treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
arn/.style = {treenode, circle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=4.1ex},
arnrec/.style = {treenode, rectangle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=7.5ex,minimum width=4.0ex, minimum height=4.0ex},
arnsmall/.style = {treenode, circle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=1.5ex},
}

\begin{document}
\centering
\tiny %font size
\begin{tikzpicture}[shorten >=1pt,scale=0.85]
\node [sibling distance=10mm,level distance = 0.5cm] [arn] {start} 
 child{[sibling distance=2.8cm,level distance = 1.5cm] node [arn] {343}
            child{ [sibling distance=0.8cm] node [arn] {343} 
                        child{[level distance=0.6cm]node [arn] {367} 
                                child{node [arnsmall] {f}}
                        edge from parent node[above left]{$x_{32}$}
                        } 
                        child{[level distance=0.6cm] node [arn] {384}
                                child{node [arnsmall] {f}}
                        edge from parent node[above] {$x_{35}$}
                        }
                        child{ [level distance = 0.6cm] node [arn] {376}
                                child{node [arnsmall] {f}}
                        edge from parent node[above right]
                        {$x_{36}$}
                        }
                edge from parent node[above left]
                {$x_{21}$} %label
            }
            child{ [sibling distance=0.8cm] node [arn] {357} 
                        child{ [level distance = 0.6cm] node [arn] {386} 
                                child{node [arnsmall] {f}}
                                edge from parent node[above left] {$x_{31}$}
                        }
                        child{[level distance=0.6cm] node [arn] {389}
                                child{node [arnsmall] {f}}
                            edge from parent node[above] {$x_{35}$}
                        }
                        child{[sibling distance=0.7cm,level distance = 1.4cm] node [arn] {357} 
                                child{ node [arnrec] {z=396}
                                    edge from parent node[below left]
                                    {$x_{42}$}
                                }   
                                child{ node [arnrec] {z=375}
                                    edge from parent node[below right]
                                    {$x_{45}$}
                                }
                            edge from parent node[above right] {$x_{36}$}
                        }
            edge from parent node[above left]
            {$x_{23}$} %label
            }                         
    edge from parent node[above right]
    {$x_{14}$} %label
    }
;
\end{tikzpicture}
\end{document}

答案1

您应该使用forest更好的(等待 cfr 添加示例,当然:-))。对于您的代码,您必须在适当的位置添加red一些。black

\documentclass[a4paper,landscape] {article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc,shapes,backgrounds}
\usepackage{amsmath, amssymb}



\tikzset{
treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
arn/.style = {treenode, circle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=4.1ex},
arnrec/.style = {treenode, rectangle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=7.5ex,minimum width=4.0ex, minimum height=4.0ex},
arnsmall/.style = {treenode, circle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=1.5ex},
}

\begin{document}
\centering
\tiny %font size
\begin{tikzpicture}[shorten >=1pt,scale=0.85]
\node [sibling distance=10mm,level distance = 0.5cm] [arn] {start}
 child{[sibling distance=2.8cm,level distance = 1.5cm] node [arn] {343}
            child{ [sibling distance=0.8cm] node [arn] {343}
                        child{[level distance=0.6cm]node [arn] {367}
                                child{node [arnsmall] {f}}
                        edge from parent node[above left]{$x_{32}$}
                        }
                        child{[level distance=0.6cm] node [arn] {384}
                                child{node [arnsmall] {f}}
                        edge from parent node[above] {$x_{35}$}
                        }
                        child{ [level distance = 0.6cm] node [arn] {376}
                                child{node [arnsmall] {f}}
                        edge from parent node[above right]
                        {$x_{36}$}
                        }
                edge from parent node[above left]
                {$x_{21}$} %label
            }
            child{ [sibling distance=0.8cm,red] node [arn] {357}
                        child{ [level distance = 0.6cm,black] node [arn] {386}
                                child{node [arnsmall] {f}}
                                edge from parent node[above left] {$x_{31}$}
                        }
                        child{[level distance=0.6cm,black] node [arn] {389}
                                child{node [arnsmall] {f}}
                            edge from parent node[above] {$x_{35}$}
                        }
                        child{[sibling distance=0.7cm,level distance = 1.4cm,red] node [arn] {357}
                                child{[black] node[arnrec] {z=396}
                                    edge from parent node[below left]
                                    {$x_{42}$}
                                }
                                child{ node [arnrec] {z=375}
                                    edge from parent node[below right]
                                    {$x_{45}$}
                                }
                            edge from parent node[above right] {$x_{36}$}
                        }
            edge from parent node[above left]
            {$x_{23}$} %label
            }
    edge from parent node[above right]
    {$x_{14}$} %label
    }
;
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

如果您愿意使用forest,这可以相当简单地完成,代码可以简化,并且可以自动创建一些节点。在这种情况下,我已f自动附加节点。我创建了一种样式my x,以便更轻松地添加边缘标签,并且我使用了tier,结合 2 个phantom节点来管理终端节点的对齐。

如果你愿意的话,这可能会进一步自动化,并且肯定可以进一步微调。

\documentclass[tikz,border=10pt,multi] {standalone}
\usepackage{forest}
\standaloneenv{forest,tikzpicture}

\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered, font=\sffamily},
  arn/.style = {treenode, circle, font=\sffamily\bfseries, draw=black, fill=white, text width=4.75ex},
  arnrec/.style = {treenode, rectangle, font=\sffamily\bfseries, draw=black,  fill=white, text width=7.5ex,minimum width=4.0ex, minimum height=4.0ex},
  arnsmall/.style = {treenode, circle, font=\sffamily\bfseries, draw=black, fill=white, text width=1.5ex},
}

\begin{document}
\begin{forest}
  make f/.style={
    append={[f, arnsmall, tier=prez]},
    l sep+=-5mm,
  },
  make empty/.style={
    coordinate,
    no edge,
    tier=prez,
    for children={
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!uu.parent anchor) -- (.child anchor)\forestoption{edge label};
      }
    },
  },
  my x/.style n args=2{
    edge label={node [midway, #2, font=\sffamily, text=black] {$x_{#1}$}},
  },
  for tree={
    arn,
    l sep+=5mm,
  }
  [start
    [343, my x={14}{right}
      [343, for children={make f}, my x={21}{left}
        [367, my x={32}{left}
        ]
        [384, my x={35}{fill=white}
        ]
        [376, my x={36}{right}
        ]
      ]
      [357, my x={23}{right}
        [386, make f, my x={31}{left}
        ]
        [389, make f, my x={35}{fill=white}
        ]
        [357, my x={36}{right}
          [, make empty
            [{z=396}, arnrec, my x={42}{left, pos=.75}]
          ]
          [, make empty
            [{z=375}, arnrec, for ancestors={edge=red}, edge=red, my x={45}{right, pos=.75}]
          ]
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

<code>森林</code> 解决方案

如果您不希望start到第一个子节点的边为红色,请删除这两个节点,然后for ancestors={edge=red}只需添加两个即可:edge=red357

  [start
    [343, my x={14}{right}
      [343, for children={make f}, my x={21}{left}
        [367, my x={32}{left}
        ]
        [384, my x={35}{fill=white}
        ]
        [376, my x={36}{right}
        ]
      ]
      [357, my x={23}{right}, edge=red
        [386, make f, my x={31}{left}
        ]
        [389, make f, my x={35}{fill=white}
        ]
        [357, my x={36}{right}, edge=red
          [, make empty
            [{z=396}, arnrec, my x={42}{left, pos=.75}]
          ]
          [, make empty
            [{z=375}, arnrec, edge=red, my x={45}{right, pos=.75}]
          ]
        ]
      ]
    ]
  ]

按照说明

只是因为它for ancestors太整洁了,我忍不住要演示一下 ;)。

相关内容