5

I would like to draw the following open ended lists one after another. On the second list, drawing an arrow from each item to a struct. This could basically viewed as a hash table.

             |     |   />   
             |_____|  /
|     |      | 300 | /
|_____|      |_____| 
| 100 |      | 100 | -----> 
|_____|      |_____|

Example sketch:

![enter image description here


What I was able to come up with:

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,arrows.meta,arrows}

\tikzset{ mymat/.style={ matrix of math nodes, text height=2.5ex, text depth=0.75ex, text width=3.25ex, align=center, row sep=-\pgflinewidth }, } \begin{document}

\begin{tikzpicture}[>=latex] \matrix[mymat,anchor=west,style={nodes=draw}] at (0,0) (mat1) { ...\ ...\ ...\ ...\ ...\ 69\ 98\ }; \matrix[mymat,right=of mat1,anchor=north,style={nodes={draw}},yshift=1.0cm] (mat2) { 7\ 7\ }; \matrix[mymat,right=of mat1,anchor=north,style={nodes={draw}},yshift=-1.0cm] (mat5) { 1\ 2\ }; \path[->] (mat1-6-1.center) edge[] node [left] {} (mat2-1-1.south west); \path[->] (mat1-7-1.center) edge[] node [left] {} (mat5-1-1.south west);

\end{tikzpicture}

\end{document}

enter image description here

alper
  • 1,389
  • 1
    https://tex.stackexchange.com/q/228835/134144 seems to be related. – leandriis Mar 06 '21 at 07:19
  • I have to find a way to draw the lists vertical from the related example – alper Mar 06 '21 at 11:25
  • 1
    https://tex.stackexchange.com/a/476490/134144 could serve as a point to start from – leandriis Mar 06 '21 at 11:47
  • Definitely matrices of nodes. Follow the link provided by @leandriis and come back with what you started with. – SebGlav Mar 06 '21 at 13:29
  • @SebGlav Thank you I will update the question with improvent using !leandriis's guide – alper Mar 06 '21 at 13:36
  • @SebGlav Please see my updated question. I was not able to put labels next to each box like 0, 1, 2 , A , B , and arrowing is not successful like I wanted on my drawn figure. And I was not able to draw the list on the left side as open ended – alper Apr 05 '21 at 19:09
  • @alper So you want something like your example sketch or the picture above it? – Excelsior Apr 06 '21 at 20:27
  • @Excelsior picture above it, but flexible if I use greater length texts inside the boxes – alper Apr 08 '21 at 00:54

1 Answers1

4

You already did almost everything on your own, congratulations. Here's what you can add to complete your picture.

EDIT

I edited my post (and the picture) to fix some issues in the creation of the matrices (now everything is drawn on grid) and to add the open ended that you wanted in the first place.

list and arrows

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,positioning,arrows}

\tikzset{ mymat/.style={ matrix of math nodes, minimum width=1cm, minimum height=1cm, text height=2.5ex, text depth=0.75ex, text width=3.25ex, align=center, row sep=-\pgflinewidth, outer sep=0pt, inner sep=0pt }, } \begin{document}

\begin{tikzpicture}[>=latex,on grid]
    \matrix[mymat,anchor=south west,style={nodes=draw}]
    at (1,0)
    (mat1)
    {
    \dots\\
    \dots\\
    \dots\\
    69\\
    98\\
    };
    \matrix[mymat,right=30mm of mat1-5-1.south,anchor=south,style={nodes={draw}}]
    (mat2)
    {
    7\\
    7\\
    };
    \matrix[mymat,above=30 mm of mat2,style={nodes={draw}}]
    (mat5)
    {
    1\\
    2\\
    };

    %%%%%% Modified part

    % Draw the open end
    \draw (mat1-1-1.north west) --++ (0,1);
    \draw (mat1-1-1.north east) --++ (0,1);

    % Draw the arrows
    \path[->]
      (mat1-5-1.east) edge[] node [left] {} (mat2-2-1.west);
    \path[->]
      (mat1-4-1.east) edge[] node [left] {} (mat5-2-1.west);

    % Number the cells
    \foreach \i [count=\l from 0] in {5,...,1} \node[left= 8mm of mat1-\i-1] {\l};
    \foreach \l [count=\i from 1] in {A,B}
        {
        \node[right=8 mm of mat2-\i-1] {\l};
        \node[right=8 mm of mat5-\i-1] {\l};
        }
\end{tikzpicture}

\end{document}

EDIT 2

Now to the colours and the text widths, you can add some filling into your matrices declaration and you can change locally the text width to adapt it to your content.

list v2

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,positioning,arrows}

\tikzset{ mymat/.style={ matrix of math nodes, minimum width=1cm, minimum height=1cm, text height=2.5ex, text depth=0.75ex, text width=3.25ex, align=center, row sep=-\pgflinewidth, outer sep=0pt, inner sep=0pt }, } \begin{document}

\begin{tikzpicture}[>=latex,on grid]
    \matrix[mymat,fill=olive!50,anchor=south west,style={nodes=draw}]
    at (1,0)
    (mat1)
    {
    \dots\\
    \dots\\
    \dots\\
    69\\
    98\\
    };
    \matrix[mymat,text width=15mm,fill=cyan,right=30mm of mat1-5-1.south,anchor=south,style={nodes={draw}}]
    (mat2)
    {
    7584\\
    7968\\
    };
    \matrix[mymat,fill=pink,above=30 mm of mat2,style={nodes={draw}}]
    (mat5)
    {
    1\\
    2\\
    };

    %%%%%% Modified part

    % Draw the open end
    \draw (mat1-1-1.north west) --++ (0,1);
    \draw (mat1-1-1.north east) --++ (0,1);

    % Draw the arrows
    \path[->]
      (mat1-5-1.east) edge[] node [left] {} (mat2-2-1.west);
    \path[->]
      (mat1-4-1.east) edge[] node [left] {} (mat5-2-1.west);

    % Number the cells
    \foreach \i [count=\l from 0] in {5,...,1} \node[left= 8mm of mat1-\i-1] {\l};
    \foreach \l [count=\i from 1] in {A,B}
        {
        \node[right=1 mm of mat2-\i-1.east] {\l};
        \node[right=1 mm of mat5-\i-1.east] {\l};
        }
\end{tikzpicture}

\end{document}

SebGlav
  • 19,186
  • @alper I edited my answer to add the open ended feature you asked for, and I also did some minor improvements in the matrices construction. – SebGlav Apr 07 '21 at 10:02
  • Is it also possible to color background of arrays on the right as well? For example from changing their backgrounds from white to light gray, if possible – alper Apr 07 '21 at 22:32
  • I am trying to combine your answer with following solution (https://tex.stackexchange.com/questions/591410/how-to-scale-and-align-the-very-basic-people-using-tikz-on-top-of-a-time-line), where your answer's output will be bottom of the timeline. The other solution is not defined using \begin{tikzpicture}[>=latex,on grid] rather its \begin{tikzpicture}. Could there be anyway to combine both under \begin{tikzpicture} ... \end{tikzpicture} – alper Apr 08 '21 at 00:22
  • If there is longer text in the arrays on the right side, lets say 700000 instead of 7, can the lists' width could be expanded little bit ? or longer text AAAAAAA rather than A – alper Apr 08 '21 at 00:36
  • I edited my post to add some colours and text width changes. If you want to insert it into a bigger project, I suggest you post a new question which we will happy to help you with. – SebGlav Apr 08 '21 at 18:22
  • Can I fill it with colors that I can define through hex ? or light-gray like at this picture (https://en.wikipedia.org/wiki/Merge_algorithm#/media/File:Merge_sort_algorithm_diagram.svg) // is it easy to convert arrays on right hand side as side buy side (horizontal)? // and thanks for your edit – alper Apr 08 '21 at 20:23
  • You can do almost everything you want, you just have to try ;) – SebGlav Apr 08 '21 at 20:41
  • That’s power of coding, but its difficult to do aligning without a GUI , it requires a lot a trial and error :) – alper Apr 08 '21 at 20:49
  • Please ask a new question with your picture, you'll have answers. Have a look at split rectangle with horizontal option from the shapes.multipart TikZ library. We should not discuss that into the comment section. – SebGlav Apr 08 '21 at 20:53
  • @alper : If you want to draw the merge sort alg in the picture you provided, I found a simple way to do it. I'd be glad to answer if you ask ;) – SebGlav Apr 09 '21 at 09:16
  • Thanks. Please see https://tex.stackexchange.com/questions/592155/how-to-draw-a-merge-sort-algorithm-figure . I also asked a sub-question in it , if its not related I can as in another question. I was just trying to apply the labeling on top of nodes part on this question, where I was trying to draw array on the right-had side vertical and write A an B on top of those linking them via a line. – alper Apr 09 '21 at 16:30
  • Can I do vertical array doing 1 & 2 & \dots \ ? where middle lines become bolder now not sure why – alper Apr 09 '21 at 19:54