我想要画如下图所示的树:
我已经写了:
\documentclass{article}
\usepackage{tikz}
\usepackage{color}
\definecolor{red}{rgb}{1,0,0}
\begin{document}
\begin{tikzpicture}
\tikzstyle{level 1}=[sibling distance=50mm]
\tikzstyle{level 2}=[sibling distance=30mm]
\node(a) {\textcolor{red}{2}|2}
child {node(b) {\textcolor{red}{2}|4}
child {node(f) {\textcolor{red}{2}|4}}
child {node(g)[rectangle, draw] {
\scalebox{0.6}{% size of the array
\begin{tabular}{c|c|c|c|c|c}
\textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4
\end{tabular}
}
}}
child {node(d) {\textcolor{red}{5} | 2}}
}
child {node(c)[rectangle, draw] {
\scalebox{0.8}{% size of the array
\begin{tabular}{c|c|c|c|c|c}
\textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4
\end{tabular}
}
}}
child {node(e) {\textcolor{red}{7} | 2}
child {node(f) {\textcolor{red}{2}|4}}
child {node(g)[rectangle, draw] {
\scalebox{0.6}{%
\begin{tabular}{c|c|c|c|c|c}
\textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4 & \textcolor{red}{2}|4
\end{tabular}
}
}}
child {node(d) {\textcolor{red}{5} | 3}}
}
;
\draw[dashed] (a) -- (b);
\end{tikzpicture}
\end{document}
我得到这个结果:
我怎样才能继续绘制父级数组子级中的多个边到另一个子级,提前谢谢您
答案1
概括
- 修复了节点名称。为了方便引用,我从上到下、从左到右将节点命名为
a,b,...
。 - 根据建议 奎伯比尔贝尔,我使用了
rectangle split
库中可用的节点形状shapes.multipart
而不是tabular
环境中的节点形状。 to path
我没有定义新的,而是使用了库brace
中的装饰decorations.pathreplacing
并设置amplitude=0pt
。有点 hacky,但确实有效。- 我删除了它
\scalebox
并用 替换了它scale=0.8
。 - 更新
我将每个正如 Qrrbrbirbel 在评论中提到的,要绘制管道:|
都括起来$...$
。|
,请\usepackage[T1]{fontenc}
在序言中使用:如何在 (La)TeX 中插入管道符号?。为了保险起见,也写一下\usepackage[utf8]{inputenc}
。 - 我没有根据
rectangle split
节点形状中的默认坐标来定义坐标,而是相对于节点的两个坐标来定义坐标。 - 我将
\tikzstyle
已弃用的 替换为\tikzset
。 - 无需定义
red
为颜色,因为这已经是预定义的。此外,无需加载color
包,因为在内部tikz
使用xcolor
。
更新的代码
\documentclass[tikz,border=5]{standalone}
%\documentclass[10pt]{article}
%\usepackage[margin=1in]{geometry}
%\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usetikzlibrary{shapes.multipart,calc,decorations.pathreplacing}
\tikzset{
my shape 1/.style={circle, draw},
my shape 2/.style={
rectangle split,
rectangle split parts=#1,
draw,
anchor = center,
rectangle split horizontal},
}
\begin{document}
\begin{tikzpicture}
\tikzset{level 1/.style={sibling distance=50mm},
level 2/.style={sibling distance=30mm}}
\node (a) [my shape 1] {\textcolor{red}{2}|2}
child {node(b) [my shape 1] {\textcolor{red}{2}|4}
child {node(e) [my shape 1] {\textcolor{red}{2}|4}}
child {node(f)[my shape 2=6,scale=0.8] {
\textcolor{red}{2}|4 \nodepart{two} \textcolor{red}{2}|4 \nodepart{three} \textcolor{red}{2}|4 \nodepart{four} \textcolor{red}{2}|4 \nodepart{five} \textcolor{red}{2}|4 \nodepart{six} \textcolor{red}{2}|4
}}
child {node(g) [my shape 1] {\textcolor{red}{5}|2}}
}
child {node(c)[my shape 2=6,scale=0.8] {
\textcolor{red}{2}|4 \nodepart{two} \textcolor{red}{2}|4 \nodepart{three} \textcolor{red}{2}|4 \nodepart{four} \textcolor{red}{2}|4 \nodepart{five} \textcolor{red}{2}|4 \nodepart{six} \textcolor{red}{2}|4
}}
child {node(d) [my shape 1] {\textcolor{red}{7}|2}
child {node(h) [my shape 1] {\textcolor{red}{2}|4}}
child {node(i)[my shape 2=6,scale=0.8] {
\textcolor{red}{2}|4 \nodepart{two} \textcolor{red}{2}|4 \nodepart{three} \textcolor{red}{2}|4 \nodepart{four} \textcolor{red}{2}|4 \nodepart{five} \textcolor{red}{2}|4 \nodepart{six} \textcolor{red}{2}|4
}}
child {node(j) [my shape 1] {\textcolor{red}{5}$|$3}}
};
\foreach \xleft/\leftpos in {1/0.416,3/0.2083,5/.375,7/.542,9/.7083,11/.875} {
\coordinate (c\xleft) at ($(c.south west)!\leftpos!(c.south east)$);
\coordinate (f\xleft) at ($(f.north west)!\leftpos!(f.north east)$);}
\foreach \x/\arrowcol in {1/blue,3/green,5/red,7/yellow,9/orange,11/purple}
\draw [->,\arrowcol] (c\x) .. controls ++(-90:0.5) and ++(0:0.5) .. ($(c\x)!.5!(f\x)$)
($(c\x)!.5!(f\x)$) .. controls ++(180:0.5) and ++(90:0.5) .. (f\x);
\foreach \xright/\rightpos in {2/.125,4/.2917,6/.4583,8/.625,10/.7917,12/.9583} {
\coordinate (c\xright) at ($(c.south west)!\rightpos!(c.south east)$);
\coordinate (i\xright) at ($(i.north west)!\rightpos!(i.north east)$);}
\foreach \x/\arrowcol in {2/blue,4/green,6/red,8/yellow,10/orange,12/purple}
\draw [->,\arrowcol,dashed] (c\x) .. controls ++(-90:0.5) and ++(0:0.5) .. ($(c\x)!.5!(i\x)$)
($(c\x)!.5!(i\x)$) .. controls ++(180:0.5) and ++(90:0.5) .. (i\x);
\draw [postaction={draw,decorate,decoration={brace,raise=-3pt,amplitude=0pt},dashed,red}] (a) -- (b);
\end{tikzpicture}
\coordinate (a*) at (c9);
\coordinate (b*) at ([yshift=-0.2cm]a*);
\draw (a*) -- (b*);
\draw ([xshift=-0.3cm]b*) -- ([xshift=0.3cm]b*);
\foreach \x in {-0.8,-0.4,0,.4,0.8}
\draw ([xshift=\x em]b*) -- +(-0.1,-0.2);
\end{document}