-1

I want to draw a hash table like in the picture with tikz. Note that the numbering on the right is important. The symbol "/" means that there is no key number on this position. enter image description here

Thanks!

UPDATE:

This answer https://tex.stackexchange.com/a/484083/260640 is almost what I want but it has no numbering on the right and the number of positions is fixed. I want to make hash tables with different number of positions.

User12345
  • 33
  • 5

2 Answers2

3
\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}

enter image description here

3

A solution with matrix of nodes and absolute dimensions.

\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}

enter image description here

polyn
  • 5,614