是否可以绘制 draw direct address tables
? 原始示例可从Cormen's Introduction to Algorithms, Second Edition
书页 223 图 11.1:
我心中的想法是下图,其中有附加步骤作为指针:
我当前的代码:
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}[font=\large,thick,>=stealth]
\foreach \j in {0,1,4,6,7}{
\draw (1,-\j)--+(-1,-1);
}
\draw (0,0) grid (1,-8);
\foreach \i in {0,...,6}{
\path (-0.5,-\i-0.5) node{$ \i $};
}
\foreach \i/\j in {2/5, 3/9, 5/5, 6/5}{
\draw[->] (0.5,-\i-0.5)--+(1.5,0)
node[shift={(0.5,0)}]{$ \j $};
\draw (2,-\i) grid +(2,-1);
}
\foreach \i/\j in {2.5/5, 5.5/8}{
\draw[->] (3.5,-\i)--+(1.5,0) node[shift={(0.5,0)}]{$ \j $};
}
\draw (5,-2) grid +(3,-1) (5,-5) grid +(3,-1);
\end{tikzpicture}
\end{document}
感谢您宝贵的时间和帮助。
答案1
就 for 循环而言,它可能不是最佳的,但我认为它能满足你的要求
\documentclass[tikz,border=2mm]{standalone}
\newcommand{\myGrid}[3]{
\begin{scope}[shift={(#1, #2)}]
\foreach \i in {0,...,#3}{
\draw (\i,0) rectangle (1, -1);
}
\end{scope}
}
\begin{document}
\begin{tikzpicture}[font=\large,thick,>=stealth]
%%%
% T
%%%
\foreach \j in {0,1,4,6,7}{
\draw (1,-\j)--+(-1,-1);
}
\draw (0,0) grid (1,-8);
\foreach \i in {0,...,6}{
\path (-0.5,-\i-0.5) node{$ \i $};
}
%%%
% First level
%%%
\foreach \i/\j in {2/2, 3.1/3, 5/5}{
\draw[->] (0.8,-\i-0.5)--+(1.2,0) % Arrows
node[shift={(0.5,0)}]{$ \j $}; % Values
\myGrid{2}{-\i}{2};
}
\node at (2.5, -6.5) {$ 5 $};
\myGrid{2}{-6}{2}
% Labels
\node (l1) at (1.8, -1.3) {Key};
\draw (l1)--(2.5, -2.0);
\node[text width=2cm,text centered] (l2) at (3.7, -1.3) {Satellite key};
\draw (l2)--(3.5, -2.0);
%%%
% Second level
%%%
% Values
\foreach \i/\j in {2.5/5, 3.6/7, 5.5/8, 6.5/9}{
\node at (3.5, -\i) {$\j$};
}
% Arrows
\foreach \i/\j in {2.5/5, 3.6/7, 5.5/8}{
\draw[->] (3.8,-\i)--+(1.2,0);
}
\draw[->] (3.8, -6.5) to[out=0, in=180] (5, -8);
% Values and grids
\foreach \i/\j/\k in {2.5/5/0, 3.6/7/0, 5.5/8/0, 6.5/8/1, 8/9/0}{
\node at (5.5, -\i) {$\j$};
\node at (6.5, -\i) {$\k$};
\myGrid{5}{-\i+0.5}{3}
}
% Labels
\node[text width=1cm,text centered] (l2) at (5.5, -1.3) {\footnotesize{Satellite key}};
\draw (l2)--(5.5, -2.0);
\node[text width=1cm,text centered] (l2) at (6.5, -1.3) {\footnotesize{Index}};
\draw (l2)--(6.5, -2.0);
\node[text width=1cm,text centered] (l2) at (7.5, -1.3) {\footnotesize{job data}};
\draw (l2)--(7.5, -2.0);
\end{tikzpicture}
\end{document}