我需要绘制一棵二叉树(实际上是 Stern-Brocot 树),其中两个伪节点作为根的父节点,如第一张图所示。
目前,我正在使用 tikz 来绘制它,但两个伪节点都在左侧。(见第二张图片)我该如何对齐它们,或者还有其他完全不同、更好的方法吗?
我正在使用的代码:
\begin{figure}
\centering
\begin{tikzpicture}
\tikzset{
level 1/.style={level distance=15mm,sibling distance=65mm},
level 2/.style={level distance=15mm,sibling distance=30mm},
level 3/.style={level distance=15mm,sibling distance=15mm},
level 4/.style={level distance=15mm,sibling distance=10mm},
}
\node {$\frac{1}{1}$}
child[grow=up, left] {node {$\frac{0}{1}$} edge from parent[dashed]}
child[grow=up, right] {node {$\frac{1}{0}$} edge from parent[dashed]}
child {node {$\frac{1}{2}$}
child {node {$\frac{1}{3}$}
child {node {$\frac{1}{4}$}}
child {node {$\frac{2}{5}$}}
}
child {node {$\frac{2}{3}$}
child {node {$\frac{3}{5}$}}
child {node {$\frac{3}{4}$}}
}
}
child {node {$\frac{2}{1}$}
child {node {$\frac{3}{2}$}
child {node {$\frac{4}{3}$}}
child {node {$\frac{5}{3}$}}
}
child {node {$\frac{3}{1}$}
child {node {$\frac{5}{2}$}}
child {node {$\frac{4}{1}$}}
}
};
\end{tikzpicture}
\caption{...}
\end{figure}
答案1
我不知道自己在做什么,但我玩了一下......
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\centering
\begin{tikzpicture}
\tikzset{
level 1/.style={level distance=15mm,sibling distance=65mm},
level 2/.style={level distance=15mm,sibling distance=30mm},
level 3/.style={level distance=15mm,sibling distance=15mm},
level 4/.style={level distance=15mm,sibling distance=10mm},
}
\node {$\frac{1}{1}$}
child[grow=up, left=7cm] {node {$\frac{0}{1}$} edge from parent[dashed]}
child[grow=up, right=7cm] {node {$\frac{1}{0}$} edge from parent[dashed]}
child [left=6cm] {node {$\frac{1}{2}$}
child [right=-.5cm] {node {$\frac{1}{3}$}
child {node {$\frac{1}{4}$}}
child {node {$\frac{2}{5}$}}
}
child [left=-.5cm] {node {$\frac{2}{3}$}
child {node {$\frac{3}{5}$}}
child {node {$\frac{3}{4}$}}
}
}
child [left=6cm] {node {$\frac{2}{1}$}
child [right=-.5cm] {node {$\frac{3}{2}$}
child {node {$\frac{4}{3}$}}
child {node {$\frac{5}{3}$}}
}
child [left=-.5cm] {node {$\frac{3}{1}$}
child {node {$\frac{5}{2}$}}
child {node {$\frac{4}{1}$}}
}
};
\end{tikzpicture}
\end{document}