我有一棵分支树,目前只显示节点。我想给它添加更多标签,但我不知道该怎么做。我附上了目前的代码,以及一张我想要的图片。我该怎么做?
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\tikzstyle{every node}=[circle,inner sep=1.5pt,draw,fill]
\draw node {} child {node {}
child {
node {}
child { node {} child { node {} }child {node {}} child {node {} }}
}
child { node {} }}
;
\end{tikzpicture}
\caption{ Branched tree}
\end{center}
\end{figure}
答案1
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}%
[tn/.style={circle,inner sep=1.5pt,draw,fill}% tree node
]
\draw
node[tn] (1) {}
child { node[tn] (11) {}
child { node[tn] (111) {}
child { node[tn] (1111) {}
child { node[tn] (11111) {} }
child { node[tn] (11112) {} }
child { node[tn] (11113) {} }
}
}
child { node[tn] (112) {}}
};
\node[left =of 11111] (t4) {$t=4$};
\node at (1 -| t4) {$t=0$};
\node at (11 -| t4) {$t=1$};
\node at (111 -| t4) {$t=2$};
\node at (1111 -| t4) {$t=3$};
\node[right=of 11113] (R4) {$R_4=3$};
\node at (1 -| R4) {$R_0=1$};
\node at (11 -| R4) {$R_1=1$};
\node at (111 -| R4) {$R_2=2$};
\node at (1111 -| R4) {$R_3=1$};
\end{tikzpicture}
\end{document}
答案2
如果有人必须绘制许多这样的树,这里有一个森林风格,nodes alone
,它会自动格式化和标记树。显然,标签中的内容细节可以根据需要进行定制。我假设 MWE 中的数字代表级别数(t=0, t=1, ...
和R_0=??, R_1=??, ...
)以及该级别的节点数(1, 1, 2, 1, 3
)。根据需要进行修改。
我添加了第二个更大的例子只是为了说明其潜力:一旦定义了样式,树木本身就可以非常简洁地指定。
例如,原始树可以用
\begin{forest}
nodes alone
[, baseline
[
[
[
[][][]
]
]
[]
]
]
\end{forest}
以及更大的演示树
\begin{forest}
nodes alone
[, baseline
[
[
[
[][][]
]
]
[[[[[[[[][[[[[]]][]]][[][]]][][][[]]]]][[[][]]]][[]]][[][[][]]]]
]
[
[[[]][][[[]]]]
]
]
\end{forest}
完整代码:
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
nodes alone/.style={
for tree={
parent anchor=center,
child anchor=center,
anchor=center,
inner sep=1.5pt,
circle,
fill,
s sep'+=10pt,
},
before typesetting nodes={
tempcounta/.max={>O{level}}{r,tree},
for nodewalk={
root,
tikz+={
\coordinate (w) at (current bounding box.west);
\coordinate (e) at (current bounding box.east);
},
until={>OR={level}{tempcounta}}{next node},
Nodewalk={}{current and ancestors}{
tempcountc/.option=level,
tempcountd'=0,
Nodewalk={}{filter={fake=root,tree}{>OR={level}{tempcountc}}}{tempcountd'+=1},
tikz+/.process={
ORw2{level}{tempcountd}{
\node [anchor=east, xshift=-10mm] at (w |- .center) {$t=##1$};
\node [anchor=west, xshift=10mm] at (e |- .center) {$R_{##1}=##2$};
}
},
}
}{},
},
},
}
\begin{document}
\begin{forest}
nodes alone
[, baseline
[
[
[
[][][]
]
]
[]
]
]
\end{forest}
\begin{forest}
nodes alone
[, baseline
[
[
[
[][][]
]
]
[[[[[[[[][[[[[]]][]]][[][]]][][][[]]]]][[[][]]]][[]]][[][[][]]]]
]
[
[[[]][][[[]]]]
]
]
\end{forest}
\end{document}