# M-array Tree in Discrete Mathematics

An m-array tree can be described as a generalization of a binary tree in which each and every node has M or less children. In other words, a tree will be known as the m array tree if each node of the tree does not contain more than m children. A binary tree will be known as the M-ary tree if it contains M = 2. With the help of following image, we can see an example of M-ary tree where M = 3.

### Types of M-ary tree

1. The given tree will be known as the full M-ary tree if every node of the M-ary tree must contain either 0 or M children.
2. The tree will be known as the perfect tree if every leaf node of this tree is at the same depth.
3. The tree will be known as the complete tree if all levels of the m-ary tree except the last one are full. In the complete tree, every non-leaf node will have M children. However, if the last level of the tree is incomplete, then all the nodes of the given tree will be as far left as possible.

### Properties of M-ary tree

There are various properties in m ary tree, and some of them are described as follows:

If there is a full m ary tree, which has i internal vertices, then we can calculate the vertices and leaves of that tree with the help of following formula:

For example: Suppose there are 4 internal vertices and m = 3 in a tree. Now we have to calculate the number of vertices and number of leaves in that tree.

Solution: From the question, we have m = 3, and i = 4 and we know that

Among 13 vertices, there is a root vertice, and the remaining 12 vertices are children of those 4 internal vertices.

Now we will calculate the leaves of the tree with the help of following formula:

Hence there will be 13 vertices and 9 numbers of leaves.

### Traversal

The binary trees and the M ary tree both have the same properties, which are described as follows:

Pre-order traversal:

In the process of pre-order traversal, we will first visit a root node, and then we will traverse to its subtree, which contains the left subtree and the right subtree. So after visiting the root node, we will first traverse the left subtree, and then we will traverse the right subtree. We can only visit one node at a time. The name "preorder traverse" also refers to the root node coming before the left and right subtree. In the preorder traverse, the time complexity is O(n). In the following form, we can represent the process of preorder traversal like this:

Post-order traversal:

In the process of post-order traversal, we will follow the left-right root policy. In this policy, we will recursively traverse all subtrees rooted in children. There are two subtrees: the left subtree and right subtree. So we will first traverse the left subtree, and then we will traverse the right subtree. Only after visiting all subtrees, we will visit the root. The name "post-order traverse" also refers to the root node coming after the left and right subtree. In the post-order traversal, the time complexity is also O(n). In the following form, we can represent the process of post-order traversal like this:

In order traverse:

In the process of in-order traversal, we will follow the left-root-right policy. In this policy, we will first traverse the left subtrees, then we will visit the root, and after that, we will traverse the right subtree. The name "in-order traverse" also refers to the root node will come between the left and right subtree. In in-order traversal, the time complexity is also O(n). In the following form, we can represent the process of in order traverse like this:

There is one more way in which we can explain the traversal of m ary tree. The traversal of binary tree and the m ary tree are both similar. But there can be a case where m>2, which contains more than two children per node, so we have to define the notation of left and right subtrees. We can get the left and right subtree with the help of dividing the list of children nodes of the tree into two groups. With the help of defining the order on the m children of a node, we will constitute some part of the node on the left subtree, i.e., {0, 1,... m/2}, and we will constitute the other part of the node on the right subtree, i.e., {m/2, ...., m}.

### Method of storing m-ary trees

We can store the m ary tree with the help of two ways: we can use an array, or we can use a pointer-based. Now we will explain them one by one like this:

Array: Suppose we have a complete m ary tree. In this type of compact arrangement, if there is an index i, then we can find its cth child in range {1,...., m} with the help of its index m.i + c. In this case, this method wastes no space. In this type of method, the space complexity will be O(mn).

Pointer-based: There must be an internal array for each node so that this array can store pointers to each of its m children. The implementation method of pointer-based contains superior space complexity as compared to the array-based implementation. So the space complexity of pointer-based method is O(m.n).

### Converting m-ary tree into binary tree

Suppose we try to convert an arbitrary m ary tree into a binary tree. In this case, we have to use a constant factor, which is used to only increase the height of a tree, and it will not be useful to affect the overall worst-case time complexity. First, we will form a link list with the help of linking all the intermediate children nodes of the given parent node. After that, we will keep the link from parent to the first child, i.e., the leftmost child, and in this process, we will remove all the other links to the rest children.

### Applications of M-ary trees

There are a lot of applications in the M-ary tree, and some of them are described as follows:

1. The M-ary tree is useful in color quantization.
2. We can use it in mesh generation and image processing.
3. In case of natural language processing applications, we will use the parse and grammar trees.
4. In case of rendering and collision detection, we can use game engines.
5. We can represent the structure of program code with the help of abstract syntax trees.
6. We can show a file system data structure. Here node is used to represent the files and folders and many more places.

In the following image, we will indicate an example of a grammar tree like this: