我怎麼能用點製作平面/二叉樹?

我怎麼能用點製作平面/二叉樹?

我一直在使用以下程式碼創建二元樹和平面樹:

\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}

在範例中,新樣式dotcirc僅在 tikzpicture 中可用。如果將這兩行移動\tikzset序言中,則它們適用於所有圖片。

答案2

如果您需要繪製許多樹,那麼值得您花時間學習 Forest,因為它允許您創建強大的樣式,您可以將其應用於極其簡潔的樹規範。

例如,定義三種樣式 ,dot treedot tree spreadadd 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}

相關內容