
다음 코드를 사용하여 바이너리 및 평면 트리를 만들었습니다.
\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
tikzpicture circ
에서만 사용할 수 있습니다. 두 줄을 \tikzset
서문으로 이동하면 모든 그림에 사용할 수 있습니다.
답변2
많은 나무를 그려야 한다면 매우 간결한 나무 사양에 적용할 수 있는 강력한 스타일을 만들 수 있으므로 Forest를 배우는 것이 가치가 있습니다.
예를 들어 세 가지 스타일 , 을 정의하면 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}
질문에 표시된 세 개의 트리 시리즈를 생성하려면 다음을 수행하십시오.
전체 코드:
\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}