thumb|An example of a m-ary tree with m=5
In graph theory, an m-ary tree (for nonnegative integers m) (also known as n-ary, k-ary, k-way or generic tree) is an arborescence (or, for some authors, an ordered tree) in which each node has no more than m children. A binary tree is an important case where m = 2; similarly, a ternary tree is one where m = 3.
Types of m-ary trees
- A full m-ary tree is an m-ary tree where within each level every node has 0 or m children.
- A complete m-ary tree (or, less commonly, a perfect m-ary tree) is a full m-ary tree in which all leaf nodes are at the same depth.
Properties of m-ary trees
- For an m-ary tree with height h, the upper bound for the maximum number of leaves is <math>m^h</math>.
- The height h of an m-ary tree does not include the root node, with a tree containing only a root node having a height of 0.
- The height of a tree is equal to the maximum depth D of any node in the tree.
- The total number of nodes <math>N</math> in a complete m-ary tree is <math display="inline"> \sum_{i=0}^h m^i = \frac{m^{h+1} - 1}{m-1}</math>, while the height h is
<math display="block">\begin{align}
& \frac{m^{h+1} - 1}{m-1} \geq N > \frac{m^h - 1}{m-1} \\[8pt]
& m^{h+1} \geq (m - 1) \cdot N + 1 > m^h \\[8pt]
& h+1 \geq \log_m \left((m - 1) \cdot N + 1\right) > h \\[8pt]
& h \ge \left\lceil\log_m ((m - 1) \cdot N + 1) - 1\right\rceil.
\end{align}</math> By the definition of Big-Ω, the maximum depth
<math display="block">D = h \ge \left\lceil\log_m ((m - 1) \cdot N + 1) - 1\right\rceil =O(\log_m n) = O(\log n/\log m).</math>
- The height of a complete m-ary tree with n nodes is <math display="inline"> \lfloor \log_m ((m-1)\cdot n) \rfloor</math>.
- The total number of possible m-ary tree with n nodes is <math display="inline"> C_n = \frac{1}{(m-1)n+1} \cdot \binom{m \cdot n}{n}</math> (which is a Catalan number).
Traversal methods for m-ary trees
Traversing a m-ary tree is very similar to traversing a binary tree. The pre-order traversal goes to parent, left subtree and the right subtree, and for traversing post-order it goes by left subtree, right subtree, and parent node. For traversing in-order, since there are more than two children per node for m > 2, one must define the notion of left and right subtrees. One common method to establish left/right subtrees is to divide the list of children nodes into two groups. By defining an order on the m children of a node, the first <math display="inline">\{1, \dots, \lfloor \frac{m}{2} \rfloor \}</math> nodes would constitute the left subtree and <math display="inline">\{\lceil \frac{m}{2} \rceil, \dots , m\}</math> nodes would constitute the right subtree.
Convert a m-ary tree to binary tree
thumb|An example of conversion of a m-ary tree with m=6 to a binary tree.
Using an array for representing a m-ary tree is inefficient, because most of the nodes in practical applications contain less than m children. As a result, this fact leads to a sparse array with large unused space in the memory. Converting an arbitrary m-ary tree to a binary tree would only increase the height of the tree by a constant factor and would not affect the overall worst-case time complexity. In other words, <math display="inline">O(\log_m n)\equiv O(\log_2 n)</math> since <math display="inline">\log_2 m \cdot \log_m n =\frac{\log m}{\log 2} \cdot \frac{\log n}{\log m} = \log_2 n</math>.
First, we link all the immediate children nodes of a given parent node together in order to form a link list. Then, we keep the link from the parent to the first (i.e., the leftmost) child and remove all the other links to the rest of the children. We repeat this process for all the children (if they have any children) until we have processed all the internal nodes and rotate the tree by 45 degrees clockwise. The tree obtained is the desired binary tree obtained from the given m-ary tree.
Methods for storing m-ary trees
Arrays
thumb|An example of storing a m-ary tree with m=3 in an array
m-ary trees can also be stored in breadth-first order as an implicit data structure in arrays, and if the tree is a complete m-ary tree, this method wastes no space. In this compact arrangement, if a node has an index i, its c-th child in range {1,…,m} is found at index <math>m\cdot i+c</math>, while its parent (if any) is found at index <math display="inline">\left \lfloor \frac{i-1}{m} \right \rfloor</math> (assuming the root has index zero, meaning a 0-based array). This method benefits from more compact storage and better locality of reference, particularly during a preorder traversal. The space complexity of this method is <math>O(m^n)</math>.
Pointer-based
Each node would have an internal array for storing pointers to each of its <math>m</math> children:
thumb|Pointer-based implementation of m-ary tree where m=4.
Compared to array-based implementation, this implementation method has superior space complexity of <math>O(m\cdot n)</math>.
Enumeration of m-ary trees
Listing all possible m-ary trees is useful in many disciplines as a way of checking hypotheses or theories.
Proper representation of m-ary tree objects can greatly simplify the generation process. One can construct a bit sequence representation using the depth-first search of a m-ary tree with n nodes indicating the presence of a node at a given index using binary values. For example, the bit sequence x=1110000100010001000 is representing a 3-ary tree with n=6 nodes as shown below.
center|3-ary tree with bit sequence of 1110000100010001000 and Simple Zero Sequence of 004433
The problem with this representation is that listing all bit strings in lexicographic order would mean two successive strings might represent two trees that are lexicographically very different. Therefore, enumeration over binary strings would not necessarily result in an ordered generation of all m-ary trees. A better representation is based on an integer string that indicates the number of zeroes between successive ones, known as Simple Zero Sequence. <math display="inline">S= s_1, s_2, \dots , s_{n-1}</math> is a Simple Zero Sequence corresponding to the bit sequence <math display="inline">10^{s_1}10^{s_2}\ldots 10^{s_{n-110^j</math> where j is the number of zeroes needed at the tail end of the sequence to make the string have the appropriate length. For example, <math> 1110000100010001000 \equiv 10^0 10^0 10^4 10^4 10^3 \equiv 00433 </math> is the simple zero sequence representation of the above figure. A more compact representation of 00433 is <math>0^2 4^1 3^2</math>, which is called zero sequence, which duplicate bases cannot be adjacent. This new representation allows to construct a next valid sequence in <math>O(1)</math>.
A simple zero sequence is valid if
<math display="block">
\sum_{i=1}^{i=j} s_i \le (m-1)j \qquad \forall j \le n-1.
</math>That is to say that number of zeros in the bit sequence of a m-ary tree cannot exceed the total number of null pointers (i.e., pointers without any child node attached to them). This summation is putting restriction on <math>n-1</math> nodes so that there is room for adding the <math>n^th</math> without creating an invalid structure (i.e. having an available null pointer to attached the last node to it).
The table below shows the list of all valid simple zero sequences of all 3-ary trees with 4 nodes:
{| class="wikitable" style="margin-left: auto; margin-right: auto; border: none;"
| 222 || 200 || 111 || 033 || 013
|-
| 221 || 132 || 110 || 032 || 012
|-
| 220 || 131 || 105 || 031 || 011
|-
| 213 || 130 || 104 || 030 || 010
|-
| 212 || 123 || 103 || 024 || 006
|-
| 211 || 122 || 102 || 023 || 005
|-
| 210 || 121 || 101 || 022 || 004
|-
| 204 || 120 || 100 || 021 || 003
|-
| 203 || 114 || 042 || 020 || 002
|-
| 202 || 113 || 041 || 015 || 001
|-
| 201 || 112 || 040 || 014 || 000
|}
Starting from the bottom right of the table (i.e., "000"), there is a backbone template that governs the generation of the possible ordered trees starting from "000" to "006". The backbone template for this group ("00X") is depicted below, where an additional node is added in the positions labeled "x".
300px|frameless|center
Once one has exhausted all possible positions in the backbone template, a new template will be constructed by shifting the 3rd node one position to the right as depicted below, and the same enumeration would occur until all possible positions labeled "X" is exhausted.
400px|frameless|center
Going back to the table of enumeration of all m-ary trees, where <math>m=3</math> and <math>n=4</math>, we can easily observe the apparent jump from "006" to "010" can be explained trivially in an algorithmic fashion as depicted below:
400px|frameless|center
The pseudocode for this enumeration is given below:
<math display="block">\sum_{i=j}^{n} \sum_{t=2}^{m} c_{(i-1)(m-1)+t-1} \qquad \le n-j ,\qquad \forall j \in 0 \dots n.</math>
Lexicographically smallest code-word representation of a m-ary with n nodes is all zeros and the largest is n−1 ones followed by m−1 zero on its right.
Initialization
c[i] to zero for all i from 1 to n⋅(k − 1)
p[i] set to n − 1 for i from 1 to n
sum ← 0
j ← m − 1
Termination Condition
Terminate when c[1] = n − 1
Procedure NEXT
