LaTeX 中的遞歸樹

LaTeX 中的遞歸樹

在LaTeX中如何繪製下面的遞歸樹?

通緝

我嘗試過使用TikZ 圖片,但我仍然在程式碼中遇到錯誤。請建議一個更簡單的程式碼。

答案1

你可以使用強大的forest包裹;您甚至可以將內容計算留給包裝:

在此輸入影像描述

代碼:

\documentclass[border=5pt]{standalone}
\usepackage{amsmath}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={
  parent anchor=south,
  s sep=2pt
},
delay={for descendants={
  if n=1
    {draw,content/.wrap 2 pgfmath args=
      {$c\binom{n}{#2}^{2}$}{content}{int(2^level)}}
      {if n'=1{draw,content/.wrap 2 pgfmath args=
        {$c\binom{n}{#2}^{2}$}{content}{int(2^level)}}{}
      },  
     where n children={0}{draw,content=c}{}
  }
}
[$cn^{2}$,draw
  [
    [
      [
        []
        []
        []
      ]
      [
        [,phantom]
      ]
      [
        [,phantom]
      ]
      [
        []
        []
        []
      ]
    ]
    [
      [,phantom]
    ]
    [
      [,phantom]
    ]
    [
      [
        []
        []
        []
      ]
      [
        [,phantom]
      ]
      [
        [,phantom]
      ]
      [
        []
        []
        []
      ]
    ]
  ]
  [
    [,phantom [,phantom]]
  ]
  [
    [,phantom [,phantom]]
  ]
  [
    [
      [
        []
        []
        []
      ]
      [
        [,phantom]
      ]
      [
        [,phantom]
      ]
      [
        []
        []
        []
      ]
    ]
    [
      [,phantom]
    ]
    [
      [,phantom]
    ]
    [
      [
         []
        []
        []
     ]
      [
        [,phantom]
      ]
      [
        [,phantom]
      ]
      [
        []
        []
        []
      ]
    ]
  ]
]
\end{forest}

\end{document}

答案2

forest允許您使用標準括號表示法指定樹。看我對之前問題的回答有關如何使用括號表示法將樹轉換為規範的說明。其他答案涵蓋了適合繪製簡單樹的其他包(如問題中指定)。因此,如果您不喜歡forest,也許其他之一會適合。

對於forest,這裡有一個可以幫助您入門的原型:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}

\begin{forest}
  for tree={
    draw,
    align=center
  }
  [root
    [child
      [grandchild
        [great\\-grandchild]
        [great\\-grandchild]
        [great\\-grandchild]
      ]
      [grandchild, calign with current]
      [grandchild
        [great\\-grandchild]
        [great\\-grandchild]
        [great\\-grandchild]
      ]
    ]
    [child
      [grandchild
        [great\\-grandchild]
        [great\\-grandchild]
        [great\\-grandchild]
      ]
      [grandchild, calign with current]
      [grandchild
        [great\\-grandchild]
        [great\\-grandchild]
        [great\\-grandchild]
      ]
    ]
    [child
      [grandchild
        [great\\-grandchild]
        [great\\-grandchild]
        [great\\-grandchild]
      ]
      [grandchild, calign with current]
      [grandchild
        [great\\-grandchild]
        [great\\-grandchild]
        [great\\-grandchild]
      ]
    ]
    [child
      [grandchild
        [great\\-grandchild]
        [great\\-grandchild]
        [great\\-grandchild]
      ]
      [grandchild, calign with current]
      [grandchild
        [great\\-grandchild]
        [great\\-grandchild]
        [great\\-grandchild]
      ]
    ]
  ]
\end{forest}


\end{document}

原型

相關內容