6

How can the following recursion tree be drawn in LaTeX?

wanted

I have tried using TikZ pictures for this, but I continue to get errors in the code. Please suggest an easier code for the same.

  • 3
    Show us your code; this is not a 'please do it for me' site. – Svend Tveskæg Jul 28 '15 at 22:52
  • Can you post the code you've got? How can we suggest easier code if we don't know what you are using right now? – cfr Jul 28 '15 at 22:52
6

You can use the powerful forest package; you can even leave the content calculations to the package:

enter image description here

The code:

\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}
  • Why the answer? It's highly likely that the person asking the question hasn't made any effort at all and just want someone to do his/her work for him/her, even though something else is said. – Svend Tveskæg Jul 28 '15 at 23:56
  • 8
    @SvendTveskæg Why not? I'd say 70% or more of the questions here follow your rule. Acoording to that, this site would be full of unanswered questions. – Gonzalo Medina Jul 29 '15 at 0:02
  • Well, because this is not a "please do it for me" site, as far as I know. :-) (At least that was what I was told when I joined the community a few years ago.) – Svend Tveskæg Jul 29 '15 at 0:04
  • 4
    @SvendTveskæg I've been here more than you and I'm pretty convinced that basically this is a do it for me site, even if sometimes we try to think it's not :) And I see no problem with that. – Gonzalo Medina Jul 29 '15 at 0:11
4

forest allows you to specify a tree using standard bracket notation. See my answer to an earlier question for an explanation of how to turn a tree into a specification using bracket notation. Other answers there cover other packages suitable for drawing simple trees (as specified in the question). So if you don't like forest, perhaps one of the others will suit.

For forest, here's a prototype to get you started:

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

prototype

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.