2

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.

alper
  • 1,389
  • This could be a start: https://tex.stackexchange.com/questions/628692/drawing-a-hash-table-with-tikz – Juan Castaño Jan 09 '22 at 20:41
  • I was able to come up with the most part where I was not able to remove arrow on the 6th index, give a space in between pointed boxes from 2th and 3rd indexes and add label on top of each table for key etc. – alper Jan 09 '22 at 21:07

1 Answers1

2

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

Phantom
  • 628
  • Which solution should I come up in order to improve workout for for loops? Woult it be also possible to label table like key label , and move arrow from center of the box to little bit right thanks – alper Jan 12 '22 at 17:32
  • I edited the answer. To improve for loops you should add condition to do not draw arrows where you don't want them. You'll be able to put the last {value,gird} of the first level in the loop, and for second level you'll be able to merge the 2 first loops {values, arrows}. If you find a way to use 4 variables for the loops (i, j, k, l) you'll even be able to merge the 4 loops! – Phantom Jan 13 '22 at 20:18
  • Thank you I appreciate your edit. Just a small question: would it be possible to add a small space in between 2. and 3rd grid where there will be a space in between table [2,5] and [3,7]. I have tried adding [yshift=-0.5ex] but it kind of messed up arrows – alper Jan 13 '22 at 20:40
  • 1
    I edited the answer. Now I'll let you work :) – Phantom Jan 13 '22 at 21:47
  • I am sorry for very late comment. It is possible to expand the size of the most right grid under job data , expand its size in x-axis ? – alper May 09 '22 at 18:20