一个节点中的两个数字 - 二项式树 - 矩阵 - tikz

一个节点中的两个数字 - 二项式树 - 矩阵 - tikz

在下面的二叉树中,我想要两个数字,一个在另一个之上,位于一个节点位置。因此,在第 1 行、第 5 列的位置,我想要 12.625,然后在 12.625 下方放置 0.183。我希望它们位于相同的节点位置 (1,5)。

有可能有这样的多线节点吗?

\begin{tikzpicture}
  \matrix (tree) [matrix of nodes,column sep=1.5cm]
          {
            &      &        &         & 12.625\\
            &      &        & 11.91   & \\
            &      & 11.236 &         & 11.316 \\
            & 10.6 &        & 10.67   & \\
         10 &      & 10.07  &         & 10.140 \\
            & 9.5  &        & 9.567   & \\
            &      & 9.025  &         & 9.088 \\
            &      &        & 8.574   & \\
            &      &        &         & 8.145 \\
          };
          \draw[->] (tree-5-1)--(tree-4-2);
          \draw[->] (tree-4-2)--(tree-3-3);
          \draw[->] (tree-3-3)--(tree-2-4);
          \draw[->] (tree-2-4)--(tree-1-5);
          \draw[->] (tree-2-4)--(tree-3-5);
          \draw[->] (tree-3-3)--(tree-4-4);
          \draw[->] (tree-4-4)--(tree-3-5);
          \draw[->] (tree-4-2)--(tree-5-3);
          \draw[->] (tree-5-3)--(tree-4-4);
          \draw[->] (tree-4-4)--(tree-5-5);
          \draw[->] (tree-5-1)--(tree-6-2);
          \draw[->] (tree-6-2)--(tree-5-3);
          \draw[->] (tree-5-3)--(tree-6-4);
          \draw[->] (tree-6-4)--(tree-5-5);
          \draw[->] (tree-6-2)--(tree-7-3);
          \draw[->] (tree-7-3)--(tree-6-4);
          \draw[->] (tree-6-4)--(tree-7-5);
          \draw[->] (tree-7-3)--(tree-8-4);
          \draw[->] (tree-8-4)--(tree-7-5);
          \draw[->] (tree-8-4)--(tree-9-5);
\end{tikzpicture}

答案1

一种可能性是将节点留空,稍后使用它来垂直移位所需的数字。

借助命令,我也自动化了箭头的代码\foreach

这是我的代码:

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}   
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
  \matrix (tree) [matrix of nodes,column sep=1.5cm,minimum width=1.3cm,minimum height=0.6cm]
          {
            &      &        &         & {}\\
            &      &        & 11.91   & \\
            &      & 11.236 &         & 11.316 \\
            & 10.6 &        & 10.67   & \\
         10 &      & 10.07  &         & 10.140 \\
            &  9.5 &        &  9.567  & \\
            &      &  9.025 &         & 9.088 \\
            &      &        &  8.574  & \\
            &      &        &         & 8.145 \\
          };
    \foreach\i/\j in {5/1,4/2,6/2,3/3,5/3,7/3,2/4,4/4,6/4,8/4}
    {%
      \pgfmathtruncatemacro\l{\i-1}
      \pgfmathtruncatemacro\m{\i+1}
      \pgfmathtruncatemacro\n{\j+1}
      \draw[->] (tree-\i-\j) -- (tree-\l-\n);
      \draw[->] (tree-\i-\j) -- (tree-\m-\n);
    }
    \node at (tree-1-5) [yshift=0.2cm] {12.625};
    \node at (tree-1-5) [yshift=-0.2cm] {0.183};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

一种方法是强制节点具有文​​本宽度,然后可以将文本分成两行。

矩阵中的两条线节点

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}
        \matrix (tree) [matrix of nodes,column sep=1.5cm,text width=1cm,align=center]
          {
            &      &        &         & {12.625\\0.183}\\
            &      &        & 11.91   & \\
            &      & 11.236 &         & 11.316 \\
            & 10.6 &        & 10.67   & \\
         10 &      & 10.07  &         & 10.140 \\
            & 9.5  &        & 9.567   & \\
            &      & 9.025  &         & 9.088 \\
            &      &        & 8.574   & \\
            &      &        &         & 8.145 \\
          };
          \draw[->] (tree-5-1)--(tree-4-2);
          \draw[->] (tree-4-2)--(tree-3-3);
          \draw[->] (tree-3-3)--(tree-2-4);
          \draw[->] (tree-2-4)--(tree-1-5);
          \draw[->] (tree-2-4)--(tree-3-5);
          \draw[->] (tree-3-3)--(tree-4-4);
          \draw[->] (tree-4-4)--(tree-3-5);
          \draw[->] (tree-4-2)--(tree-5-3);
          \draw[->] (tree-5-3)--(tree-4-4);
          \draw[->] (tree-4-4)--(tree-5-5);
          \draw[->] (tree-5-1)--(tree-6-2);
          \draw[->] (tree-6-2)--(tree-5-3);
          \draw[->] (tree-5-3)--(tree-6-4);
          \draw[->] (tree-6-4)--(tree-5-5);
          \draw[->] (tree-6-2)--(tree-7-3);
          \draw[->] (tree-7-3)--(tree-6-4);
          \draw[->] (tree-6-4)--(tree-7-5);
          \draw[->] (tree-7-3)--(tree-8-4);
          \draw[->] (tree-8-4)--(tree-7-5);
          \draw[->] (tree-8-4)--(tree-9-5);
    \end{tikzpicture}
\end{document}

请注意,如果您觉得第 1 列和第 2 列的空白太多,您可以为每一列单独设置文本宽度:

        \matrix (tree) [matrix of nodes,column sep=1.5cm,text width=1cm,align=center,
            column 1/.style={text width=5mm},
            column 2/.style={text width=8mm},
            ]

第一列和第二列的空白较少

现在,为了正确放置箭头,我建议您将节点高度稍微提高一点minimum height=7mm

最终稿

完整代码:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}
        \matrix (tree) [matrix of nodes,column sep=1.5cm,text width=1cm,minimum height=7mm,align=center,
            column 1/.style={text width=5mm},
            column 2/.style={text width=8mm},
            ]
          {
            &      &        &         & {12.625\\0.183}\\
            &      &        & 11.91   & \\
            &      & 11.236 &         & 11.316 \\
            & 10.6 &        & 10.67   & \\
         10 &      & 10.07  &         & 10.140 \\
            & 9.5  &        & 9.567   & \\
            &      & 9.025  &         & 9.088 \\
            &      &        & 8.574   & \\
            &      &        &         & 8.145 \\
          };
          \draw[->] (tree-5-1)--(tree-4-2);
          \draw[->] (tree-4-2)--(tree-3-3);
          \draw[->] (tree-3-3)--(tree-2-4);
          \draw[->] (tree-2-4)--(tree-1-5);
          \draw[->] (tree-2-4)--(tree-3-5);
          \draw[->] (tree-3-3)--(tree-4-4);
          \draw[->] (tree-4-4)--(tree-3-5);
          \draw[->] (tree-4-2)--(tree-5-3);
          \draw[->] (tree-5-3)--(tree-4-4);
          \draw[->] (tree-4-4)--(tree-5-5);
          \draw[->] (tree-5-1)--(tree-6-2);
          \draw[->] (tree-6-2)--(tree-5-3);
          \draw[->] (tree-5-3)--(tree-6-4);
          \draw[->] (tree-6-4)--(tree-5-5);
          \draw[->] (tree-6-2)--(tree-7-3);
          \draw[->] (tree-7-3)--(tree-6-4);
          \draw[->] (tree-6-4)--(tree-7-5);
          \draw[->] (tree-7-3)--(tree-8-4);
          \draw[->] (tree-8-4)--(tree-7-5);
          \draw[->] (tree-8-4)--(tree-9-5);
    \end{tikzpicture}
\end{document}

相关内容