2

I am drawing a tree like given below. The MWE for it is below:

\documentclass[]{article}   
\usepackage{forest}   
\title{}   
\author{}   
\begin{document}   
\date{}   
\begin{forest}   
    [n   
    [n-1   
    [n-2[$\vdots$]][n-2[$\vdots$]]]   
    [n-1   
    [n-2[$\vdots$]][n-2[$\vdots$]]]]   
\end{forest}   
\end{document}  

tree

I wish to write the count of number of nodes at each level of the tree. For example, adjacent to n, I wish to write 1. In the next level, 2 and so on. Since I am using forest package,

[n\hspace{1cm}1]

doesn't seem to help. Any simple ideas on how to do this?

Edit 1:

I found a related question which is not working for me.

  • I'm not surprised you couldn't get the code to work. I've more-or-less abandoned justtrees as a bad job. Some of the design decisions just made it horrible. On the positive side, it taught me to avoid those 'easy' solutions when writing prooftrees, though I realise that's not much consolation to you. – cfr Oct 01 '19 at 18:00

2 Answers2

2

A simple robust way would be to name one of the nodes in each level and use the y coordinate of that node (making use of the calc tikzlibrary) to get the correct height for each label. Counting the number of nodes can be done progrmatically - in your case the formula is clearly 2^n (use pgfs pow):

\documentclass[]{article}
\usepackage{forest}
\usetikzlibrary{calc} % let
\newcommand\leftsep{3} % How far left of center line of forest labels appear (cm)
\newcounter{levelcount} % Stores current level in tree
\newcommand\countnodes[1]{% Command to add label at height of node #1
    \draw let \p{L} = (#1) in (-\leftsep,\y{L}) node {\pgfmathparse{int(pow(2,\value{levelcount}))}\pgfmathresult};
    \stepcounter{levelcount} % Step counter for next level
}
\begin{document}
\begin{forest}
[n, name=root % Name root
[n-1, name=level1 % Name a node in level 1
[n-2, name=level2 [$\vdots$]][n-2[$\vdots$]]] % Name a node in level 2
[n-1
[n-2[$\vdots$]][n-2[$\vdots$]]]]
{% Node counting - these must be in order
\countnodes{root}
\countnodes{level1}
\countnodes{level2}
}
\end{forest}
\end{document}

Output:

output

pip
  • 1,847
  • After the vdots, I would like to write 2 raised to k, for the k-th level of nodes. I assume this cannot be done using this approach. – GermanShepherd Feb 25 '19 at 10:48
  • Sure, just name your vdots node e.g. mydots and add a command like \draw let \p{L} = (mydots) in (-\leftsep,\y{L}) node {$2^k$}; in the same group ({..}) as the \countnodes commands. (If you want it below the vdots, use e.g. \y{L}-1 instead of \y{L}.) – pip Feb 25 '19 at 10:59
2

Forest already counts levels, so a separate function to do this is superfluous. In addition, minimising the use of pgfmath will speed compilation. (Not an issue if this is your only tree and it is this simple, but more of a problem if you have many or complex trees.)

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

\begin{forest}
  for tree=math content,
  before drawing tree={
    tikz+={\coordinate (a) at (current bounding box.east);},
    for nodewalk={fake=r, L, ancestors}{
      if={>O+t_+t={content}{\vdots}}{
        tikz+={\node [anchor=base west] at (.base -| a) {$2^k$};}
      }{%
        tikz+/.process={Ow+Pw}{level}{int(2^#1)}{\node [anchor=base west] at (.base -| a) {#1};}
      }
    }
  }
  [n   
    [n-1   
      [n-2
        [\vdots]
      ]
      [n-2
        [\vdots]
      ]
    ]   
    [n-1   
      [n-2
        [\vdots]
      ]
      [n-2
        [\vdots]
      ]
    ]
  ]   
\end{forest}   

\end{document}

tree with labelled levels

cfr
  • 198,882
  • Yes, forest counts levels, this is neat! – GermanShepherd Oct 17 '19 at 05:24
  • 1
    @GermanShepherd Indeed, isn't Forest neat? So surprising how much you can pull out for free :). – cfr Oct 17 '19 at 22:49
  • @cfr Hello, Could you help me to draw a recursion tree for T(n)=T(n/2)+T(n/4)+n^2 using justtrees package that you provided it? Thank you. – tstt Nov 23 '23 at 13:22
  • Also https://tex.stackexchange.com/questions/302864/depth-labels-in-recursion-tree-using-forest?noredirect=1&lq=1 I read this post. – tstt Nov 23 '23 at 13:23
  • @MR_ I'd recommend asking a new question. justtrees turned out to be problematic and I dropped it, so it may or may not work with current Forest. But you can do the same directly with Forest if necessary. – cfr Nov 23 '23 at 15:08
  • @cfr I asked here, https://tex.stackexchange.com/questions/702119/draw-recursion-tree , could you see that? – tstt Nov 23 '23 at 17:38