tags:

views:

6529

answers:

6

I'm wondering if anyone can recommend a good C++ tree implementation, hopefully one that is stl compatible if at all possible.

For the record, I've written tree algorithms many times before, and I know it can be fun, but I want to be pragmatic and lazy if at all possible. So an actual link to a working solution is the goal here.

Note: I'm looking for a generic tree, not a balanced tree or a map/set, the structure itself and the connectivity of the tree is important in this case, not only the data within. So each branch needs to be able to hold arbitrary amounts of data, and each branch should be separately iterateable.

+3  A: 

I am going to suggest using std::map instead of a tree.

The complexity characteristics of a tree are:

Insert:       O(ln(n))
Removal:  O(ln(n))
Find:         O(ln(n))

These are the same characteristics the std::map guarantees.
Thus as a result most implementations of std::map use a tree (Red-Black Tree) underneath the covers (though technically this is not required).

Martin York
I think you mean that most std::map implementations use a tree underneath the covers. Specifically, many (most?) use a Red-Black tree.
Michael Burr
I though that is what I wrote? But I should note the Red-Black part.
Martin York
The nice thing about a (generic)tree is that you can add n children to a branch, and apply operations onto different branches, but std::map abstracts all that away, or uses red-black (child n=2), and abstracts away the structure into a flat interface.
Robert Gould
Being able to operate on a single branch is the whole point or a tree. Examples could be filesystem folders, data layout, scene graphs, language analysis, xml-like structured data, and probably many more cases I can't think of right now.
Robert Gould
But for flat data, yes you are right, a map will do nicely, however personally I prefer a hash for flat data
Robert Gould
+7  A: 

Take a look at this.

The tree.hh library for C++ provides an STL-like container class for n-ary trees, templated over the data stored at the nodes. Various types of iterators are provided (post-order, pre-order, and others). Where possible the access methods are compatible with the STL or alternative algorithms are available.

HTH

Doesn't look bad, but its GNU. That is a very heavy price to pay for a tree
Robert Gould
First of all, this project is not part of GNU. Second, if you talk about the GPL, this is not necessarily any price to pay. It depends on if one wants to publish the code and under what terms.
ypnos
He mentions on the homepage to contact him if the GPL doesn't suit. What is a fair price to pay for a tree? How much work/time does it save your project?
gnibbler
+1  A: 

If you don't have (key, value) pairs, but simply keys, use std::set. That uses the same Red-Black tree as std::map.

Denes Tarjan
A: 

Well good on you for wanting to re-use rather than re-write. I opened a project yesterday, saw a directory called "Collections" (first warning) and opened that to find BinaryTree.cs, SortedBinaryTree.cs, RedBlackTree.cs... all of course re-implementations.

Even better, however, was BufferedEnumerator.cs. It bears this description:

    /// This enumerator is designed to encapsulate a forward only 
    /// iterator to provide limited rewind functionality.
    /// It is only possible to rewind the the last restore point.

The mind truly boggles.

endian
LOL, the general tragedy is something I've seen many times. But BufferedEnumator does deserve special mention :)
Robert Gould
+2  A: 

Ok folks, I found another tree library, but haven't tried it out yet

Robert Gould
+6  A: 

I don't know about your requirements, but wouldn't you be better off with a graph (implementations for example in Boost Graph (http://www.boost.org/doc/libs/1_36_0/libs/graph/doc/index.html) if you're interested mostly in the structure and not so much in tree-specific benefits like speed through balancing? You can 'emulate' a tree through a graph, and maybe it'll be (conceptually) closer to what you're looking for.

Roel
Yes you are right, and I just came back from looking at Boost::Graph. It seems suitable to my needs (structure), however the documentation is huge! Would've been nice if there was a simpler version of a tree though.
Robert Gould
True, the documentation is daunting. If you are really going to base a serious part of your project on this, I suggest you get the boost graph book. It's clearly written (for as far as a book on this topic can be clear) and fairly comprehensive. Most importantly, it'll get you up to speed quickly.
Roel