[Tex/LaTex] tikz tree fork down edge labels

nodestikz-pgftikz-treestrees

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm},
edge from parent fork down]
\tikzstyle{every node}=[circle,draw]

\node (Root) [red] {3}
    child {
    node {1}
    child { node {4} edge from parent node[above,draw=none] {help!} }
    child { node {1} }
    child { node {3} }
}
child {
    node {2}
    child { node {3} }
    child { node {2} }
    child { node {5} }
};

\end{tikzpicture}
\end{document}

yields

enter image description here

but I'd like to add textual labels the positions marked with the red and blue text below.

enter image description here

This post somewhat addresses the red issue but not the blue issue.

Getting the (horizontally running) text along that first, short line before it splits is what is giving me the most trouble.

It seems that first stem needs to be taller to allow room for a label there, especially when the right branch will have its own label.

Best Answer

Does this illustration help to understand the relation between path operators (like -- and -|) and the pos key?

For the last example, your will need my paths.ortho library which has been developed for this reason (amongst others).

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{trees}
\tikzset{level distance=1.5cm,
         level 1/.style={sibling distance=3.5cm},
         level 2/.style={sibling distance=1cm}}
\makeatletter
\def\tikz@ege@from@parent@macro@node#1#2{%
  \pgfextra{\def\tikz@@tonodes{#2}}
  [style=edge from parent, #1, /utils/exec=\tikz@node@is@a@labeltrue]
  \tikz@edge@to@parent@path}
\tikzset{
  edge from parent fork down*/.style={
    edge from parent macro=\tikz@ege@from@parent@macro@node,
    edge from parent path={(\tikzparentnode\tikzparentanchor) |-
    ([shift=(down:.5\tikzleveldistance)]
      \tikzparentnode\tikzparentanchor-|\tikzchildnode\tikzchildanchor)
      \tikz@@tonodes -- (\tikzchildnode\tikzchildanchor)}}}
\makeatother
\usetikzlibrary{paths.ortho}
\tikzset{
  edge from parent fork down ortho/.style={
    edge from parent path={{[hvvh/distance=.5\tikzleveldistance, hvvh/from center]
      (\tikzparentnode\tikzparentanchor) |-| (\tikzchildnode\tikzchildanchor)}}}}
\newcommand*\myTree[1]{%
\tikz[#1]\path [every child node/.append style={draw, circle}]
  node[every child node] (Root) [red] {3}
    child { node {1} 
      child { node {4} } child { node {1} } child { node {3} }
      edge from parent[font=\tiny] node[at start] {0} node [pos=.125] {.125}
        node[near start] {.25} node[midway] {.5} node[near end] {.75}
        node [pos=.875] {.875} node[at end] {1}}
    child { node {2}
      child { node {3} } child { node {2} } child { node {5} }
      edge from parent[font=\tiny] node[at start] {0} node [pos=.125] {.125}
        node[near start] {.25} node[midway] {.5} node[near end] {.75}
        node [pos=.875] {.875} node[at end] {1}};}
\begin{document}
\myTree{edge from parent fork down}
\myTree{edge from parent fork down*}
\myTree{edge from parent fork down ortho}
\end{document}

Output

enter image description here enter image description here enter image description here