[Tex/LaTex] drawing a syntax tree in tikz

tikz-pgftikz-treestrees

I would like to draw a syntax tree, which resembles the below syntax tree. I know that I can use tikz to draw syntax trees, but I can't seem to figure out how to draw a straight line down to a node, which is placed right below the parent node.

\begin{tikzpicture}
\node{S}
 child {node {a}}
 child {node {B}};
\end{tikzpicture}

The syntax tree I want to draw

Best Answer

Here you have a starting point:

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

\begin{document}

\begin{tikzpicture}[
  tlabel/.style={pos=0.4,right=-1pt,font=\footnotesize\color{red!70!black}},
]
\node{S}
child {node {a}}
child {node {S} 
  child {node {a}}
  child {node {S}
    child {node {$\varepsilon$}
      edge from parent node[tlabel,pos=0.2] {2}
    }
    edge from parent node[tlabel] {1}
  }
  child {node {B}
    child {node {B}
      child {node {b}
        edge from parent node[tlabel,pos=0.2] {5}
     }
      edge from parent node[tlabel] {4}
    }
    child {node {b}}
  }
    edge from parent node[tlabel] {1}
  }
child {node {B}
  child[missing] {}
  child[missing] {}
  child {node {b}
    edge from parent node[tlabel,pos=0.15,right=2pt] {5}
  }
};
\end{tikzpicture}

\end{document}

enter image description here