Hey guys :)

I'm working a project but I'm not quite sure how to start it.

I'd like to be able to create a tree by typing in a line of commands - such as this "dudduud". D would stand for down, creating the first node, U would stand for up, taking you back to the root, down again to create the second child node, down again from there to create another child node, then up, up to the root, and down once more to create a 3rd node.

What would be the best way to implement this? I just need an idea to get me started.

Thanks!

Probably you have a wonderful talent to work on the project before it's started. It's a pity I don't know how to do that...

Well, define a class with back (to parent object) link (up pointer) and std::vector of down links. After that create root node then start to scan a line. Create new node and push_back a pointer to it into the current node down vector on 'd', move to the parent node via up link on 'u'.

It's so simple in C++. Start your project as soon as possible then present your code...

Thank you for the quick reply.

I've been thinking about the project quite a bit before coding and I just have a few questions...

How will my program know what level of the tree, or better yet, where the node is placed in the tree? I think I understand it with a binary, but with an n-ary I can't quite grip the idea.

Would a linked list be an acceptable route to doing this? Or is vector superior? I'm not too familiar with vectors.

"create root node then start to scan a line"

I don't fully understand what you mean by this, scan what?

To scan a line "dudduud": get the 1st character ('d') and process it then get the 2nd character ('u') and process it then... and so on...

I think I understand it with a binary, but with an n-ary I can't quite grip the idea

What a difference between 2-ary and n-ary trees in this aspect? Add int level member of a node class if you wish...

Of course, you can use linked list for down links. I think, std::vector<Node*> better but std::list<Node*> (or self-made list of pointers) is a suitable data structure too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.