Home » U++ Library support » U++ Core » NEW: Tree<T> container
Re: NEW: Tree<T> container [message #27717 is a reply to message #27707] |
Fri, 30 July 2010 13:31   |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
You know that won't work at all right? You've completely mixed up the data type and the storage type, there's mis-casts from Tree<T> to T all over the place.
I think I can see what you're trying to do, but this would work better IMO:
template <class T>
class Tree : public One<T>, public Moveable<Tree<T> >
{
private:
Tree<T> * parent;
Vector<Tree<T> > children;
public:
Tree<T> *GetParent() { return parent; }
const Tree<T> *GetParent() const { return parent; }
Tree<T> *GetRoot() { return Tree<T> *p = this; while (p->GetParent()) p = GetParent(); return p; }
const Tree<T> *GetRoot() const { return const Tree<T> *p = this; while (p->GetParent()) p = GetParent(); return p; }
// All the necessary add/insert stuff goes here
Tree<T>& operator[](int i) { return children[i]; }
const Tree<T>& operator[](int i) const { return children[i]; }
};
You'll have to add as much of the Array interface as required but there isn't any way round this. If you inherit from Array then you end up having to do the same for One<> anyway and I think it makes more sense this way personally.
I've also attached a templated tree implementation that I wrote a while ago. It uses a different approach and has some different problems (notably the traversal algortihms are broken) but may be interesting to you.
And Link<T, N> isn't suitable for trees IMO, its really for multiply linked lists (such as an indexed database).
-
Attachment: Tree.zip
(Size: 2.17KB, Downloaded 205 times)
[Updated on: Fri, 30 July 2010 13:43] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Tue Aug 26 19:27:03 CEST 2025
Total time taken to generate the page: 0.05492 seconds
|