更新:
我所做的是这样的:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3cm},
level 2/.style={sibling distance=1.5cm}]
\node {16}
child {node {14}
child {node [red] {4}
child {node {2}}
child {node {8}}
}
child {node [circle, draw, red] {7}
child {node {1}}
}
}
child {node {10}
child {node {9}}
child {node {3}}
};
\end{tikzpicture}
\end{document}
我对 tikz 还不熟悉,我看到有很多不同的包可以画树。我不知道最好使用“森林”
原始问题:
答案1
总的来说,TikZ 的基本树方法最不灵活,语法也最不实用。对于绘制这种树,我推荐使用包forest
。这样你就可以非常简单地绘制这棵树:
\documentclass{article}
\usepackage[linguistics]{forest}
\newcommand{\lbl}[2][red]{{\color{#1}^#2}}
\begin{document}
\begin{forest}for tree=math content
[\lbl{1}16
[\lbl{2}14,name=14
[4,red,name=4
[2]
[8]
]
[7,draw=red,circle
[1]
]
]
[\lbl{3}10,name=10
[\lbl{6}9 ]
[3,name=3]
]
]
\node [left of=4] (i) {i};
\draw[->] (i) -- (4);
\node[draw,fit=(i) (4)] {};
\node[draw,dashed,ellipse,rotate=45,fit=(10.north) (3)]{};
\draw[<->,] (4.north west) to[bend left] (14.south west);
\node [above of=i] {exchange};
\end{forest}
\end{document}