我想用 tikz 画一个像图中这样的哈希表。注意右边的编号很重要。符号“/”表示这个位置没有键号。
谢谢!
更新:
这个答案https://tex.stackexchange.com/a/484083/260640几乎就是我想要的,但是右边没有编号,位置数是固定的。我想制作具有不同位置数的哈希表。
答案1
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}[font=\large,thick,>=stealth]
\foreach \j in {0,2,4,6,7}{
\draw (1,-\j)--+(-1,-1);
}
\draw (0,0) grid (1,-8);
\foreach \i in {0,...,7}{
\path (-0.5,-\i-0.5) node{$ \i $};
}
\foreach \i/\j in {1/5,3/9,5/1}{
\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 {1.5/32,5.5/12}{
\draw[->] (3.5,-\i)--+(1.5,0) node[shift={(0.5,0)}]{$ \j $};
}
\draw[->] (6.5,-5.5)--+(1.5,0) node[shift={(0.5,0)}]{$ 4 $};
\draw (5,-1) grid +(2,-1) (5,-5) grid +(2,-1) (8,-5) grid +(2,-1);
\end{tikzpicture}
\end{document}
答案2
具有节点矩阵和绝对维度的解决方案。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\pgfmathsetlengthmacro{\nodx}{2em} % width of node
\pgfmathsetlengthmacro{\nody}{2em} % height of node
\tikzset{mynod/.style={
matrix of nodes,
ampersand replacement=\&,
nodes in empty cells,
column sep=0pt,
row sep=0pt,
nodes={
minimum width=\nodx,
minimum height=\nody,
anchor=center,
inner sep=0pt,
outer sep=0pt
}
}}
\begin{document}
\begin{tikzpicture}[>=stealth]
\matrix[mynod](Z){
0 \&\&\&\&\&\&\&\&\&\& \\
1 \&\&\& 5 \&\&\& 32 \&\&\&\& \\
2 \&\&\&\&\&\&\&\&\&\& \\
3 \&\&\& 9 \&\&\&\&\&\&\& \\
4 \&\&\&\&\&\&\&\&\&\& \\
5 \&\&\& 1 \&\&\& 12 \&\&\& 4 \& \\
6 \&\&\&\&\&\&\&\&\&\& \\
7 \&\&\&\&\&\&\&\&\&\& \\
};
\foreach \x in {1,...,8}
\draw (Z-\x-2.north west) rectangle (Z-\x-2.south east);
\foreach \x in {2,4,6} {
\draw (Z-\x-4.north west) rectangle (Z-\x-4.south east);
\draw (Z-\x-5.north west) rectangle (Z-\x-5.south east);}
\foreach \x in {2,6} {
\draw (Z-\x-7.north west) rectangle (Z-\x-7.south east);
\draw (Z-\x-8.north west) rectangle (Z-\x-8.south east);}
\draw (Z-6-10.north west) rectangle (Z-6-10.south east);
\draw (Z-6-11.north west) rectangle (Z-6-11.south east);
\foreach \x in {1,3,5,7,8}
\draw (Z-\x-2.north east) -- (Z-\x-2.south west);
\foreach \x in {2,4,6}
\draw[->, shorten >= 2pt] (Z-\x-2.center) -- (Z-\x-4);
\foreach \x in {2,6}
\draw[->, shorten >= 2pt] (Z-\x-5.center) -- (Z-\x-7);
\draw[->, shorten >= 2pt] (Z-6-8.center) -- (Z-6-10);
\end{tikzpicture}
\end{document}