# m-ary tree

In graph theory, an ** m-ary tree** (also known as

**or**

*k*-ary**tree) is a rooted tree in which each node has no more than**

*k*-way*m*children. A binary tree is the special case where

*m*= 2, and a ternary tree is another case with

*m*= 3 that limits its children to three.

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.

Listing all possible *m*-ary trees are useful in many disciplines as a way of checking hypothesis 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.

The table below shows the list of all valid simple zero sequences of all *3*-ary trees with 4 nodes:

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".

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.

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.

One of the applications of *m*-ary tree is creating a dictionary for validation of acceptable strings. In order to do that, let *m* be equal to the number of valid alphabets (e.g., number of letters of the English alphabet) with the root of the tree representing the starting point. Similarly, each of the children can have up to *m* children representing the next possible character in the string. Thus, characters along the paths can represent valid keys by marking the end character of the keys as "terminal node". For example, in the example below "at" and "and" are valid key strings with "t" and "d" marked as terminal nodes. Terminal nodes can store extra information to be associated with a given key. There are similar ways to building such a dictionary using B-tree, Octree and/or trie.