答案1
一种可能性是使用chains
和positioning
库。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{chains,positioning} %
\begin{document}
\begin{tikzpicture}[start chain=1 going right,every node/.style={draw,minimum
width=0.6cm}]
\begin{scope}[node distance=-\pgflinewidth,thick]
\foreach \X [count=\Y] in {0,...,15}
{\node [on chain=1] (X\X) {\X};}
\end{scope}
\begin{scope}[node distance=2mm]
\node[below=of X1] (Y23){23};
\node[below=of Y23] (Y57){57};
\draw (X1) -- (Y23) -- (Y57);
\node[below=of X4] (Y4){4};
\draw (X4) -- (Y4);
\node[below=of X7] (Y12){12};
\node[below=of Y12] (Y31){31};
\node[below=of Y31] (Y34){34};
\draw (X7) -- (Y12) -- (Y31) -- (Y34);
\node[below=of X10] (Y8){8};
\draw (X10) -- (Y8);
\node[below=of X14] (Y15){15};
\node[below=of Y15] (Y21){21};
\draw (X14) -- (Y15) -- (Y21);
\node[left=of X0,draw=none] {$h(x)$};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
这本质上marmot 的回答但使用\chainin
命令从现有节点启动链可以使代码更简洁:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}
\scoped[start chain=going right, node distance=-0.1ex]{
\node[on chain] {$h(x)$};
\foreach \i in {0,...,15}
\node [on chain, line width=0.2ex, minimum width=5ex, draw] (n-\i) {\i};
}
\foreach \i/\J in {1/{23,57},4/{4},7/{12,31,34},8/{204},10/{8},14/{15,29}}{
\scoped[start chain=going below, node distance=0.75ex]{
\chainin (n-\i);
\foreach \j in \J
\node [on chain, join, minimum width=3ex, draw] {\j};
}}
\end{tikzpicture}
\end{document}