Solved

# Looking for C++ Simple N-ary Tree Examples - Very Stuck!

Posted on 2008-10-11

Hi Experts,

I'm trying to find an example of an n-ary tree for void data types. I understand the basics but I am struggling with implementation, so an example of a simple n-ary tree would be invaluable (with view to writing an XML data retrieval class).

I understand that a node in the tree will need a pointer to a parent, an array of pointers to child nodes, and a void pointer to data on the node.

I don't quite understand how I add a node, because how do I know where I am in the tree? I've written a similar queue/stack/list class that adds nodes to a given position (front, end, midpoint) of the doubly linked list, but I'm conceptually stuck with how I refer to a particular node on the tree.

If anyone could point me to some example code it would really help. As I will have to maintain and expand this code, I have to understand it fully, which is why I am not using an off-the-shelf class. (Please note that I am not a student trying to skip homework, this is work for me...).

I've scoured the internet but each example seems to say "...and you can take it from here" and it's the implementation that I am having difficulty with.

Help would be very greatly appreciated. [The List/Queue/Stack class is available to anyone who wants it, it's fast and has been used in real apps, and does no copying so it's quite fast]

For reference, this is C++ in Visual C++ 6, on Windows Vista.

Thanks in advance.