# Thread: Help in implementing N-ary trees or general trees in C.

1. ## Help in implementing N-ary trees or general trees in C.

Hi, I am new to trees in C. I am implementing an n-ary tree in C. I have implemented binary trees. But I am facing problems in implementing n-ary trees. The implementation is very easy for binary trees is easy for me but the implementation of n-ary trees are very confusing to me. So it would be very helpful if someone gives me some hints or code snippets or resources or explanation or if someone has a lot of time then perhaps a tutorial which would also be helpful to other newcomers.

2. For an "N-ary" tree to function properly, its leaves must be able to be distinguished from each other in at least N ways. Once you understand how to do that, making nodes that can hold N pointers each is piece of cake.

For example, in a binary tree, we sort using two states, either smaller or not smaller (or variations of this). A ternary tree could be configured to sort for lesser, equal or greater. Higher trees such as quad tree and oct tree are sometimes used with spatial dimensions, because they help subdivide the space into smaller and smaller blocks. A 2D vector, because it consists of two coordinates, each of which can differ in two or three ways, can itself differ from another 2D vector in 4 to 8 ways. That's one way of looking at it...

3. Perhaps you can start with turning a binary tree
Code:
```struct node {
int data;
struct node *left;
struct node *right;
};```
Into a 2-way tree
Code:
```struct node {
int data;
struct node children[2];
};```
I mean, once you figure out how to choose a children[i], the rest is the same.

4. Thanks, I got the idea

Popular pages Recent additions