我正在尝试绘制以下合并排序算法图片。请注意,答案如何绘制一个列表,其项目指向一个结构?对于起点很有帮助。
以下部分与主要问题相关,即如何在每个数组节点顶部添加标题,关于该问题主要部分的简单示例将会很有帮助:
答案1
这里的tree
选项很有用。为了让一切都对齐,你需要画两棵树,一棵向下生长,一棵向上生长。我使用了库rectangle split
中的选项shapes.multipart
,但你也可以使用节点矩阵来实现。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.multipart}
\tikzset{block/.style={
font=\sffamily,
draw=black,
thin,
fill=pink!50,
rectangle split,
rectangle split horizontal,
rectangle split parts=#1,
outer sep=0pt},
%
gblock/.style={
block,
rectangle split parts=#1,
fill=green!30}
}
\begin{document}
\def\lvld{1.2} % Choose level distance
\pgfmathsetmacro\shft{-6*\lvld} % Calculate the yshift for the green tree
\begin{tikzpicture}[level distance=\lvld cm,
level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2cm},
level 3/.style={sibling distance=1cm},
edgedown/.style={edge from parent/.style={draw=red,thick,-latex}},
edgeup/.style={edge from parent/.style={draw=green!50!black,thick,latex-}}
]
% GREEN TREE (drawn first to let the middle line filled in pink)
\node[gblock=7,yshift=\shft cm] (A') {3 \nodepart{two} 9 \nodepart{three} 10 \nodepart{four} 27 \nodepart{five} 39 \nodepart{six}43 \nodepart{seven}82}
[grow=up,edgeup]
child {node[gblock=3] (B2') {9 \nodepart{two} 10 \nodepart{three} 82}
child {node[gblock=1] (C4') {10}
child {node[gblock=1] (D7') {10}}
}
child {node[gblock=2] (C2') {9 \nodepart{two} 82}
child {node[gblock=1] (D3') {82}}
child {node[gblock=1] (D4') {9}}
}
}
child {node[gblock=4] (B1') {3 \nodepart{two} 27 \nodepart{three} 39 \nodepart{four} 43}
child {node[gblock=2] (C3') {3 \nodepart{two} 43}
child {node[gblock=1] (D5') {3}}
child {node[gblock=1] (D6') {43}}
}
child {node[gblock=2] (C1') {27 \nodepart{two} 39}
child {node[gblock=1] (D1') {27}}
child {node[gblock=1] (D2') {39}}
}
};
% PINK TREE
\node[block=7] (A) {39 \nodepart{two} 27 \nodepart{three} 43 \nodepart{four} 3 \nodepart{five} 9 \nodepart{six}82 \nodepart{seven}10}
[grow=down,edgedown]
child {node[block=4] (B1) {39 \nodepart{two} 27 \nodepart{three} 43 \nodepart{four} 3}
child {node[block=2] (C1) {39 \nodepart{two} 27}
child {node[block=1] (D1) {39}}
child {node[block=1] (D2) {27}}
}
child {node[block=2] (C2) {43 \nodepart{two} 3}
child {node[block=1] (D3) {43}}
child {node[block=1] (D4) {3}}
}
}
child {node[block=3] (B2) {9 \nodepart{two} 82 \nodepart{three} 10}
child {node[block=2] (C3) {9 \nodepart{two} 82}
child {node[block=1] (D5) {9}}
child {node[block=1] (D6) {82}}
}
child {node[block=1] (C4) {10}
child {node[block=1] (D7) {10}}
}
};
\end{tikzpicture}
\end{document}
您还可以看到每个数组都是一个特定节点,因此有自己的名称。现在您可以在 Ti 中执行任何您想做的事情钾通过指向或来自这些节点,围绕这些节点进行 Z 操作。