How to draw direct-address tables

diagramsliststikz-pgf

Is it possible to draw draw direct address tables? Original examples could be seen from Cormen's Introduction to Algorithms, Second Edition book page 223 Figure 11.1:

enter image description here

What's in my mind is following figure with additional step for pointers:

enter image description here


My current code:

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

Thank you for your valuable time and help.

Best Answer

It's maybe not optimal in term of for loops, but I think it does what you asked

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

Hash table

Related Question