我正在尝试使用包绘制二叉树tikz
。
如果仅使用circle
,我会得到一棵树。但我想用 来表示subtree
。triangle
现在树变得参差不齐。我想将箭头附加到 的顶部triangle
,还希望 的大小triangle
与 的大小相当circle
。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{shapes.geometric,arrows,fit,matrix,positioning}
\tikzset
{
treenode/.style = {circle, draw=black, align=center, minimum size=1cm},
subtree/.style = {isosceles triangle, draw=black, align=center, minimum height=0.5cm, minimum width=1cm, shape border rotate=90, anchor=north}
}
\begin{document}
\begin{figure}[t]
\centering
\caption{An illustration of a simple delete operation. \label{fig:simple}}
{
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
level distance = 1.5cm},scale=0.6, transform shape]
\node [treenode] {$X$ \\ 100}
child
{
node [treenode] {$Y$ \\ 50}
child
{
node [treenode] {$Z$ \\ 40}
child
{
node [treenode] {$S1$ \\ 30}
}
child
{
node [treenode] {$S2$ \\ 60}
}
}
child
{
node [subtree] {$Z$ \\ 200}
}
}
child
{
node [subtree] {$Z$ \\ 200}
}
;
\end{tikzpicture}
}
\end{figure}
\end{document}
答案1
在 OP 的当前设置下,只要文本未写入节点,圆圈和三角形的大小就相当
subtree
。因此,一个可能的解决方案是在绘制完成后再写入文本。最后两行代码显示了这一点。为了使箭头指向三角形的尖端,将以下行添加到相应的父节点
来自父路径的边 ={(\tikzparentnode.-50) -- (\tikzchildnode.north)} % -50 是可调整的。
如果三角形仍然无法比较,则可以写入
scale=0.9
样式subtree
以进行收缩调整。
代码
\documentclass{article}
\usepackage[paper size={10cm,8cm}]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{shapes.geometric,arrows,fit,matrix,positioning}
\tikzset
{
treenode/.style = {circle, draw=black, align=center, minimum size=1cm},
subtree/.style = {isosceles triangle, draw=black, align=center, minimum height=0.5cm, minimum width=1cm, shape border rotate=90, anchor=north}
}
\begin{document}
\begin{figure}[t]
\centering
\caption{An illustration of a simple delete operation. \label{fig:simple}}
{
\begin{tikzpicture}[->,>=stealth', level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}, scale=0.6,transform shape]
\node [treenode] {$X$ \\ 100}
child
{
node [treenode] {$Y$ \\ 50}
child
{
node [treenode] {$Z$ \\ 40}
child
{
node [treenode] {$S1$ \\ 30}
}
child
{
node [treenode] {$S2$ \\ 60}
}
}
child[edge from parent path ={(\tikzparentnode.-50) -- (\tikzchildnode.north)}]
{
node [subtree,yshift=0.4cm] (a) {} % delay the text till later
}
}
child[edge from parent path ={(\tikzparentnode.-30) -- (\tikzchildnode.north)}]
{
node [subtree,yshift=0.4cm] (b) {} % delay the text till later
}
;
% ------------------------------------------------ put the text into subtree nodes
\node[align=center,yshift=0.1cm] at (a) {$Z$\\200};
\node[align=center,yshift=0.1cm] at (b) {$Z$\\200};
\end{tikzpicture}
}
\end{figure}
答案2
对于节点,您需要了解的第一件事TiKZ
就是minimum size
它的确切含义minimum
,即,如果node contents
需要更大的尺寸,TiKZ
就会采用它。
这里有一个带有treenode
和的示例subtree
(我已将其替换isosceles triangle
为带有的等边线regular polygon
)。如您所见,如果节点内容为空,minimum size
则受尊重,并且圆和三角形大小之间存在关系。但是,只要节点包含一些文本,它们的大小就会相应地进行调整。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes.geometric,arrows,fit,matrix,positioning}
\tikzset
{
treenode/.style = {circle, draw=black, align=center, minimum size=1cm, anchor=center},
subtree/.style = {regular polygon, regular polygon sides=3, draw=black, align=center, minimum size=1cm, anchor=center}
}
\begin{document}
\begin{tikzpicture}
\node[treenode,blue] at (0,0) {};
\node[subtree,red] at (0,0) {};
\node[treenode] at (2,0) {$Y$ \\ 50};
\node[treenode, blue] at (2,0) {};
\node[subtree, inner sep=0pt] at (4,0) {$Y$ \\ 50};
\node[subtree, red, inner sep=0pt] at (4,0) {};
\node[treenode, blue] at (4,0) {};
\end{tikzpicture}
\end{document}
如果您希望圆形和三角形节点呈现相似的大小,则必须使用它们的最小尺寸。
代码的第二个问题是节点定义anchor=north
中的选项,而具有默认值。然后,子树的最顶端顶点与相应树节点的中心点对齐。subtree
treenodes
anchor=center
您的最后一个问题是关于指向北顶点的箭头。使用 [child anchor=north]。
结果可能是:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes.geometric,arrows,fit,matrix,positioning}
\tikzset
{
treenode/.style = {circle, draw=black, align=center,
minimum size=1cm, anchor=center},
subtree/.style = {regular polygon, regular polygon sides=3,
draw=black, align=center,
minimum size=1cm, anchor=center}
}
\begin{document}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
level distance = 1.5cm},scale=0.6, transform shape]
\node [treenode] {$X$ \\ 100}
child
{
node [treenode] {$Y$ \\ 50}
child
{
node [treenode] {$Z$ \\ 40}
child
{
node [treenode] {$S1$ \\ 30}
}
child
{
node [treenode] {$S2$ \\ 60}
}
}
child [child anchor=north]
{
node [subtree] {$Z$ \\ 200}
}
}
child [child anchor=north]
{
node [subtree] {$Z$ \\ 200}
}
;
\end{tikzpicture}
\end{document}
答案3
要将箭头附加到子树的顶部,请说child[child anchor=apex]