我对 Latex 完全陌生,目前正在尝试创建将节点插入树的流程。我的树上的线条有问题,因为有些线条变直了,有人知道如何解决这个问题吗?
\begin{minipage}{0.1\textwidth}
\centering
\begin{tikzpicture}[
edge from parent path=
{(\tikzparentnode.south) .. controls +(0,-.5) and +(0,.5)
.. (\tikzchildnode.north)},
every node/.style={draw,circle, label distance=-2mm},
level distance=20mm
]
\node [label=330:$0$]{5};
\end{tikzpicture}
\end{minipage}%
\begin{minipage}{0.02\textwidth}
\centering
$\rightarrow$
\end{minipage}%
\begin{minipage}{0.1\textwidth}
\centering
\begin{tikzpicture}[
edge from parent path=
{(\tikzparentnode.south) .. controls +(0,-.5) and +(0,.5)
.. (\tikzchildnode.north)},
every node/.style={draw,circle, label distance=-2mm},
level distance=20mm
]
\node [label=330:$1$]{5}
child {node[label=330:$0$] {4}};
\end{tikzpicture}
\end{minipage}%
答案1
编辑:
AVL 树的示例(如维基百科):
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
C/.style = {draw, circle, minimum size=1.5em, inner sep=0pt,
label={[font=\scriptsize, text=blue, inner sep=0pt]330:$#1$}},
]
\node [C=-1] {5}
child {node[C=1] {5}}
child {node[C=0] {4}}
;
\end{tikzpicture}
\end{document}
\tikz
或者用纯使用库绘制的两棵树的演变positioning
(arrows.meta
仅适用于好看的箭头):
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 7mm and 21mm,
C/.style = {draw, circle, minimum size=1.5em, inner sep=0pt,
label={[font=\scriptsize, text=blue, inner sep=0pt]330:$#1$}},
]
\node (n1)[C=-1] {5};
\node (n21) [C=1, above right=of n1] {5};
\node (n22) [C=0, below right=of n1] {4};
\draw (n21) -- (n22);
\draw[-Straight Barb,shorten <=7mm, shorten >=7mm] (n1) -- (n1 -| n21);
\end{tikzpicture}
\end{document}