
私は次のコードを使用してバイナリツリーとプレーンツリーを作成しています。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,chains,fit,shapes,calc}
\begin{tikzpicture}
\node[circle,draw](z){}
child[missing]{}
child{
node[circle,draw]{} child{node[circle,draw] {}} child[missing] };
\end{tikzpicture}
\end{document}
答え1
円はノードの数が多いため大きくなりますinner sep
。オプションinner sep=<length>
(例inner sep=1pt
: )でノードごとに個別に設定することも、画像全体に対して設定することもできます。
\begin{tikzpicture}[inner sep=1pt]
または、ここに示すように、ノードのスタイルを設定して、ノードの外観を個別に簡単に変更することもできます。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,chains,fit,shapes,calc}
\begin{document}
\begin{tikzpicture}
\tikzset{dot/.style={inner sep=1pt,circle,draw,fill},
circ/.style={inner sep=1pt,circle,draw}}
\node[dot](z){}
child[missing]{}
child{
node[dot]{} child{node[circ] {}} child[missing] };
\end{tikzpicture}
\end{document}
この例では、新しいスタイルdot
とがcirc
tikzpicture でのみ使用可能です。 の 2 行を\tikzset
プリアンブルに移動すると、すべての画像で使用できるようになります。
答え2
たくさんの木を描く必要がある場合は、非常に簡潔な木の仕様に適用できる強力なスタイルを作成できるため、Forest を学習する価値があります。
例えば、、およびの3つのスタイルを定義するとdot tree
、次のように書くことができます。dot tree spread
add arrow
\begin{forest}
dot tree spread,
add arrow,
[[[][]][][[[][][]]][]]
\end{forest}
\begin{forest}
dot tree spread,
add arrow,
where level=0{!1.no edge, coordinate}{
if n=1{}{
edge path'={(!p) -- ()},
},
},
[[[][]][][[[][][]]][]]
\end{forest}
\begin{forest}
dot tree,
where n children=0{
before computing xy={l*=.5, s*=.5},
edge+={densely dashed},
}{},
[[[][[][]]][[][[[][]][[[][[][[][]]]][]]]]]
\end{forest}
質問に示されている 3 つのツリーのシリーズを作成します。
完全なコード:
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\forestset{
dot tree/.style={
/tikz/>=Latex,
for tree={
inner sep=1pt,
fill,
draw,
circle,
calign angle=45,
calign=fixed edge angles,
},
baseline,
before computing xy={
where n children>=4{
tempcounta/.option=n children,
tempdima/.option=!1.s,
tempdimb/.option=!l.s,
tempdimb-/.register=tempdima,
tempdimc/.process={RRw2+P {tempcounta}{tempdimb}{##2/(##1-1)}},
for children={
if={>On>OR<&{n}{1}{n}{tempcounta}}{
s/.register=tempdima,
s+/.process={ORw2+P {n} {tempdimc} {(##1-1)*##2} }
}{},
},
}{},
},
},
dot tree spread/.style={
dot tree,
for tree={fit=rectangle},
},
add arrow/.style={
tikz+={
\draw [thick, blue!15!gray] (current bounding box.east) ++(2.5mm,0) edge [->] ++(10mm,0) ++(2.5mm,0) coordinate (o);
}
}
}
\begin{forest}
dot tree spread,
add arrow,
[[[][]][][[[][][]]][]]
\end{forest}
\begin{forest}
dot tree spread,
add arrow,
where level=0{!1.no edge, coordinate}{
if n=1{}{
edge path'={(!p) -- ()},
},
},
[[[][]][][[[][][]]][]]
\end{forest}
\begin{forest}
dot tree,
where n children=0{
before computing xy={l*=.5, s*=.5},
edge+={densely dashed},
}{},
[[[][[][]]][[][[[][]][[[][[][[][]]]][]]]]]
\end{forest}
\end{document}