突出显示 TikZ 二叉树的某些节点

突出显示 TikZ 二叉树的某些节点

在这棵二项式树中我有 4 列节点。

我希望最后一列的整个列都用浅蓝色突出显示,并且可能在突出显示周围加一个边框。例如,用不透明的蓝色矩形或细椭圆突出显示最后一列节点。

刚刚开始使用 TikZ,但目前还没有任何进展。

我正在使用 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}

答案1

这里有两种解决方案。第一种解决方案使用column 4样式添加边框并填充矩阵第四列中的每个节点。第二种解决方案使用fitbackground库将新节点放在背景拟合tree-1-4tree-7-4节点上。

在此处输入图片描述

\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}

编辑:这是定位节点的另一种方法(使用positioning而不是matrix)。

在此处输入图片描述

\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}

答案2

这是一个新的解决方案hobby包(这个问题给了我第一次使用它的机会:无法抗拒,但我相信答案可以改进)。背后的想法很简单:要突出显示的区域由一组坐标决定,这些坐标实际上是矩阵的节点,并且已background用专用库填充。

Paul Gaborit 提供的解决方案非常有效,但我对不仅突出显示列而且突出显示树中的稀疏元素这一事实很感兴趣。

下面的示例包含两个突出显示的部分:OP 所需的部分和其他三个节点。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,backgrounds, hobby,calc}

\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] {};

    \begin{pgfonlayer}{background}
    % the requested case
    \draw[blue,fill=cyan!15,xscale=1.725] (tree-1-4.west) to[closed,curve through={(tree-1-4.north) .. (tree-1-4.east) .. (tree-7-4.east)..(tree-7-4.south).. (tree-7-4.west) }] (tree-1-4.west);

    % another case

   \draw[blue,fill=cyan!15,xscale=1.1] (tree-5-2.west) to[closed,curve through={([tension in=1.4]$(tree-5-2.west)!0.5!(tree-4-3.west)$)..([tension in=-0.75]tree-4-3.west)..(tree-2-3.west)..(tree-2-3.north)..(tree-2-3.east) .. ([tension in=-0.1]tree-4-3.east)..(tree-5-2.south east)}] (tree-5-2.west);
    \end{pgfonlayer}
    \end{tikzpicture}

\end{document}

结果:

在此处输入图片描述

答案3

我认为我找到了一个非常有趣的解决方案,因为它很简单。

这个想法是构建一条仅穿过感兴趣的节点的路径。为了突出显示,路径的宽度必须大于节点。此路径应位于后层,以免覆盖节点。

结果如下:

路径突出显示

代码如下:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\tikzset{
    my box/.style = {
        , line cap = round
        , line join = round
    }
}

\newcommand{\highlight}[3]{
    \path [my box, line width = #1, draw = #2] #3;

    \pgfmathsetmacro{\innerlinewidth}{0.9 * #1}
    \path [my box, line width = \innerlinewidth, draw = #2!20] #3;
}

\begin{document}
    \begin{tikzpicture} 
        \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] {};

        \begin{pgfonlayer}{background}
            \highlight{12mm}{blue}{(tree-2-3.center) -- (tree-4-3.center) -- (tree-5-2.center)}
            \highlight{20mm}{green}{(tree-1-4.center) -- (tree-3-4.center) -- (tree-5-4.center) -- (tree-7-4.center)}
        \end{pgfonlayer}
    \end{tikzpicture}
\end{document}

因此,我们可以使用库修饰获得其他类型的结果。下面是一些示例。

加上蛇形装饰的结果:

蛇形路径

以及 tikzset 代码:

\tikzset{
    my box/.style = {
        , line cap = round
        , line join = round
        , decorate
        , decoration = {
            snake
            , segment length = 20mm
            , amplitude = 1mm
        }
    }
}

另一个结果,采用随机步骤:

随机步进路径

以及 tikzset 代码:

\tikzset{
    my box/.style = {
        , line cap = round
        , line join = round
        , decorate
        , decoration = {
            random steps
            , segment length = 10mm
            , amplitude = 1mm
        }
    }
}

相关内容