0

Suppose we want to draw recursion tree of T(n)=T(n/4)+T(n/2)+n^2 using Justtree package that introduced here (justtree.sty).

I try as follows:

    \begin{justtree}
    {
        declare count={tree n}{1},
        just format={xshift=1.5em},
        annotate/.style={% style should be applied to the rightmost node at each level for which an arrow and annotation is required
            if n children=0{}{
                right just=$n^2$,
            },
        },
        where n children=0{
            edge={dotted},
        }{},
        for tree={
            math content,
            if level=0{}{%
                if level=1{%
                    tree n'=1,
                }{%
                    if n=1{%
                        tree n/.wrap pgfmath arg={#1}{tree_n("!u")},
                    }{%
                        tree n/.wrap pgfmath arg={#1}{int((tree_n("!u"))},
                    },
                },
            },
            delay={
                if level=1{
                    content=n^2,
                    tikz+={
                        \draw [<->] (!F.south west) +(-2.5em,0) coordinate (c) -- (.north -| c) node [midway, anchor=south, sloped] {$\log_{\frac{4}{3}}n$};
                    }
                    ,
                }{
                    if n children=0{%
                    }{
                        if level=0{}{
                            content/.wrap 2 pgfmath args={(\frac{#1n}{#2})^2}{(tree_n()==1) ? "" : (tree_n())}{int(4^(level("!u")))},
                        }
                    }
                }
            },
        },
    }
    [, annotate
    [
    [
    [[][]]
    [[][]]
    ]
    [
    [[][]]
    [[][]]
    ]
    ]
    [, annotate
    [
    [[][]]
    [[][]]
    ]
    [, annotate
    [, annotate[][]]
    [[][]]
    ]
    ]
    ]
\end{justtree}

Unfortunately, above code draws a recursion tree for T(n)=T(n/4)+T(3n/4)+n^2 Could someone help me to modify the above code to achieve my goal?

4
  • I don't have time right now, but I'll look later.
    – cfr
    Commented Nov 23, 2023 at 17:41
  • 1
    Give us a compilable code.
    – projetmbc
    Commented Nov 23, 2023 at 18:27
  • And what is it supposed to look like?
    – cfr
    Commented Nov 23, 2023 at 22:42
  • 1
    If you copy code into your question, note three things. First, you should say whose it is and link the source. SE's licence requires it, but it also makes it far easier for people to help you. [Note that this is true even if the person trying to help you was the original author. At least, it is true for me. Perhaps everyone else has a better system than I do.] Second, just copying code doesn't constitute making an effort. You should show what you tried to adapt it to your needs. Third, you should never post fragments.
    – cfr
    Commented Nov 24, 2023 at 5:09

1 Answer 1

3

Note 1: I don't really think I should answer this question. It provides a fragment of copied code without link or attribution, makes no effort to adapt that code and doesn't even bother to explain what the new tree should look like. I'm answering because I like doing things with Forest. If someone finds it useful, well and good.

Note 2: I consider justtrees inherently flawed and abandoned work on it several years ago. Unlike prooftrees, justtrees died an early death and never made it to CTAN. I have no plans to resurrect it. I recommend using forest directly unless you are drawing tableaux, in which case I recommend prooftrees. If this answer is at odds with that position, so much the worse for this answer.

Caveat emptor ...


recursion tree

This case is significantly simpler than the earlier example because the numerator is always simply n. (But it took me some while to figure this out because I had to figure out what a recursion tree was first.)

\documentclass[border=10pt]{standalone}
\usepackage[linguistics]{forest}
\usepackage{justtrees}

\begin{document}
\begin{justtree}
  {% ateb cfr: https://tex.stackexchange.com/a/702157/
    declare count={tree n}{1},
    just format={xshift=1.5em},
    annotate/.style={% style should be applied to the rightmost node at each level for which an arrow and annotation is required
      if n children=0{}{
        right just=$#1$,
      },
    },
    where n children=0{
      edge={dotted},
    }{},
    for tree={
      math content,
      if level=0{}{%
        if level=1{%
          tree n'=1,
        }{%
          if n=1{%
            tree n/.process={ O+nw+P {!u.tree n} {int(4*#1)}},
          }{%
            tree n/.process={ O+nw+P {!u.tree n} {int(2*#1)}},
          },
        },
      },
      delay={
        if level=1{
          content=n^2,
          tikz+={
            \draw [<->] (!F.south west) +(-2.5em,0) coordinate (c) -- (.north -| c) ;
          }
          ,
        }{
          if n children=0{%
          }{
            if level=0{}{
              content/.process={%
                O  w
                {tree n}  {(\frac{n}{#1})^2}
              },
            }
          }
        }
      },
    },
  }
  [, annotate=n^2
    [
      [
        [[][]]
        [[][]]
      ]
      [
        [[][]]
        [[][]]
      ]
    ]
    [, annotate=\frac{5n^2}{16}
      [
        [[][]]
        [[][]]
      ]
      [
        [[][]]
        [[][]]
      ]
    ]
  ]
\end{justtree}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .