
Tenho criado árvores binárias e planas usando o seguinte código:
\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}
Ele faz círculos muito grandes nas extremidades das bordas, e espero ter nós menores e, eventualmente, fazer algo parecido com isto:
Responder1
Os círculos são grandes devido ao inner sep
número de nós. Você pode configurá-lo com a opção inner sep=<length>
(por exemplo inner sep=1pt
), individual para cada nó ou para a imagem inteira com
\begin{tikzpicture}[inner sep=1pt]
Ou você pode definir estilos para seus nós para alterar facilmente sua aparência individualmente, conforme mostrado aqui
\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}
No exemplo os novos estilos dot
e circ
só estão disponíveis na imagem tikz. Se você mover as duas linhas para \tikzset
no preâmbulo, elas estarão disponíveis para todas as imagens.
Responder2
Se você precisar desenhar muitas árvores, valerá a pena aprender Floresta, pois permite criar estilos poderosos que podem ser aplicados a especificações de árvores extremamente concisas.
Por exemplo, definir três estilos, dot tree
e dot tree spread
nos add arrow
permite escrever
\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}
para produzir a série de três árvores mostradas na questão:
Código completo:
\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}