如何用 tikz 重现这幅合成图像?

如何用 tikz 重现这幅合成图像?

我无法使用 tikz 重现以下图像:

在此处输入图片描述

我可以画出每棵树,但我不知道如何将它们组合起来以获得上面显示的图像。

你要如何排列它们?如何添加箭头和文字?

先感谢您。

下面是我为图像前两行的树编写的代码。

PS:森林是执行此操作的正确包吗?

\documentclass[tikz,border=10pt]{standalone}
\usepackage[linguistics]{forest}
\begin{document}
\begin{forest}
  [
    4 
    [3
        [2]
        [1]
    ]
    [5
        [6]
    ]
  ]
\end{forest}

\begin{forest}
    [
    4 
        [3
            [2]
            [1]
        ]
        [5
            [6]
        ]
    ]
\end{forest}

\begin{forest}
    [
    5 
        [3
            [2]
            [1]
        ]
        [4
            [6]
        ]
    ]
\end{forest}

\begin{forest}
    [
    1 
        [3
            [2]
            [5]
        ]
        [4
            [6]
        ]
    ]
\end{forest}

\begin{forest}
    [
    1 
        [3
            [2]
            [5]
        ]
        [4
            [6]
        ]
    ]
\end{forest}

\begin{forest}
    [
    4 
        [3
            [2]
            [5]
        ]
        [1
            [6]
        ]
    ]
\end{forest}
\end{document}

答案1

使用 Tizpic定义和matrix库。仅显示两行。

对于白色圆圈,给出数字 1、2、3、...

对于灰色圆圈,给出数字 11,22,33,...

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{%
pics/circs/.style args={#1-#2-#3-#4-#5-#6}{%
    code={%
    \pgfmathparse{#1}
    \ifnum\pgfmathresult<10
    \node at (0,0) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=white](#1){#1}; 
    \else
    \pgfmathtruncatemacro{\a}{round(#1/10)}
    \node at (0,0) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=gray!30](#1){\a};
    \fi
    \pgfmathparse{#2}
    \ifnum\pgfmathresult<10
    \node at (-1,-1) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=white](#2){#2};
    \else
    \pgfmathtruncatemacro{\a}{round(#2/10)}
    \node at (-1,-1) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=gray!30](#2){\a};
    \fi
    \pgfmathparse{#3}
    \ifnum\pgfmathresult<10
    \node at (1,-1) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=white](#3){#3};
    \else
    \pgfmathtruncatemacro{\a}{round(#3/10)}
    \node at (1,-1) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=gray!30](#3){\a};
    \fi
    \pgfmathparse{#4}
    \ifnum\pgfmathresult<10
    \node at (-1.5,-1.5) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=white](#4){#4};
    \else
    \pgfmathtruncatemacro{\a}{round(#4/10)}
    \node at (-1.5,-1.5) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=gray!30](#4){\a};
    \fi
    \pgfmathparse{#5}
    \ifnum\pgfmathresult<10  
    \node at (-0.5,-1.5) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=white](#5){#5};  
    \else
    \pgfmathtruncatemacro{\a}{round(#5/10-1)}
    \node at (-0.5,-1.5) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=gray!30](#5){\a}; 
    \fi
    \pgfmathparse{#6}
    \ifnum\pgfmathresult<10   
    \node at (0.5,-1.5) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=white](#6){#6};
    \else
    \pgfmathtruncatemacro{\a}{round(#6/10-1)}
    \node at (0.5,-1.5) [draw,circle,minimum size=0.5cm,inner sep=1pt,fill=gray!30](#6){\a};
    \fi
    \draw (#1.220)--(#2.50);
    \draw (#1.-40)--(#3.130);
    \draw (#2.220)--(#4.50);
    \draw (#2.-40)--(#5.130);
    \draw (#3.220)--(#6.50);  
    }
    }
    }

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,column  sep=5mm,row  sep=3mm, ] {%First Row
   \node[left=3mm]{Exchange $(A[1],\,A[6])$};   
   \pic {circs={4-3-5-2-1-6}};
   \draw[->]([xshift=5mm] 5.0)--++(0:1.5);& 
   \node[left=10mm]{\textit{Heapsize:=Heapsize-1}};   
   \pic {circs={4-3-5-2-1-66}};
   \draw[->]([xshift=5mm] 5.0)--++(0:1.5);&
   \node[left=3mm]{Heapify (A,1)};       
   \pic {circs={5-3-4-2-1-66}}; \\
 %Second Row  
   \node[left=3mm]{Exchange $(A[1],\,A[5])$};   
   \pic {circs={1-3-4-2-5-66}};
   \draw[->]([xshift=5mm] 4.0)--++(0:1.5);& 
   \node[left=10mm]{\textit{Heapsize:=Heapsize-1}};   
   \pic {circs={1-3-4-2-55-66}};
   \draw[->]([xshift=5mm] 4.0)--++(0:1.5);&
   \node[left=3mm]{Heapify (A,1)};       
   \pic {circs={4-3-1-2-55-66}}; \\
   };

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

你可以使用以下技巧这个答案:a\subnode似乎不会干扰魔法forest,所以您可以使用它来标记节点,然后可以在中使用overlay picture

\documentclass{article}
\usepackage[linguistics]{forest}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{tabular}{cp{1cm}cp{1cm}c}
\begin{forest}
  [
    4 
    [3
        [2]
        [1]
    ]
    [\subnode{pft1}{5}
        [6]
    ]
  ]
\end{forest}
& &
\begin{forest}
    [
    4 
        [\subnode{pft2}{3}
            [2]
            [1]
        ]
        [5
            [6]
        ]
    ]
\end{forest}
& &
\begin{forest}
    [
    5 
        [3
            [2]
            [1]
        ]
        [4
            [6]
        ]
    ]
\end{forest}
\\
\begin{forest}
    [
    1 
        [3
            [2]
            [5]
        ]
        [4
            [6]
        ]
    ]
\end{forest}
& &
\begin{forest}
    [
    1 
        [3
            [2]
            [5]
        ]
        [4
            [6]
        ]
    ]
\end{forest}
& &
\begin{forest}
    [
    4 
        [3
            [2]
            [5]
        ]
        [1
            [6]
        ]
    ]
\end{forest}
\\
\end{tabular}%
\begin{tikzpicture}[overlay,remember picture]
\draw[thick,-stealth] (pft1) -- (pft2) node[midway,above]{Heap size\dots};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容