请问我如何使用 TikZ 绘制这棵树?
我的问题是如何连接两个子节点并创建一个带有标签的新节点?
当前代码是
\documentclass[a4paper,openany,11pt,oneside]{book}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[grow=right]
\tikzstyle{every node}=[draw,shape=circle];
\node{r}
child { node {a}
child { node {c} }
child { node {d} }
child { node {e} }
}
child [missing]
child [missing]
child { node {b}
child { node {c} }
child { node {d} }
child { node {e} }
};
\end{tikzpicture}
\end{document}
答案1
您可以通过为树的根节点命名来为树命名,然后树的每个节点也会收到一个自动生成的名称,形式(root-n-m)
为 ,即为root
根节点的名称、n
第一级子节点的序数和m
第二级子节点的序数。此名称定义了通过树到达任何所需节点的“路径”。
使用这些名称,您可以添加 tikz 命令来在树的外部绘制到其他节点的线条。
例如:
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees, positioning}
\begin{document}
\begin{tikzpicture}[grow=right]
\tikzset{
every node/.style = {draw,shape=circle},
every label/.style = {shape=rectangle, draw=none}
}
\node (t) {r}
child { node {a}
child { node {c} }
child { node {d} }
child { node {e} }
child { node {f} }
}
child [missing]
child [missing]
child { node {b}
child { node {c} }
child { node {d} }
child { node {e} }
};
\node[rectangle, draw, below right=5mm and 3cm of t-4-3, label={$p=0.3$}] (S1) {S11, s22};
\node[rectangle, draw, below=1.8cm of S1 , label={$p=0.5$}] (S2) {S11, s24};
\node[rectangle, draw, below=of S2, label={$p=0.12$}] (S3) {S12, s23};
\node[rectangle, draw, below=1.8cm of S3, label={$p=0.6$}] (S4) {S13, s22};
\foreach \start/\finish in {t-4-3/S1,t-4-3/S2,t-4-2/S3,t-4-1/S4,t-1-4/S1,t-1-3/S4,t-1-2/S3,t-1-1/S2}
\draw[blue, ->] (\start) -- (\finish.west);
\end{tikzpicture}
\end{document}
结果是:
答案2
因为这不是一棵树,并且为了获得最佳结果,节点必须在视觉上定位,所以我建议明确绘制节点和边。
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\colorlet{nblue}{blue!50}
\colorlet{nred}{brown!50!red}
\colorlet{norange}{orange!50}
\colorlet{ngreen}{green!50}
\usetikzlibrary{shapes,positioning}
\tikzset
{common/.style={blue,draw,fill=#1,text=white},
single/.style={common=#1,ellipse,inner sep=1pt},
multiple/.style 2 args=
{common=#1,rounded corners,label=above:{\scriptsize$P=#2$},
minimum width=4em,inner sep=2pt
}
}
\begin{document}
\begin{tikzpicture}
\node[single=nblue,inner sep=3pt] (s) {$s$};
\node[single=nblue,above right=of s,yshift=5mm] (s1) {$s_1$};
\node[single=nblue,below right=of s,yshift=-5mm] (s2) {$s_2$};
\node[single=nred,above right=of s1,yshift=-5mm] (s11) {$s_{11}$};
\node[single=ngreen,right=of s1] (s12) {$s_{12}$};
\node[single=norange,below right=of s1,yshift=5mm] (s13) {$s_{13}$};
\node[single=nred,above right=of s2] (s21) {$s_{21}$};
\node[single=norange,right=of s2,yshift=5mm] (s22) {$s_{22}$};
\node[single=ngreen,right=of s2,yshift=-5mm] (s23) {$s_{23}$};
\node[single=nred,below right=of s2] (s24) {$s_{24}$};
\node[multiple={nred}{0.2},right=of s12,xshift=1cm,yshift=2mm] (s11s22) {$s_{11},s_{22}$};
\node[multiple={nred}{0.5},below=of s11s22] (s11s24) {$s_{11},s_{24}$};
\node[multiple={ngreen}{0.12},below=of s11s24] (s12s23) {$s_{12},s_{23}$};
\node[multiple={norange}{0.6},below=of s12s23] (s13s22) {$s_{13},s_{22}$};
\node[multiple={ngreen}{0.12},right=of s11s22,yshift=-7mm] (s12s23b) {$s_{12},s_{23}$};
\node[multiple={nred}{0.3},below=of s12s23b] (s11s22b) {$s_{11},s_{22}$};
\node[multiple={norange}{0.6},below=of s11s22b] (s13s22b) {$s_{13},s_{22}$};
\foreach \f/\t in
{s/s1,s/s2,s1/s11,s1/s12,s1/s13,s2/s21,s2/s22,s2/s23,s2/s24,%
s11/s11s22,s11/s11s24,s12/s12s23,s13/s13s22,s21/s11s22,s22/s13s22,%
s22/s13s22,s23/s12s23,s24/s11s24,s11s22/s11s22b,s12s23/s12s23b,s13s22/s13s22b%
}
\draw[blue,-latex] (\f.east) -- (\t.west);
\end{tikzpicture}
\end{document}