2

Using this code:

\begin{tikzpicture}[level distance=1.5cm,
                level 1/.style={sibling distance=1.3cm},
                level 2/.style={sibling distance=1cm}]
                level 3/.style={sibling distance=0.8cm}]
                \tikzstyle{every node}=[circle,draw]

                \node (Root) [red] {e}

                child {
                    node {a}
                    child { 
                        node {b} 
                        child {node {c}} 
                    }
                    child { node {d} }
                }
                child {
                    node {h}
                }
                child {
                    node {f} 
                    child { node {g} }
                };
\end{tikzpicture}

I produce this image:

enter image description here

My question is how I can produce edge weights on this image for example if I wanted an edge weight from e to a or a to b, how would I produce it for this tree?

Im using this library: \usetikzlibrary{trees}

  • what you mean with "edge weight"? – Zarko May 1 '18 at 11:25
  • @Zarko Edge weight meaning if there is a edge from a to b in my tree above , there can be a 5 written above it (edge weight of 5) if that makes sense – bigfocalchord May 1 '18 at 11:29
1

You can use edge from parent, but this may require switching back.

enter image description here

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
                level 1/.style={sibling distance=1.3cm},
                level 2/.style={sibling distance=1cm}]
                level 3/.style={sibling distance=0.8cm}]
                \tikzstyle{every node}=[circle,draw]

                \node (Root) [red] {e}

                child {
                    node {a}
                    child { 
                        node {b} 
                        edge from parent[blue]
                        child {node[black] {c}
                        edge from parent[black]} 
                    }
                    child { node {d} }
                 edge from parent[double]
                }
                child {
                    node {h}
                }
                child {
                    node {f} 
                    child { node {g} }
                };
\end{tikzpicture}
\end{document}
1

like this?

enter image description here

for labels of edges serve edge from parent node[<options>] {<label>}. it should be on correct place (after node before it you like to have label), see mwe below:

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

\begin{document}
    \begin{tikzpicture}[
   level distance = 1.5cm,
   level 1/.style = {sibling distance=1.3cm},
   level 2/.style = {sibling distance=1.0cm},
   level 3/.style = {sibling distance=0.8cm},
every node/.style = {circle,draw},
       lbl/.style = {rectangle, draw=none, #1,% position
                     font=\footnotesize}
                        ]
%
\node (Root) [red] {e}
    child {node {a}
        child {node {b}
            child {node {c}
            edge from parent node[lbl=left] {$bc$}
            }
        edge from parent node[lbl=left] {$ab$}
       }
    child {node {d}
    edge from parent node[lbl=right] {$ad$}
        }
    edge from parent node[lbl=left] {$ea$}
      }
    child { node {h}}
    child { node {f}
        child { node {g} }
          };
    \end{tikzpicture}
\end{document}

used labels of edges are selected so that indicate nodes name, where edge begin and end. explanation about this you can find in 21.6 Edges From the Parent Node, page 323 in "tikz & pgf manual", version 3.0.1a,

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.