我问自己是否可以用 tikz 创建像图片中的点:
代码
\begin{tikzpicture}
\tikzstyle{level 1}=[sibling distance=60mm]
\tikzstyle{level 2}=[sibling distance=33mm]
\tikzstyle{level 3}=[sibling distance=10mm]
\tikzstyle{level 4}=[sibling distance=5mm]
\node {n}
child {node {$\frac{n}{3}$}
child {node {$\frac{n}{9}$}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}}
child {node {$\frac{n}{9}$}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}}}
child {node {$\frac{n}{3}$}
child {node {$\frac{n}{9}$}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}}
child {node {$\frac{n}{9}$}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}}}
child {node {$\frac{n}{3}$}
child{node{$\frac{n}{9}$}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}}
child{node{$\frac{n}{9}$}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}
child{node {$\frac{n}{27}$}}}
};
我想在最后一个子节点和子节点之间开始点线。
答案1
以下是使用 Tikz 的解决方案。代码主要基于这是递归树的示例,但也进行了大量修改以适合您的示例图像。不确定您是否希望将 设为cn/n
实际分数,但从下面的代码开始可以轻松解决这个问题。
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees,calc,arrows.meta,positioning,decorations.pathreplacing,bending}
\tikzset{
edge from parent/.style={draw, thick, blue!70!black},
no edge from this parent/.style={
every child/.append style={
edge from parent/.style={draw=none}}},
level 3/.style={yshift=5cm},
level 4/.style={level distance=5mm}
}
\begin{document}
\begin{tikzpicture}[
level/.style={sibling distance=40mm/#1},
text=blue!70!black,
>=latex,
font=\sffamily
]
\node (z){cn}
child {node (a) {cn/2}
child {node (b) {cn/4}
child {node (b1) {$\vdots$}[no edge from this parent]
child {node (b11) {c}}
}
child {node (b2) {$\vdots$}[no edge from this parent]
child {node (b12) {c}}
}
}
child {node (g) {cn/4}
child {node (g1) {$\vdots$}[no edge from this parent]
child {node (g11) {c}}
}
child {node (g2) {$\vdots$}[no edge from this parent]
child {node (g12) {c}}
}
}
}
child {node (d) {cn/2}
child {node (e) {cn/4}
child {node (e1) {$\vdots$}[no edge from this parent]
child {node (e11) {c}}
}
child {node (e2) {$\vdots$}[no edge from this parent]
child {node (e12) {c}}
}
}
child {node (f) {cn/4}
child {node (f1) {$\vdots$}[no edge from this parent]
child {node (f11) {c}}
}
child {node (f2) {$\vdots$}[no edge from this parent]
child {node (f12) {c}
}
}
}
};
\node[left=5 of z] (ln1) {cn}[no edge from this parent]
child {node (ln2) {cn}[no edge from this parent]
child {node (ln3) {cn}[no edge from this parent]
child {node (ln4) {}[no edge from this parent]
child {node (ln5) {cn}}}}};
\path (b12.north east) -- (g11.north west) node [midway] {$\cdots$};
\path (e12.north east) -- (f11.north west) node [midway] {$\cdots$};
\coordinate (cd1) at ($(f12)+(1,0)$);
\coordinate (nb1) at ($(g12)!.5!(e11)$);
\draw[blue!70!black,thick,<->,]
(cd1) -- (cd1|-z.east) node [near start, fill=white] {log(n)};
\draw[blue!70!black,dashed,thick,->]
($(z.west)+(-1em,0)$) -- (ln1);
\draw[blue!70!black,dashed,thick,->]
($(a.west)+(-1em,0)$) -- (ln2.east);
\draw[blue!70!black,dashed,thick,->]
($(b.west)+(-1em,0)$) -- (ln3);
\draw[blue!70!black,dashed,thick,->]
($(b11.west)+(-1em,0)$) -- (ln5);
\draw[blue!70!black,thick,decorate,decoration={brace,amplitude=10pt,mirror},->,-{latex[flex=1pt]}] (b11.south west) -- (f12.south east);
\end{tikzpicture}
\end{document}
答案2
这是一个使用的解决方案forest
。请注意,这会获取目标图像的“类似图像”极其认真的说。这意味着主树的实际规范非常非常紧凑:
[c, name=node 1
[, name=node 2 [, name=node 3 [][]][[][]]][[[][]][[][]]]
]
显然,如果您想要一些不太通用的东西,您可以手动指定节点的内容。
还请注意,我实际上没有理由以我的方式指定节点 4-13 的标签。特别是,指定编号为 10-13 的节点实际上只是因为我想对节点遍历进行一些实验。
\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest,textcomp}
\usetikzlibrary{positioning,arrows.meta,decorations.pathreplacing}
\begin{document}
\begin{forest}
for tree={
font=\sffamily,
before typesetting nodes={
if={level>0}{
if n children=0{
append/.wrap pgfmath arg={
[#1, font=\sffamily, edge=dotted]
}{content("!r")},
content={},
}{
content/.wrap 2 pgfmath args={#2n\textfractionsolidus#1}{int(level()*2)}{content("!r")}
}
}{
delay n=1{
content/.wrap value={##1n},
node walk={
every step/.style={alias=node 4}, F,
every step/.style={alias=node 10}, N,
every step/.style={alias=node 11}, N,
every step/.style={alias=node 9},
node walk={
rL,
node walk={
every step/.style={alias=node 12}, P,
every step/.style={alias=node 13}, P}
}
}
}
}
},
}
[c, name=node 1
[, name=node 2 [, name=node 3 [][]][[][]]][[[][]][[][]]]
]
\begin{scope}[font=\sffamily, >=Triangle]
\node (node 5) [left=25pt of node 4] {cn};
\foreach \i/\j in {6/3,7/2,8/1} \node (node \i) at (node 5 |- node \j) {cn};
\foreach \i/\j in {1/8,2/7,3/6,4/5} \draw [densely dashed, ->] (node \i.west) -- (node \j.east);
\path [draw, decorate, decoration={brace, amplitude=10pt}] (node 9.south east) -- (node 4.south west);
\path [draw, <->] (node 9.south east) +(15pt,0) coordinate (c) -- (c |- node 1.north) node [pos=.4, fill=white] {log(n)};
\end{scope}
\draw [dotted] (node 10.north east) -- (node 11.north west) (node 13.north east) -- (node 12.north west);
\end{forest}
\end{document}