4

I want to draw a graphic like this one:

But the vertical rectangle split does not have the same size (the horizontal one works OK). How to ensure the width and height with the same size?

MWE:

\documentclass[border=10pt]{standalone}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                positioning,
                shapes.multipart}

\newlength\MinimumWidth
\setlength{\MinimumWidth}{\widthof{00}}
\tikzset{
arr/.style={{Circle[length=2pt 4, sep=0pt -4]}-Stealth},
arity/.style={
        rectangle split,
        rectangle split parts=#1+1,
        rectangle split horizontal,
        rectangle split empty part width=6mm,
        minimum height=6mm,
        text width=\MinimumWidth, align=center,% <--- NOTE
        draw,
        on chain},
arityv/.style={
        rectangle split,
        rectangle split parts=#1+1,
        rectangle split every empty part={},
        rectangle split empty part height=height("I"),
        minimum height=6mm,
        text height=height("I"),
        text width=\MinimumWidth, align=center,% <--- NOTE
        draw,
        on chain},
box/.style = {draw, text width=#1, minimum size=#1, inner sep=0pt},
box/.default = 4mm
        }
        
\begin{document}
    \begin{tikzpicture}[
node distance = 8mm and 7mm,
  start chain = on chain,
                        ]
\node[arity=0, box, label=head] (a0) {};
\foreach \labela/\val [count=\i from 0, count=\j] in {7/,7/,88/,12/,13/,0/-1}%
{
\ifnum \i=1
    \node[arity=1,red] (a\j) {\labela\nodepart{two}\val};
    \draw[arr] (a\i.two north |- a\i.east) -- (a\j);
\else
    \node[arity=1,blue] (a\j) {\labela\nodepart{two}\val};
    \draw[arr] (a\i.two north |- a\i.east) -- (a\j);
\fi
}

\node[arity=0, box, label=head,
      below=of a0] (b0) {};
\foreach \labela/\val [count=\i from 0, count=\j] in {17/,27/,1/,2/,3/,0/-1}%
{
\ifnum \i=1
    \node[arityv=1,red] (b\j) {\labela\nodepart{two}\val};
    \draw[arr] (b\i.two north |- b\i.two east) -- (b\j.two);
\else
    \node[arityv=1,blue] (b\j) {\labela\nodepart{two}\val};
    \draw[arr] (b\i.two north |- b\i.two east) -- (b\j.two);
\fi
}
\end{tikzpicture}
\end{document}

2 Answers 2

5

Just for fun, an alternative way of drawing such a structure

\documentclass[tikz, border=2mm]{standalone}

\usetikzlibrary{positioning, matrix, arrows.meta}

\begin{document}

\begin{tikzpicture}[
arr/.style={{Circle[length=2pt 4, sep=0pt -4]}-Stealth},
mycell/.style={minimum size=7mm, anchor=center, draw}]
]
\matrix (A) [matrix of nodes, nodes in empty cells, nodes=mycell, column sep=5mm, row sep=-\pgflinewidth] {%
17 & 27 & 1 & 2 & 3 & 0\\
& & & & & -1\\
& & & & & \\
};

\foreach \i [evaluate=\i as \ni using {int(\i+1)}] in {1,...,5}
    \draw[arr] (A-2-\i.center)--(A-2-\ni);

\foreach \i [evaluate=\i as \ni using {int(\i-1)}] in {6,...,2}
    \draw[arr] (A-3-\i.center)--(A-3-\ni);
    
\node[draw, left=8mm of A-2-1.west, label=head] (head){};
\draw[arr] (head.center)--(A-2-1);
\end{tikzpicture}
\end{document}

enter image description here

1
  • Nice solution, it is more expandable, Thanks!
    – xaero
    Commented Mar 24, 2022 at 1:09
3

This reproduce showed image:

\documentclass[border=10pt]{standalone}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                positioning,
                shapes.multipart}

\newlength\MinimumWidth
\setlength{\MinimumWidth}{\widthof{00}}
\tikzset{
arr/.style={{Circle[length=2pt 4, sep=0pt -4]}-Stealth},
arityv/.style={
        rectangle split,
        rectangle split parts=#1+1,
        rectangle split empty part height=10pt,
        execute at end node=\vphantom{fg},
        text width=\MinimumWidth, align=center,
        draw,
        on chain},
box/.style = {draw, text width=#1, minimum size=#1, inner sep=0pt},
box/.default = 4mm
        }

\begin{document}
    \begin{tikzpicture}[
node distance = 8mm and 7mm,
  start chain = on chain,
                        ]
\node[arityv=0, box, label=head] (b0) {};
\foreach \labela/\val [count=\i from 0, count=\j] in {17/,27/,1/,2/,3/,0/-1}%
{
    \node[arityv=2] (b\j) {\labela\nodepart{two}\val};
    \draw[arr] (b\i.north |- b\i.east) -- (b\j.west);
\ifnum \i>0
    \draw[arr] (b\j.north |- b\j.three west) -- (b\i.three east);
\fi
}
    \end{tikzpicture}
\end{document}

enter image description here

Edit: Nodes part heights are accommodated to used font size (10pt).

2
  • I've accept your answer in the previous question :-D, Thanks!
    – xaero
    Commented Mar 24, 2022 at 7:53
  • Another question: How to modify the node distance between b2 and b3? (and others distance remain unchanged)
    – xaero
    Commented Mar 24, 2022 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .