我有一個分支樹,目前僅顯示節點。我想給它多加一些標籤,但我不知道該怎麼做。我已經包含了到目前為止的程式碼,以及我希望它看起來像什麼的圖片。我該怎麼做?
\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}