implementing a generic tree

C

Christopher

I hate to start from scratch as the need for it isn't as great as the
work it would take. But, it sure would be nice if I had some data
structure that represented a generic tree.

I heard using boost's graph library might be an option. Any resources
on doing so to implement a tree around? Or any suggestions on an
alternative?
 
V

Victor Bazarov

Christopher said:
I hate to start from scratch as the need for it isn't as great as the
work it would take. But, it sure would be nice if I had some data
structure that represented a generic tree.

I heard using boost's graph library might be an option. Any resources
on doing so to implement a tree around? Or any suggestions on an
alternative?

If your tree is so generic, and doesn't require balancing or some
other dynamic adjustment upon insertion, then it takes about an hour
to type up. More time to decide what to name member functions than
to implement them...

template<class T>
class Tree {
Tree* parent;
Tree* prev_sibling;
Tree* next_sibling;
Tree* first_child;
T data;
public:
Tree() : parent(0), prev_sibling(0),
next_sibling(0), first_child(0), data(T()) {}
...
};

Can't you just take a book on data structures and swipe some tree
from there and templatize it?

V
 
M

Martin York

I hate to start from scratch as the need for it isn't as great as the
work it would take. But, it sure would be nice if I had some data
structure that represented a generic tree.

I heard using boost's graph library might be an option. Any resources
on doing so to implement a tree around? Or any suggestions on an
alternative?


Do you really want a tree.
Or do you just want the access characteristics of a tree.

If the latter than you can store your data in std::map<> or std::set<>
depending on your usage model. As the standard garantees that access
to these containers have certain characteristics (that happen to be
very similar to the access characteristics of a tree). Just a thought.

If you give us more detail about what you need to do then we can give
some more advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top