What's a good and stable C++ tree implementation?

后端 未结 6 1505
孤独总比滥情好
孤独总比滥情好 2020-12-04 15:29

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 a

相关标签:
6条回答
  • 2020-12-04 15:59

    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).

    0 讨论(0)
  • 2020-12-04 16:01

    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

    0 讨论(0)
  • 2020-12-04 16:02

    Let suppose the question is about balanced (in some form, mostly red black tree) binary trees, even if it is not the case.

    Balanced binaries trees, like vector, allow to manage some ordering of elements without any need of key (like by inserting elements anywhere in vector), but :

    • With optimal O(log(n)) or better complexity for all the modification of one element (add/remove at begin, end and before & after any iterator)
    • With persistance of iterators thru any modifications except direct destruction of the element pointed by the iterator.

    Optionally one may support access by index like in vector (with a cost of one size_t by element), with O(log(n)) complexity. If used, iterators will be random.

    Optionally order can be enforced by some comparison func, but persistence of iterators allow to use non repeatable comparison scheme (ex: arbitrary car lanes change during traffic jam).

    In practice, balanced binary tree have interface of vector, list, double linked list, map, multimap, deque, queue, priority_queue... with attaining theoretic optimal O(log(n)) complexity for all single element operations.

    <sarcastic> this is probably why c++ stl does not propose it </sarcastic>

    Individuals may not implement general balanced tree by themselves, due to the difficulties to get correct management of balancing, especially during element extraction.

    There is no widely available implementation of balanced binary tree because the state of the art red black tree (at this time the best type of balanced tree due to fixed number of costly tree reorganizations during remove) know implementation, slavishly copied by every implementers’ from the initial code of the structure inventor, does not allow iterator persistency. It is probably the reason of the absence of fully functionnal tree template.

    0 讨论(0)
  • 2020-12-04 16:04

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

    0 讨论(0)
  • 2020-12-04 16:19

    Ok folks, I found another tree library; stlplus.ntree. But haven't tried it out yet.

    0 讨论(0)
  • 2020-12-04 16:20

    I don't know about your requirements, but wouldn't you be better off with a graph (implementations for example in Boost Graph) 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.

    0 讨论(0)
提交回复
热议问题