![Stern-Brocot 树,每个节点都有两个父亲](https://linux22.com/image/433048/Stern-Brocot%20%E6%A0%91%EF%BC%8C%E6%AF%8F%E4%B8%AA%E8%8A%82%E7%82%B9%E9%83%BD%E6%9C%89%E4%B8%A4%E4%B8%AA%E7%88%B6%E4%BA%B2.png)
我尝试了森林包,但我只能绘制根为 1/1 的黑色边缘的树。
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
[$\frac{1}{1}$
[$\frac{1}{2}$
[$\frac{1}{3}$
[$\frac{1}{4}$
[ $\frac{1}{5}$ ]
[$\frac{2}{7}$] ]
[$\frac{2}{5}$
[$\frac{3}{8}$ ]
[$\frac{3}{7}$ ] ] ]
[$\frac{2}{3}$
[$\frac{3}{5}$
[$\frac{4}{7}$ ]
[$\frac{5}{8}$ ] ]
[$\frac{3}{4}$
[$\frac{5}{7}$ ]
[$\frac{4}{5}$ ] ] ] ]
[$\frac{2}{1}$
[$\frac{3}{2}$
[$\frac{4}{3}$
[$\frac{5}{4}$]
[$\frac{7}{5}$ ] ]
[$\frac{5}{3}$
[$\frac{8}{5}$ ]
[$\frac{7}{4}$ ] ] ]
[$\frac{3}{1}$
[$\frac{5}{2}$
[$\frac{7}{3}$ ]
[$\frac{8}{3}$ ] ]
[$\frac{4}{1}$
[$\frac{7}{2}$ ]
[$\frac{5}{1}$ ] ] ] ] ] ]
\end{forest}
\end{document}
我也想画出灰色边缘和两个“根”0/1 和 1/0。
答案1
这是一个蛮力解决方案。应该可以完全自动化,即让森林做所有事情,但我真的不知道 Stern-Brocot 图的规则是什么。
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={alias/.wrap pgfmath arg={a-#1}{id},
%label/.wrap pgfmath arg={[gray,font=\tiny,inner sep=0pt]above:#1}{id}
}
[$\frac{1}{1}$
[$\frac{1}{2}$
[$\frac{1}{3}$
[$\frac{1}{4}$
[$\frac{1}{5}$]
[$\frac{2}{7}$] ]
[$\frac{2}{5}$
[$\frac{3}{8}$]
[$\frac{3}{7}$] ] ]
[$\frac{2}{3}$
[$\frac{3}{5}$
[$\frac{4}{7}$]
[$\frac{5}{8}$] ]
[$\frac{3}{4}$
[$\frac{5}{7}$]
[$\frac{4}{5}$] ] ] ]
[$\frac{2}{1}$
[$\frac{3}{2}$
[$\frac{4}{3}$
[$\frac{5}{4}$]
[$\frac{7}{5}$] ]
[$\frac{5}{3}$
[$\frac{8}{5}$]
[$\frac{7}{4}$] ] ]
[$\frac{3}{1}$
[$\frac{5}{2}$
[$\frac{7}{3}$]
[$\frac{8}{3}$] ]
[$\frac{4}{1}$
[$\frac{7}{2}$]
[$\frac{5}{1}$] ] ] ] ] ]
\path (current bounding box.north west) node[below right] (tl) {$\frac{0}{1}$}
(tl) foreach \x in {2,...,6} {edge (a-\x)}
(current bounding box.north east) node[below right] (tr) {$\frac{1}{1}$}
(tr) foreach \x in {2,18,26,30,32} {edge (a-\x)}
(a-2) foreach \x in {11,15,17,19,20,21} {edge (a-\x)}
(a-3) foreach \x in {8,10,12,13} {edge (a-\x)}
(a-4) foreach \x in {7,9} {edge (a-\x)}
(a-11) foreach \x in {14,16} {edge (a-\x)}
(a-18) foreach \x in {23,25,27,28} {edge (a-\x)}
(a-19) foreach \x in {22,24} {edge (a-\x)}
(a-26) foreach \x in {29,31} {edge (a-\x)};
\end{forest}
\end{document}
如果你取消注释
%label/.wrap pgfmath arg={[gray,font=\tiny,inner sep=0pt]above:#1}{id}
你会看到所有神奇数字的来源:
人们可以对此进行优化(显而易见的选项包括math content
),但考虑到该图背后肯定存在逻辑,寻求部分改进似乎有点没有动力。