ArticleslgStudy

computer science

M-ary tree

M-ary tree is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand M-ary tree rather than just read about it. In short: 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.

M-ary tree — main illustration
M-ary tree — illustration

Key takeaways

  • M-ary tree belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect M-ary tree to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of M-ary tree from memory before moving on to harder problems.

Reference excerpt

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 mh. 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 N in a complete m-ary tree is ∑i=0hmi=mh+1−1m−1, while the height h is mh+1−1m−1≥N>mh−1m−1mh+1≥(m−1)⋅N+1>mhh+1≥logm((m−1)⋅N+1)>hh≥⌈logm((m−1)⋅N+1)−1⌉. By the definition of Big-Ω, the maximum depth D=h≥⌈logm((m−1)⋅N+1)−1⌉=O(logmn)=O(log⁡n/log⁡m).

The height of a complete m-ary tree with n nodes is ⌊logm((m−1)⋅n)⌋. The total number of possible m-ary tree with n nodes is Cn=1(m−1)n+1⋅(m⋅nn) (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 {1,…,⌊m2⌋} nodes would constitute the left subtree and {⌈m2⌉,…,m} nodes would constitute the right subtree.

Convert a m-ary tree to 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, O(logmn)≡O(log2n) since log2m⋅logmn=log⁡mlog⁡2⋅log⁡nlog⁡m=log2n. 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

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 m⋅i+c, while its parent (if any) is found at index ⌊i−1m⌋ (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 O(mn).

Pointer-based Each node would have an internal array for storing pointers to each of its m children:

Compared to array-based implementation, this implementation method has superior space complexity of O(m⋅n).

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.

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. S=s1,s2,…,sn−1 is a Simple Zero Sequence corresponding to the bit sequence 10s110s2…10sn−110j 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, 1110000100010001000≡100100104104103≡00433 is the simple zero sequence representation of the above figure. A more compact representation of 00433 is 024132, which is called zero sequence, which duplicate bases cannot be adjacent. This new representation allows to construct a next valid sequence in O(1). A simple zero sequence is valid if ∑i=1i=jsi≤(m−1)j∀j≤n−1.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 n−1 nodes so that there is room for adding the nth 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:

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.

… excerpt ends here. Continue reading the full article.

Illustrations

M-ary tree: An example of a m-ary tree with m=5
An example of a m-ary tree with m=5
M-ary tree: An example of conversion of a m-ary tree with m=6 to a binary tree.
An example of conversion of a m-ary tree with m=6 to a binary tree.
M-ary tree: An example of storing a m-ary tree with m=3 in an array
An example of storing a m-ary tree with m=3 in an array
M-ary tree: Pointer-based implementation of m-ary tree where m=4.
Pointer-based implementation of m-ary tree where m=4.
M-ary tree: 3-ary tree with bit sequence of 1110000100010001000 and Simple Zero Sequence of 004433
3-ary tree with bit sequence of 1110000100010001000 and Simple Zero Sequence of 004433

Worked examples

Example 1 — a first encounter with M-ary tree

Start with the simplest possible case. Write down what M-ary tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to M-ary tree before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about M-ary tree ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of M-ary tree

In research
M-ary tree appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses M-ary tree in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
M-ary tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics Trees (data structures), Trees (graph theory), so understanding it makes those chapters shorter.
In everyday life
Look for M-ary tree outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “M-ary tree” →

How to study M-ary tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what M-ary tree means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain M-ary tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is M-ary tree in simple terms?

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…

Why does M-ary tree matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study M-ary tree?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on M-ary tree.

Tags

  • Trees (data structures)
  • Trees (graph theory)

Keep exploring