Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Set implementation in STL

37 views
Skip to first unread message

MC

unread,
Aug 7, 2010, 11:05:07 PM8/7/10
to
How are sets generally implemented in STL. I have read they are
implemented as BST, are they some kind of balanced BST like Red Black
trees?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Zeljko Vrba

unread,
Aug 9, 2010, 10:30:32 AM8/9/10
to
On 2010-08-08, MC <manan....@gmail.com> wrote:
>
> How are sets generally implemented in STL. I have read they are
> implemented as BST, are they some kind of balanced BST like Red Black
> trees?
>
The standard specifies only complexity requirements: insertion and
search must
be O(log n) (in the number of elements already in the tree) worst-case.
Thus,
e.g., both red-black trees and AVL trees qualify.

Juan Pedro Bolivar Puente

unread,
Aug 9, 2010, 10:55:01 AM8/9/10
to
On 08/08/10 05:05, MC wrote:
> How are sets generally implemented in STL. I have read they are
> implemented as BST, are they some kind of balanced BST like Red Black
> trees?
>

AFAIK, yes, Red Black Trees are one of the most common implementations
for sets, maps and alikes. There are also AVL implementations out there,
that you can google for.

JP

Mathias Gaunard

unread,
Aug 9, 2010, 10:53:36 AM8/9/10
to
On Aug 8, 4:05 am, MC <manan.cho...@gmail.com> wrote:
> How are sets generally implemented in STL. I have read they are
> implemented as BST, are they some kind of balanced BST like Red Black
> trees?

It's necessarily some kind of self-balancing binary tree since that's
the only way to provide the complexity guarantees that are required.
Implementations typically use a red-black tree or an AVL tree.

Daniel T.

unread,
Aug 9, 2010, 10:28:36 PM8/9/10
to
MC <manan....@gmail.com> wrote:

> How are sets generally implemented in STL. I have read they are
> implemented as BST, are they some kind of balanced BST like Red Black
> trees?

Yes.

Vaclav Haisman

unread,
Aug 9, 2010, 10:59:27 PM8/9/10
to
MC wrote, On 8.8.2010 5:05:
> How are sets generally implemented in STL. I have read they are
> implemented as BST, are they some kind of balanced BST like Red Black
> trees?
The C++ ISO standard does not specify such implementation details, only run
time complexitis. For std::set<>, they pretty much imply some kind of search
tree. The standard library shipped with GCC uses Red-Black Tree and os does
the one shipped with VS.NET 2005.


--
VH

Michael Doubez

unread,
Aug 10, 2010, 12:20:58 AM8/10/10
to
On 8 août, 05:05, MC <manan.cho...@gmail.com> wrote:
> How are sets generally implemented in STL.

Well, it depends on ... the implementation :)

> I have read they are
> implemented as BST, are they some kind of balanced BST like Red Black
> trees?

IMO that's the obvious (I dare say intended) implementation. But
nothing prevents the implementer from providing more efficient
structures for specific specialization.

--
Michael

Joe Gottman

unread,
Aug 10, 2010, 12:29:33 AM8/10/10
to
On 8/7/2010 11:05 PM, MC wrote:
> How are sets generally implemented in STL. I have read they are
> implemented as BST, are they some kind of balanced BST like Red Black
> trees?
>
>

Every implementation I know of uses a red-black tree, although the
standard says nothing about which underlying data type to use.

Joe Gottman

0 new messages