An n-ary tree is similar to a binary tree. The difference is that with a Binary Tree node, you have at most 2 children. With an n-ary tree, you could have any number of children, based upon the design of the data structure.
The difference is, while some languages/libraries will give you a ready made binary tree, an n-ary tree often has to be custom designed.
Consider a data structure where you have a character, and 26 possible children characters, one for each letter of the alphabet. This would allow you to quickly search for names, because you get rid of 96% of all possibly combinations with each letter allowing you to quickly find the name.
A basic data structure would look something like this:
public class charNode {
char letter;
char[26] children;
boolean terminal;
}
Here we spend a little bit of memory space on potential letters, while quickly allowing us to find children using a search procedure, or even manually finding every possible combination. Choosing memory efficiency or speed, is a choice we will often have to make.
Of course, we don’t have to limit ourselves to alphabetic characters. We can have numbers to allow us to quickly find numeric data, if it’s not sequential numbers, or we could have other values as well.
Consider something like a tree structure where you have a movie, and then the children nodes are of all the actors who worked in that movie. Then they have children nodes of other movies they have been in…which in turn have children nodes of actors… As you go about building this “crazy entertainment tree” you realize you just built IMDB.
Or you could have a company organizational chart, or any number of other things. Being able to find people/items in these trees becomes very important, and is why we will next look at breadth first and depth first searching in our algorithms.
n-ary Tree Structures was originally found on Access 2 Learn