TikZ 箭头用于显示排序算法

TikZ 箭头用于显示排序算法

我想从下面的代码中添加一些像下图这样的箭头。

预期输出

在此处输入图片描述

代码

\documentclass{article}
    \usepackage{tikz}
    \usepackage{xstring}

    \tikzset{
        raw sort entry/.style={rectangle, thick, draw, node distance=1.5em},
        sort entry black/.style={raw sort entry, black, fill=white},
        sort entry blackgray/.style={raw sort entry, black, fill=gray!25},
        s1/.style={raw sort entry, red, fill=yellow!30},
        s2/.style={raw sort entry, blue, fill=green!20},
        s3/.style={raw sort entry, violet, fill=orange!25}
    }

    \newcommand*{\List}[2][sort entry black]{%
      \par\noindent%
      \edef\listtoprocess{#2}%
      \def\ListToProcess{}%
      \begin{tikzpicture}[inner sep=2pt, outer sep=0]
        \foreach \content in \listtoprocess{
          \IfSubStr{\content}{/}{% true
            \xdef\ListToProcess{\ListToProcess,\content}
          }{%                      false
            \xdef\ListToProcess{\ListToProcess,#1/\content}
          }
        }
        \StrGobbleLeft{\ListToProcess}{1}[\ListToProcess]% removes the first comma (\listToProcess is empty at the start)
        \foreach [count=\i] \Style/\Value in \ListToProcess {
          \ifnum\i=1\relax
            \node [raw sort entry, \Style] (sortnode\i) {\Value};
          \else
            \node [raw sort entry, right of=sortnode\number\numexpr\i-1\relax, \Style] (sortnode\i) {\Value};
          \fi
        }
      \end{tikzpicture}%
    }

\begin{document}

\List[sort entry blackgray]{1, 2, 3, s1/1, s1/2, s1/3, 1, 2, 3}

\bigskip

\List[sort entry blackgray]{1, 2, 3, s1/1, s1/2, s1/3, 1, 2, 3}

\end{document}

答案1

添加

  • remember picturetikzpicture环境
  • 两种内部样式/键:

    • /qrr/default
    • /qrr/name row

  • 两种外部风格:
    • default
    • name row

name row最初是空白的,因此节点被命名为-1-2、...。

默认的默认样式是sort entry black(如前面在可选\List参数中所述)。另请参阅代码中的注释。

代码

\documentclass{article}
    \usepackage{tikz}
    \usepackage{xstring}
    \usetikzlibrary{calc}
    \tikzset{
        raw sort entry/.style={rectangle, thick, draw, node distance=1.5em},
        sort entry black/.style={raw sort entry, black, fill=white},
        sort entry blackgray/.style={raw sort entry, black, fill=gray!25},
        s1/.style={raw sort entry, red, fill=yellow!30},
        s2/.style={raw sort entry, blue, fill=green!20},
        s3/.style={raw sort entry, violet, fill=orange!25},
        /qrr/default/.style=sort entry black,
        name row/.initial={},
        default/.style={/qrr/default/.style={#1}},
        |-|/.style={
          to path={
            let \p1=(\tikztostart),
                \p2=(\tikztotarget) in
                -- (\x1,.5*\y1+.5*\y2) -- (\x2,.5*\y1+.5*\y2) \tikztonodes
                                                                       -- (\tikztotarget)
          }
        }
    }
    % you can leave the default value for the optional argument blank
    % as the value of "/qrr/default" is taken anyway
    % (but you could make different \List macros
    % with different default default values).
    \newcommand*{\List}[2][default=sort entry black]{%
      \tikzset{#1}%
      \par\noindent%
      \edef\listtoprocess{#2}%
      \pgfkeysgetvalue{name row}{\rowname}
      \def\ListToProcess{}%
      \begin{tikzpicture}[remember picture,inner sep=2pt, outer sep=0]
        \foreach \content in \listtoprocess{
          \IfSubStr{\content}{/}{% true
            \xdef\ListToProcess{\ListToProcess,\content}
          }{%                      false
            \xdef\ListToProcess{\ListToProcess,{/qrr/default}/\content}
          }
        }
        \StrGobbleLeft{\ListToProcess}{1}[\ListToProcess]% removes the first comma (\listToProcess is empty at the start)
        \foreach [count=\i] \Style/\Value in \ListToProcess {
          \ifnum\i=1\relax
            \node [raw sort entry, \Style] (\rowname-\i) {\Value};
          \else
            \node [raw sort entry, right of=\rowname-\number\numexpr\i-1\relax, \Style] (\rowname-\i) {\Value};
          \fi
        }
      \end{tikzpicture}%
    }

\begin{document}

\List[default=sort entry blackgray, name row=a]{1, 2, 3, s1/1, s1/2, s1/3, 1, 2, 3}

\bigskip

\List[default=sort entry blackgray, name row=b]{1, 2, 3, s1/1, s1/2, s1/3, 1, 2, 3}

\tikz[overlay,remember picture] \draw[thick, shorten >=\pgflinewidth,->] (a-7.south) to[|-|] (b-9.north);
\end{document}

输出

在此处输入图片描述

相关内容