
私は、AVLを、私が教わった昔の方法で表現しようとしています。これは、最近の一般的な本に比べて、より直感的に表示できるとわかりました。私が興味を持っている機能を備えた生の画像は、以下から示されています。ニクラウス・ヴィルト 1986年刊そして手書きのメモもいくつか。
視覚化は特定のものかもしれませんが、AVL を表現することはコンピュータ サイエンスの一般的なデータ構造の概念であり、実際にはそのような図を講義ノートに使用することが意図されています。そのため、より幅広いユーザーにとって役立つものになると考えています。
この本から、示されているとおりのものを正確に取得しようとしています: Some nodes as circles
、some as long vertical rectangles
、破線でツリーの下部が強調表示され、X が付いた単一のノードが削除ノードを示しています。図に示すように、一部の四角形は上、中、下に残ります。破線を何らかの tikz ノードとして表し、それに応じて配置しようとしていますが、うまくいきません。
手書きのメモから、numbers around the nodes (-2, -1, h-1, etc)
長方形の上の文字「A、B、C、D」、およびを取得しようとしていますcircle oriented arrows to indicate rotation direction
。
私は数字を表現する方法を見つけることができました別の質問表現してみる三角形の木の部分しかし、彼らの最小限の例を自分の望むものに変更するのは特に困難でした。
答え1
TikZベースのforest
パッケージ比較的効率的に作業が行えるようです。次の例を参考にしてください。
2つのノードスタイルを定義します。
circnode
ノードを円としてフォーマットしますrectnode=<num>
<num>
ノードを高さがxに等しい長方形としてフォーマットします.5cm
\crossnode
さらに、指定した位置にクロスボックスを追加するためのマクロを定義します(あなたの例では、これは通常(some rectnode.south)
)
最後に、2 本の破線を手動で描画し、背景レイヤーに配置します。
最初の2つのケースのコード
\documentclass{article}
\usepackage{forest}
\usetikzlibrary{backgrounds}
\forestset{
circnode/.style={circle,draw,minimum size=.5cm,inner sep=0},
rectnode/.style={draw,minimum width=.5cm,fill=white,minimum height=#1*.5cm,child anchor=north,anchor=north},
}
\newcommand\crossnode[2][anchor=north]{
\node(temp)[draw,fill=white,minimum size=.5cm,yshift=\pgflinewidth,#1]at(#2){};
\draw(temp.north west)--(temp.south east) (temp.north east)--(temp.south west);
}
\begin{document}
\begin{forest}
for tree={l+=.5cm,s sep=1cm},
[
[B,circnode
[A,circnode
[,rectnode=3,name=n][,rectnode=3]
]
[,rectnode=3]
]
]
\crossnode[anchor=north]{n.south}
\begin{scope}[on background layer]
\draw[dashed,thin] let \p1=(temp.north), \p2=(current bounding box.west), \p3=(current bounding box.east) in
(\x2-.5cm,\y1)--(\x3+.5cm,\y1) (\x2-.5cm,\y1+.5cm)--(\x3+.5cm,\y1+.5cm);
\end{scope}
\end{forest}
\begin{forest}
for tree={l+=.5cm,s sep=1cm},
[
[C,circnode
[A,circnode,
[,rectnode=3]
[B,circnode
[,rectnode=2,name=n1]
[,rectnode=2,name=n2]
]
]
[,rectnode=3]
]
]
\crossnode{n1.south}
\crossnode{n2.south}
\begin{scope}[on background layer]
\draw[dashed,thin] let \p1=(temp.north), \p2=(current bounding box.west), \p3=(current bounding box.east) in
(\x2-.5cm,\y1)--(\x3+.5cm,\y1) (\x2-.5cm,\y1+.5cm)--(\x3+.5cm,\y1+.5cm);
\end{scope}
\end{forest}
\end{document}