我想要绘制如下图形:
但是垂直分割的矩形大小不一致(水平分割的矩形大小正常),如何保证宽度和高度大小一致?
梅威瑟:
\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}
答案1
只是为了好玩,画这种结构的另一种方法
\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}
答案2
此重现显示图像:
\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}
编辑: 节点部分的高度适合使用的字体大小(10pt)。