[Tex/LaTex] Highlighting some nodes of a TikZ binomial tree

highlightingnodestikz-pgftikz-trees

In this binomial tree I have 4 columns of nodes.

I want the entire last column to be highlighted with light blue, and perhaps a border around this highlighting. Something like a opaque blue-ish rectangle or thin oval highlighting the entire last column of nodes.

Just started with TikZ and nothing is working so far.

I'm using pdfLaTeX.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

   \begin{tikzpicture}[>=stealth,sloped]
      \matrix (tree) [%
        matrix of nodes,
        minimum size=1cm,
        column sep=3.5cm,
        row sep=1cm,
      ]
      {
                &                    &                     &  $^\tau S_0$     \\
                &                    &  $^{2\Delta t}S_0$  &              \\
                & $^{\Delta t}S_0$   &                     &  $^\tau S_1$     \\
        $^0S_0$ &                    &  $^{2\Delta t}S_1$  &              \\
                & $^{\Delta t}S_1$  &                      &  $^\tau S_2$     \\
                &                    &  $^{2\Delta t}S_2$  &              \\
                &                    &                     &  $^\tau S_2$     \\
      };
      \draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
      \draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
      \draw[->] (tree-3-2) -- (tree-2-3) node [midway,above] {};
      \draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
      \draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
      \draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};

    \draw[->] (tree-2-3) -- (tree-1-4) node [midway,above] {};
      \draw[->] (tree-2-3) -- (tree-3-4) node [midway,below] {};
      \draw[->] (tree-4-3) -- (tree-3-4) node [midway,above] {};
      \draw[->] (tree-4-3) -- (tree-5-4) node [midway,below] {};
      \draw[->] (tree-6-3) -- (tree-5-4) node [midway,above] {};
      \draw[->] (tree-6-3) -- (tree-7-4) node [midway,below] {};

    \end{tikzpicture}

\end{document}

Best Answer

Here are two solutions. First solution uses column 4 style to add a border and to fill each node in fourth column of your matrix. Second solution uses fit and background libraries to put a new node on background fitting tree-1-4 and tree-7-4 nodes.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,backgrounds}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
  \matrix (tree) [%
  matrix of nodes,
  minimum size=1cm,
  column sep=3.5cm,
  row sep=1cm,
  % fill and draw each node of fourth column 
  column 4/.style={nodes={draw,fill=cyan!30,rounded corners}},
  ]
  {
            &                  &                   & $^\tau S_0$ \\
            &                  & $^{2\Delta t}S_0$ &             \\
            & $^{\Delta t}S_0$ &                   & $^\tau S_1$ \\
    $^0S_0$ &                  & $^{2\Delta t}S_1$ &             \\
            & $^{\Delta t}S_1$ &                   & $^\tau S_2$ \\
            &                  & $^{2\Delta t}S_2$ &             \\
            &                  &                   & $^\tau S_2$ \\
  };
  \draw[->] (tree-4-1) -- (tree-3-2) node [midway,above] {$p$};
  \draw[->] (tree-4-1) -- (tree-5-2) node [midway,below] {$1-p$};
  \draw[->] (tree-3-2) -- (tree-2-3) node [midway,above] {};
  \draw[->] (tree-3-2) -- (tree-4-3) node [midway,below] {};
  \draw[->] (tree-5-2) -- (tree-4-3) node [midway,above] {};
  \draw[->] (tree-5-2) -- (tree-6-3) node [midway,below] {};

  \draw[->] (tree-2-3) -- (tree-1-4) node [midway,above] {};
  \draw[->] (tree-2-3) -- (tree-3-4) node [midway,below] {};
  \draw[->] (tree-4-3) -- (tree-3-4) node [midway,above] {};
  \draw[->] (tree-4-3) -- (tree-5-4) node [midway,below] {};
  \draw[->] (tree-6-3) -- (tree-5-4) node [midway,above] {};
  \draw[->] (tree-6-3) -- (tree-7-4) node [midway,below] {};

  % put a new node on background fitting (tree-1-4) and (tree-7-4) nodes
  \begin{scope}[on background layer]
    \node[fit=(tree-1-4)(tree-7-4),
    fill=blue!20,rounded corners,draw=blue,line width=1pt]{};
  \end{scope}
\end{tikzpicture}
\end{document}

Edit: Here is another method to position your nodes (using positioning instead of matrix).

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
  \tikzset{
    on grid,
    node distance=1cm and 3.5cm,
    col4/.style={fill=cyan!20,draw,rounded corners},
    every node/.style={minimum size=1cm},
  }
  % first column
  \node (n-0-0) at (-5,-3) {$^0S_0$};

  % second column
  \node[above right=of n-0-0] (n-1-0) {$^{\Delta t}S_0$};
  \node[below right=of n-0-0] (n-1-1) {$^{\Delta t}S_1$};

  % third column
  \node[above right=of n-1-0] (n-2-0) {$^{2\Delta t}S_0$};
  \node[below right=of n-1-0] (n-2-1) {$^{2\Delta t}S_1$};
  \node[below right=of n-1-1] (n-2-2) {$^{2\Delta t}S_2$};

  % fourth column
  \node[col4,above right=of n-2-0] (n-3-0) {$^{\tau}S_0$};
  \node[col4,below right=of n-2-0] (n-3-1) {$^{\tau}S_1$};
  \node[col4,below right=of n-2-1] (n-3-2) {$^{\tau}S_2$};
  \node[col4,below right=of n-2-2] (n-3-3) {$^{\tau}S_2$};

  \draw[->] (n-0-0) -- (n-1-0) node [midway,above] {$p$};
  \draw[->] (n-0-0) -- (n-1-1) node [midway,below] {$1-p$};
  \foreach \col in {1,2}{
    \pgfmathtruncatemacro{\nextcol}{\col+1}
    \foreach \line in {0,...,\col}{
      \pgfmathtruncatemacro{\nextline}{\line + 1}
      \draw[->] (n-\col-\line) -- (n-\nextcol-\line);
      \draw[->] (n-\col-\line) -- (n-\nextcol-\nextline);
    }
  }

  \begin{scope}[on background layer]
    \node[fit=(n-3-0)(n-3-3),inner sep=.5em,
    fill=blue!20,rounded corners,draw=blue,line width=1pt]{};
  \end{scope}
\end{tikzpicture}
\end{document}