7

enter image description here

I have created a tree diagram using tikz on Latex. However I'm not sure how to go about highlighting/adding colour to a branch. For instance, looking at the tree that I've illustrated, I want to highlight(in red) the edges of the path from 343 to 357 to 357 to z = 375. Any help would be appreciated. Thanks.

\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}
6

You should be using forest for better (wait for cfr to add an example, for sure :-)). For your code, you have to add few red and black at suitable places.

\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}

enter image description here

  • Thanks so much! I'm new to latex and tikz, hence the long, messy code:) – alex12 Apr 8 '15 at 0:30
  • @alex12 Harish Kumar knows me too well, it seems ;). Did you want the labels to be red, as well? You only mentioned the edges, so I did something to stop the labels being red. But I can undo it if you want them red. Your code is not that long or messy. It is really good if you are new to it. (IMHO.) But forest allows trees to be specified with really concise code. – cfr Apr 8 '15 at 1:14
  • @cfr I know your love for forest ;-) – user11232 Apr 8 '15 at 1:25
4

If you are willing to use forest, this can be done fairly simply, the code can be simplified and some nodes can be created automatically. In this case, I've appended the f nodes automatically. I've created a style my x to make it easier to add the edge labels, and I've used a tier, in combination with 2 phantom nodes to manage the alignment of the terminal nodes.

This could probably be further automated and could certainly be further fine-tuned if you've a mind.

\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>forest</code> solution

If you don't want the edge from start to the first child to be red, delete for ancestors={edge=red} and just add two edge=red to the two 357 nodes instead:

  [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}]
          ]
        ]
      ]
    ]
  ]

per instructions

It is just that the for ancestors is so neat that I couldn't resist demonstrating it ;).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.