[Tex/LaTex] How to i make this tree diagram with labels on edges and vertices

tikz-pgftrees

Tree diagram
I would like to understand how to add labels to edges and vertices and how to make an invisible vertex such that an edge can start or end in it, but the vertex itself is not visible. I'm interested only in ascending trees of this type, but i couldn't find any similar question or any specific documentation about it. I'd rather understand how to improve my (poor) code instead of changing it at all if it's possible.

CODE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}
\graph [chain shift=(45:1), branch left, nodes={inner sep=0pt, minimum size=2.5pt, circle, fill, draw}, empty nodes]
{a -- b -- {c,d,e -- {f,g}}
};
\end{tikzpicture}
\end{document}

Any help would be appreciated.

Best Answer

Figuring out how to do stuff with the graph syntax is really just a question of reading the relevant part of the manual and/or looking at examples. If it is a tree, I would use Forest since you are not using the automatic layout algorithms anyway. However, here's an example based on your code which uses the quotes library to label the edges and the standard label option for the vertices. coordinate is used to override the default node keys you've considered for a single node so that it isn't drawn.

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{graphs,quotes}
\begin{document}
\begin{tikzpicture}
  \graph [chain shift=(45:1), branch left, nodes={inner sep=0pt, minimum size=2.5pt, circle, fill, draw}, empty nodes, every edge quotes/.append style={font=\scriptsize, inner sep=0pt}]
  {%
    a -- ["$a$"'] b[shape=coordinate] -- {c[label=0:$w$, >"$c$"'],d[label=0:$v$],e[label=180:$u$, >"$b$"] -- {f,g}}
  };
\end{tikzpicture}
\end{document}

tree in graph syntax

EDIT

Note that the way the nodes are placed depends on the placement strategy in use. chain shift shifts nodes at new levels. 45:1 means the node is place at an angle of 45 and distance of 1 from the current node. branch left means that branches always go left.

You can

  1. place nodes manually (you specify exactly where to put each node);
  2. use an online placement strategy, like the one you've tried (you specify a strategy which takes account of the graph-so-far but not the graph-yet-to-come);
  3. use an offline placement strategy, which requires the advanced facilities supported by LuaTeX (you specify a strategy which takes account of the entire graph).

Sticking to the current strategy type, you can, for example, change it to grow up, branch left:

a different strategy

Notice that the graph is specified in the same way - only one of the specified strategies (what to do for a new level of nodes, as opposed to a new branch) changes.

\begin{tikzpicture}
  \graph [grow up, branch left, nodes={inner sep=0pt, minimum size=2.5pt, circle, fill, draw}, empty nodes, every edge quotes/.append style={font=\scriptsize, inner sep=0pt}]
  {%
    a -- ["$a$"'] b[shape=coordinate] -- {c[label=0:$w$, >"$c$"'],d[label=0:$v$],e[label=180:$u$, >"$b$"] -- {f,g}}
  };
\end{tikzpicture}

If you want something more like this:

tree layout

then you might want to consider using the graphdrawing algorithms library with the trees graph-drawing library. This is an offline placement strategy requiring LuaTeX for compilation.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{luatex85}
\usetikzlibrary{graphs,quotes,graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}
  \graph [tree layout, grow=up, nodes={inner sep=0pt, minimum size=2.5pt, circle, fill, draw}, empty nodes, every edge quotes/.append style={font=\scriptsize, inner sep=0pt}]
  {%
    a -- ["$a$"'] b -- {c[label=0:$w$, >"$c$"'],d[label=0:$v$],e[label=180:$u$, >"$b$"] -- {f,g}}
  };
\end{tikzpicture}
\end{document}

If LuaTeX is not an option, for some reason, then you could always try something like Forest. (Forest can easily do this, as I mentioned above. It is probably the most powerful and flexible package for drawing trees, although it obviously does not support the other kinds of graphs the LuaTeX algorithms do.)