# How to a draw a tree with n edges and show count of edges at each level?

I am trying to draw a combinatorics tree starting with n_1 edges at level 1, n_2 edges at level 2, ... , n_k edges at level k. Something like this:

I found a binary tree code from this site:

Creating dots/lines brackets in tree with Tikz

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees,calc,arrows.meta,positioning,decorations.pathreplacing,bending}

\tikzset{
edge from parent/.style={draw, thick, blue!70!black},
no edge from this parent/.style={
every child/.append style={
edge from parent/.style={draw=none}}},
level 3/.style={yshift=5cm},
level 4/.style={level distance=5mm}
}

\begin{document}
\begin{tikzpicture}[
level/.style={sibling distance=40mm/#1},
text=black!70!black,>=latex,
font=\sffamily
]

\node (z){1}
child {node (a) {2}
child {node  (b) {3}
child {node (b1) {$\vdots$}[no edge from this parent]
child {node (b11) {k}}
}
child {node (b2) {$\vdots$}[no edge from this parent]
child {node (b12) {k}}
}
}
child {node (g) {3}
child {node (g1) {$\vdots$}[no edge from this parent]
child {node (g11) {k}}
}
child {node (g2) {$\vdots$}[no edge from this parent]
child {node (g12) {k}}
}
}
}
%child{node(h) {2}[no edge from this parent]{2}}
child {node (d) {2}
child {node  (e) {3}
child {node (e1) {$\vdots$}[no edge from this parent]
child {node (e11) {k}}
}
child {node (e2) {$\vdots$}[no edge from this parent]
child {node (e12) {k}}
}
}
child {node (f) {3}
child {node (f1) {$\vdots$}[no edge from this parent]
child {node (f11) {k}}
}
child {node (f2) {$\vdots$}[no edge from this parent]
child {node (f12) {k}
}
}
}
};

\node[left=5 of z]  (ln1) {$n_1$ ways}[no edge from this parent]
child {node (ln2) {$n_2$ ways}[no edge from this parent]
child {node (ln3) {$n_3$ ways}[no edge from this parent]
child {node (ln4) {}[no edge from this parent]
child {node (ln5) {$n_k$ ways}}}}};

%\path (b12.north east) -- (g11.north west) node  {$\cdots$};
%\path (e12.north east) -- (f11.north west) node [midway] {$\cdots$};

\coordinate (cd1) at ($(f12)+(1,0)$);
\coordinate (nb1) at ($(g12)!.5!(e11)$);

%\draw[blue!70!black,thick,<->,]
%(cd1) -- (cd1|-z.east) node [near start, fill=white] {log(n)};

\draw[black!70!black,dashed,thick,->]
($(z.west)+(-1em,0)$) -- (ln1);
\draw[black!70!black,dashed,thick,->]
($(a.west)+(-1em,0)$) -- (ln2.east);
\draw[black!70!black,dashed,thick,->]
($(b.west)+(-1em,0)$) -- (ln3);
\draw[black!70!black,dashed,thick,->]
($(b11.west)+(-1em,0)$) -- (ln5);

\draw[black!70!black,thick,decorate,decoration={brace,amplitude=10pt,mirror},->,-{latex[flex=1pt]}] (b11.south west) -- (f12.south east);
\end{tikzpicture}


I tried to edit this to suit my needs, but I am stuck at getting the code to work properly. Moreover, I am not sure if this code can be generalised to n levels with vdots. I also need to show count of edges at each level.

• Please provide a link to the question or answer you are using and attribute it correctly. I would be better to post a complete example rather than just a fragment we can't compile.
– cfr
Apr 23, 2016 at 13:23
• If you want the code to do the counting etc. look at Forest. There are similar examples on this site. But I'm not really clear what you mean by generalising it. What exactly do you want the code to do?
– cfr
Apr 23, 2016 at 13:24
• My Bad .. I used code from tex.stackexchange.com/questions/241232/…. Atleast one problem that I have is to how do put \vdots between level 2 and level k. Moreover, my attempts to customise the code haven't worked so far. Apr 23, 2016 at 14:16
• I found this tex.stackexchange.com/questions/236501/…. But I have the same problem. how do I put \vdots between level 2 and level k. vdots and the next level are printed together. I wants vdots on top of level 3. I am not sure if I explained clearly. Apr 23, 2016 at 14:32
• As I say, I'd look at the Forest solutions for this kind of tree. I've certainly answered similar questions with dots in them before, but I'm by no means the only one.
– cfr
Apr 23, 2016 at 19:24

I note that your example shows no signs of any attempt to modify the tree as you desire. Not even the content of the nodes is altered to reflect your target image.

Anyway, though I ought not to answer this question, I'm going to anyway. At least, I'm answering the aspects which interest me. Further modifications are left for you, gentle reader, to complete at leisure.

This could be drawn essentially automatically, I think, by just feeding Forest the requisite numbers. However, I would only bother to do that if I wanted to draw several of these with different numbers of levels/siblings etc.

The only tricky bits here are the styles to make the arcs, which test whether we are at level 1 and whether the current node is its parent's last child and then proceeds accordingly, and the patched definition of nice empty nodes which is required to work around a bug in Forest's linguistics library but is here also adapted a bit to ensure things are not done to early in the process of constructing the tree. (Some of this could be streamlined as I did things while playing around which are probably no longer required.)

Here's some explanation of the arcs which are not just an extension of a workaround for a bug.

  mk arc/.style n args=2{
tikz+={
\draw [-{Stealth[bend]}] ($(!u.parent anchor)!1/5!(!u1.child anchor)$) [out=-30, in=-150] to ($(!u.parent anchor)!1/5!(!ul.child anchor)$) node [right] {$n_#1$ #2} ;
}
},


mk arc just takes 2 arguments: a number for the subscript and an additional annotation, which may be empty. It then draws the arc with the arrow between the current node's parent's first and last child and appends the relevant label.

  make arc/.style={
if level=1{
mk arc={1}{options for choice 1}
}{
mk arc/.wrap 2 pgfmath args={{##1}{##2}}{(n_children()==0) ? ("k") : level()}{((n("!u"))==(n_children("!uu"))) ? strcat(strcat("options for choice $",((n_children()==0) ? ("k") : level())),"$") : ""}
},
},


make arc figures out which arguments should be passed to mk arc by considering where we are in the tree. It then passes those arguments to mk arc which draws and annotates the arced arrow.

Complete code:

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage[linguistics]{forest}
\usetikzlibrary{arrows.meta,bending,calc}
\forestset{%
mk arc/.style n args=2{
tikz+={
\draw [-{Stealth[bend]}] ($(!u.parent anchor)!1/5!(!u1.child anchor)$) [out=-30, in=-150] to ($(!u.parent anchor)!1/5!(!ul.child anchor)$) node [right] {$n_#1$ #2} ;
}
},
make arc/.style={
if level=1{
mk arc={1}{options for choice 1}
}{
mk arc/.wrap 2 pgfmath args={{##1}{##2}}{(n_children()==0) ? ("k") : level()}{((n("!u"))==(n_children("!uu"))) ? strcat(strcat("options for choice $",((n_children()==0) ? ("k") : level())),"$") : ""}
},
},
nice empty node/.style={% override buggy version of nice empty nodes supplied by Forest & modify for this case
delay={
if content={}{%
shape=coordinate,
for nodewalk={%
Nodewalk={%
on invalid=fake,
}{%
parent,
}{%
for children={anchor=north},
}
}{},
}{},
}
},
nice empty nodes/.style={%
before packing={
for tree={calign=fixed edge angles},
},
before typesetting nodes={
for tree={
nice empty node,
}
},
},
}

\begin{document}
\begin{forest}
nice empty nodes,
for tree={
fit=band,
},
[
[, make arc
[, make arc
[, edge=densely dotted
[, make arc]
[]
[]
[]
[]
]
[, phantom]
]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[[, edge=densely dotted]]
[, make arc
[, phantom]
[, edge=densely dotted
[, make arc]
[]
[]
[]
[, label=-45:$n_1\cdot n_2 \cdot\ldots n_k$]
]
]
]
]
\end{forest}
\end{document}