我一直在尝试以如下所示的子图格式创建决策树,但无论我尝试哪种间距,总会在某个点或另一个点出现重叠节点。在下面的示例中,我有同一棵树的两个副本作为示例。如何使用 tikz 包修复此问题?此外,对于使用 tikz 包绘制稍大的树,有什么建议吗?这非常令人沮丧,因为我觉得我正处于随机更改级别/兄弟距离以尝试让事情正常运转的阶段……
\documentclass{book}
\usepackage{tikz}
\usepackage{amsmath,amssymb,amstext,amsthm}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[h]{0.475\textwidth}
\centering
\begin{tikzpicture}[scale=0.6, level distance=25mm,
level 1/.style={sibling distance=80mm},
level 2/.style={sibling distance=50mm},
level 3/.style={sibling distance=30mm}]
\node[circle, draw]{$x\smash{_1}$}
child{node[circle, draw]{$x\smash{_2}$}
child{node[circle, draw]{$x\smash{_4}$}
child{node[rectangle, draw]{$x\smash{_1} \gets 1$}}
child{node[rectangle, draw]{$x\smash{_2} \gets 1$}}
}
child{node[circle, draw]{$x\smash{_4}$}
child{node[rectangle, draw]{$x\smash{_1} \gets 1$}}
child{node[rectangle, draw]{$x\smash{_4} \gets 0$}}
}
}
child{node[circle, draw]{$x\smash{_3}$}
child{node[circle, draw]{$x\smash{_2}$}
child{node[rectangle, draw]{$\begin{aligned} x\smash{_1} &\gets 0 \\ x\smash{_3} &\gets 1 \end{aligned}$}}
child{node[rectangle, draw]{$\begin{aligned} x\smash{_1} &\gets 0 \\ x\smash{_2} &\gets 0 \end{aligned}$}}
}
child{node[circle, draw]{$x\smash{_4}$}
child{node[rectangle, draw]{$x\smash{_4} \gets 1$}}
child{node[rectangle, draw]{$x\smash{_3} \gets 0$}}
}
}
;
\end{tikzpicture}
\caption{A Tree}
\end{subfigure}
\begin{subfigure}[h]{0.475\textwidth}
\centering
\begin{tikzpicture}[scale=0.6, level distance=25mm,
level 1/.style={sibling distance=80mm},
level 2/.style={sibling distance=50mm},
level 3/.style={sibling distance=30mm}]
\node[circle, draw]{$x\smash{_1}$}
child{node[circle, draw]{$x\smash{_2}$}
child{node[circle, draw]{$x\smash{_4}$}
child{node[rectangle, draw]{$x\smash{_1} \gets 1$}}
child{node[rectangle, draw]{$x\smash{_2} \gets 1$}}
}
child{node[circle, draw]{$x\smash{_4}$}
child{node[rectangle, draw]{$x\smash{_1} \gets 1$}}
child{node[rectangle, draw]{$x\smash{_4} \gets 0$}}
}
}
child{node[circle, draw]{$x\smash{_3}$}
child{node[circle, draw]{$x\smash{_2}$}
child{node[rectangle, draw]{$\begin{aligned} x\smash{_1} &\gets 0 \\ x\smash{_3} &\gets 1 \end{aligned}$}}
child{node[rectangle, draw]{$\begin{aligned} x\smash{_1} &\gets 0 \\ x\smash{_2} &\gets 0 \end{aligned}$}}
}
child{node[circle, draw]{$x\smash{_4}$}
child{node[rectangle, draw]{$x\smash{_4} \gets 1$}}
child{node[rectangle, draw]{$x\smash{_3} \gets 0$}}
}
}
;
\end{tikzpicture}
\caption{B Tree}
\end{subfigure}
\caption{Caption}
\label{datftt}
\end{figure}
\end{document}
答案1
最终,树的最小宽度取决于叶节点(末端)内的文本。对于这两棵树,您可以看到,将所有端到端的x_n <- 1
结果放置在一起已经占用了文本宽度的一半以上。如果您想强制这些树并排放置,则要么有重叠的树(如您目前所做的那样),要么有重叠的节点。这两种选择都不优雅……
可以通过为较低级别设置较小的字体大小来稍微调整此最小宽度,以便它们并排放置时占用更少的空间。这可以通过 来实现level n/.style={font=\footnotesize}
。
您还提到,您必须不断尝试调整兄弟距离。原因是 Ti 中的标准树构造算法钾Z 的预见能力很差。它看到下一级有n
节点,并根据兄弟距离放置它们,但它不知道这些节点下面是否还有更多节点,从而使得节点的有效“宽度”更大。
可以使用graph drawing
这个问题可以通过使用Ti 中的库钾Z.它提供了更多的算法来自动放置节点,这使得绘图图表
简单得多。我提供了一个示例来展示如何使用图形绘制库:
\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{
graphs,
graphdrawing,
}
\usegdlibrary{trees}
\usepackage{amsmath,amssymb,amstext,amsthm}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{\linewidth}
\centering
\tikz \graph [
tree layout,
nodes={
draw,
circle,
},
level 3/.style={
font=\small,
},
level 4/.style={
nodes={
rectangle,
font=\footnotesize,
}
}
] {
"\(x_{}\)"
-> {
"\(x_{1}\)"
-> {
"\(x_{11}\)"
-> {
"\(x_{111}\)",
"\(x_{112}\)"
},
"\(x_{12}\)"
-> {
"\(x_{121}\)",
"\(x_{122}\)"
}
},
"\(x_{2}\)"
-> {
"\(x_{21}\)"
-> {
"\(x_{211}\)",
"\(x_{212}\)"
},
"\(x_{22}\)"
-> {
"\(x_{221}\)",
"\(x_{222}\)"
}
}
}
};
\caption{Tree \(x\)}
\end{subfigure}
\begin{subfigure}[b]{\linewidth}
\centering
\tikz \graph [
tree layout,
nodes={
draw,
circle,
},
level 3/.style={
font=\small,
},
level 4/.style={
nodes={
rectangle,
font=\footnotesize,
}
}
] {
"\(y_{}\)"
-> {
"\(y_{1}\)"
-> {
"\(y_{11}\)"
-> {
"\(y_{111}\)",
"\(y_{112}\)"
},
"\(y_{12}\)"
-> {
"\(y_{121}\)",
"\(y_{122}\)"
}
},
"\(y_{2}\)"
-> {
"\(y_{21}\)"
-> {
"\(y_{211}\)",
"\(y_{212}\)"
},
"\(y_{22}\)"
-> {
"\(y_{221}\)",
"\(y_{222}\)"
}
}
}
};
\caption{Tree \(y\)}
\end{subfigure}
\caption{My awesome captions for trees!}
\label{fig:trees}
\end{figure}
\end{document}
输出: