473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Iterator performance on associative containers

I have implemented a red-black tree based on the description in Introduction
to Algorithms Cormen section 13. But I would like to make all iterator
operations to take O(1) time in worst case. Is this even possible and if so
does anyone know of any articles websites dealing with this optimzation?
Jun 3 '07 #1
3 1547
On 2007-06-03 22:12, Johs wrote:
I have implemented a red-black tree based on the description in Introduction
to Algorithms Cormen section 13. But I would like to make all iterator
operations to take O(1) time in worst case. Is this even possible and if so
does anyone know of any articles websites dealing with this optimzation?
This is not a C++ question so if you have more like this one try some
other group, perhaps comp.programming.

While I'm not 100% sure I don't think that you can make all iterator
operation work in O(1) (but you have not defined which operations you
want on an iterator). The field of data-structures and algorithms is
quite well charted so what you read in the books is usually "state of
the art" (and usually figured out a number of years ago).

--
Erik Wikström
Jun 3 '07 #2
On Jun 3, 11:33 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-06-03 22:12, Johs wrote:
I have implemented a red-black tree based on the description in Introduction
to Algorithms Cormen section 13. But I would like to make all iterator
operations to take O(1) time in worst case. Is this even possible and if so
does anyone know of any articles websites dealing with this optimzation?
This is not a C++ question so if you have more like this one try some
other group, perhaps comp.programming.
While I'm not 100% sure I don't think that you can make all iterator
operation work in O(1) (but you have not defined which operations you
want on an iterator).
Sure you can, at the cost of an additional pointer or two per
node, and some extra time at each insertion. Just keep all the
nodes in a double linked list.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 3 '07 #3
Johs wrote:
I have implemented a red-black tree based on the description in Introduction
to Algorithms Cormen section 13. But I would like to make all iterator
operations to take O(1) time in worst case. Is this even possible and if so
does anyone know of any articles websites dealing with this optimzation?
I suppose you could have two additional pointers in the tree nodes:
One pointer for the previous node and another for the next node (in the
traversal order). You'll have to figure out how to update these pointers
when nodes are added and removed from the tree, but I'm confident that
it can be done in log n time.

OTOH one could ask if you really need this. While one single iterator
increment or decrement may require O(log n) steps, the total number of
steps for a full traversal is still O(n) (not even amortized O(n), but
pure O(n)). So you should ask yourself if random single increments /
decrements are so frequent in your program that it justifies the
additional memory needed for the pointers and the overhead of keeping
them updated when the tree changes.
Jun 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Harald Grossauer | last post by:
Usually STL containers have something like "iterator container<>::erase(iterator)" which erases the element the iterator points to and returns another valid iterator. Set does not have this,...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
13
by: Chameleon | last post by:
I am programming in C++ for 4 years, so I am not a newbie but I am not a very experienced programmer on standard C++ libary. My question is this: Can you tell me one case which an iterator is...
1
by: yonil | last post by:
I hope this is the correct group for this... I'm currently implementing the TR1 associative containers according to specification found in...
22
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
windows xp, visual studio 2005 ---------------------------------------------------------------------------------------------------------------------------------- #include <iostream> #include <map>...
5
by: desktop | last post by:
set, map, multiset and multimap are all associative containers. But what does the word "associative" have to do with these 4 containers?
3
by: subramanian100in | last post by:
I am copying the following lines as it is, from Stanley Lippman's C++ Primer 4th edition, page 418(First paragraph). It says: "Although the map and set types provide bidirectional iterators, we...
11
by: barcaroller | last post by:
I've noticed that neither container.begin() nor container.end() return an error when the container is empty, so I get a nasty segfault when I dereference the iterator. Do I have to check if the...
8
by: Bo Yang | last post by:
Hi, Today, I make some test on the C++ STL iterators of set containers with GNU g++ compiler. The code is: #include <set> #include <iostream> using namespace std; int main(int argc, char...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.