Skip to main content

N-ary Tree

An n-ary tree is a rooted tree in which each node can have at most n children. This generalizes the binary tree concept to any number of children.

  • If n = 2, it’s a binary tree.
  • If n = 3, each node can have 0, 1, 2, or 3 children.

Structure of an N-ary Tree (n = 3 Example)

Types of N-ary Trees

  • Full N-ary Tree – Every internal node has exactly n children.
  • Complete N-ary Tree – All levels except possibly the last are full; last level filled left to right.
  • Perfect N-ary Tree – All internal nodes have exactly n children, and all leaves are at the same depth.

Number of Possible N-ary Trees – Catalan Generalization

For full n-ary trees (every internal node has exactly n children), the generalized Catalan number gives the number of structurally distinct trees with m internal nodes:

Cm(n)=1(n1)m+1(nmm)C^{(n)}_m = \frac{1}{(n-1)m + 1} \binom{nm}{m}

Where:

  • mm = number of internal nodes
  • Total nodes = nm+1nm + 1

Example: For a full ternary tree (n=3n = 3) with m=2m = 2 internal nodes:

C2(3)=15(62)=1515=3C^{(3)}_2 = \frac{1}{5} \cdot \binom{6}{2} = \frac{1}{5} \cdot 15 = 3

Number of Labelled Full N-ary Trees

If nodes are labelled (distinct), the number is:

Count=Cm(n)(nm+1)!\text{Count} = C^{(n)}_m \cdot (nm + 1)!

Where:

  • Cm(n)C^{(n)}_m = generalized Catalan number
  • nm+1nm + 1 = total nodes

Max and Min Number of Nodes Given Height

Let hh = height (number of edges from root to deepest leaf).

  • Max Nodes (Perfect N-ary Tree):
Nmax=nh+11n1N_{\text{max}} = \frac{n^{h+1} - 1}{n - 1}
  • Min Nodes (Full N-ary Tree):
Nmin=nh+1N_{\text{min}} = nh + 1

If not strict:

Nmin=h+1N_{\text{min}} = h + 1

Example: For n=3n = 3 and h=2h = 2: Max:

33131=13\frac{3^{3} - 1}{3 - 1} = 13

Min (full):

32+1=73 \cdot 2 + 1 = 7

Min (non-full):

2+1=32 + 1 = 3

Max and Min Height Given Number of Nodes

Let NN = total nodes.

  • Min Height (Perfect N-ary Tree):
hmin=logn((N(n1))+1)1h_{\text{min}} = \lfloor \log_n((N(n-1))+1) \rfloor - 1
  • Max Height: If full: hmax=N1nh_{\text{max}} = \frac{N - 1}{n} If not full: hmax=N1h_{\text{max}} = N - 1

Internal and External Nodes (Full N-ary Tree)

For a full n-ary tree:

E=(n1)I+1E = (n-1)I + 1

Where:

  • EE = external nodes (leaves)
  • II = internal nodes

Also:

Total Nodes=E+I\text{Total Nodes} = E + I

Example: For n=3n = 3, I=4I = 4 internal nodes:

E=(31)4+1=9E = (3-1) \cdot 4 + 1 = 9

Total nodes:

4+9=134 + 9 = 13

Summary Table for N-ary Tree

ConceptFormula
Generalized Catalan Number (full)Cm(n)=1(n1)m+1(nmm)C^{(n)}_m = \frac{1}{(n-1)m + 1} \binom{nm}{m}
Labelled full n-ary treesCm(n)(nm+1)!C^{(n)}_m \cdot (nm+1)!
Max nodes for height hhnh+11n1\frac{n^{h+1} - 1}{n - 1}
Min nodes for height hh (full)nh+1nh + 1
Min nodes for height hh (non-full)h+1h + 1
Min height for NN nodeslogn((N(n1))+1)1\lfloor \log_n((N(n-1))+1) \rfloor - 1
Max height for NN nodes (full)N1n\frac{N - 1}{n}
Max height for NN nodes (non-full)N1N - 1
Full n-ary tree relationE=(n1)I+1E = (n-1)I + 1