0

I would like to use Tikz to generate pictures of trees for a talk I'm writing. I need to be able to color the edges of the tree and add numeric weights to each edge. I've got code that can do the first task, and I'm wondering if there's an easy way to get it to do the second. (I've seen examples elsewhere of Tikz trees with weighted edges and in them it looks like vertices need to be drawn into the tree, and I don't want vertices in my trees.)

$\begin{tikzpicture}[thick, level distance=6mm]
   \tikzstyle{level 2}=[sibling distance=15mm]
   \tikzstyle{level 3}=[sibling distance=5mm]
   \coordinate
      child[blue] 
      {
            child[blue]   {child child}
            child[blue] {child child[blue]}
            child[red] {child {child} child child[red]}
      } ;
\end{tikzpicture}$

This yields the following illustration, which is roughly what I want. But how can I add weights to each edge?

My tree

1

like this?

enter image description here

\documentclass[tikz, margin=3mm]{standalone}

\begin{document}
    \begin{tikzpicture}[
thick, 
level distance = 6mm,
level 2/.style = {sibling distance=15mm},
level 3/.style = {sibling distance=5mm},
every node/.append style = {font=\footnotesize}
                        ]
\coordinate
  child[blue]
  {
        child[blue] {child {
                     edge from parent node[left] {1}
                            }
                     child {
                     edge from parent node[right] {2}
                            }
        edge from parent node[left] {a}
                    }
        child[red]  {child {child {
                     edge from parent node[left] {5}
                                  }
                     edge from parent node[left] {3}
                            }
                     child {
                     edge from parent node[right] {4}
                            }
        edge from parent node[right] {b}
                    }
  edge from parent node[right] {ab}
  } ;
    \end{tikzpicture}
\end{document} 
| improve this answer | |
1

The edge from parent gets automatically inserted. You can modify it using the edge from parent path key. Here is a modification that allows you to put edge labels on it using the quotes syntax. Then you need only to say child[el={"2"}] to get a 2 on the edge, and if you say child[el={"2"'}], the 2 will be swapped, i.e. on the other side.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}[thick, level distance=6mm,
    level 2/.style={sibling distance=15mm},
   level 3/.style={sibling distance=5mm,pos=0.8},
   edge from parent path={
    (\tikzparentnode\tikzparentanchor) edge[my el] 
    (\tikzchildnode\tikzchildanchor)},
    el/.code={\tikzset{my el/.style={#1}}},
    my el/.style={},
    font=\footnotesize]
   \coordinate
      child[blue] 
      {
            child[blue,el={"1"'}]   {
                child[el={"2"'}]
                child[el={"3"}]
                }
            child[blue,el={"4"'}] {
                child[el={"5"'}] 
                child[blue,el={"6"}]}
            child[red,el={"7"}] {
                child[el={"8"'}] {
                    child[el={"9"'}]} 
                child[el={"5"}] 
                child[red,el={"11"}]}
      } ;
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

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.