更新:
我所做的是這樣的:
\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}