473,386 Members | 1,699 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,386 software developers and data experts.

Sets and level order iteration.

Greetings All.

One of the usages that I need is accessing the contents of the container
that I am using in level order( which would mean breadth- first search
approach for a binary tree) as compared to the default depth-first search
approach that happens when we traverse using the default iterator for a set
or a map, when using either the less/greater comparator function object.

Would anyone know, how I can access the elements of a set or a map in a
level order? I tried specifying the comparator object that I want, but
having it sorted as well as getting what I want (level order access) is not
working out.
Further more, if I want all level order elements of that container(either
set or map) maintained via a linked list for traversing, is there an easy
way to get it done ? This would of course mean deriving a custom container
from a Set and adding another pointer that points to a peer Node. I know
there is a _Tree template class(which is a red black tree), the class
template which is used for deriving Map and Set . Maybe I can use that
directly, but I am still not sure how I can still get level order access
using that.

Thanks.
Jul 23 '05 #1
3 2278
Amit wrote:
Greetings All.

One of the usages that I need is accessing the contents of the container
that I am using in level order( which would mean breadth- first search
approach for a binary tree) as compared to the default depth-first search
approach that happens when we traverse using the default iterator for a
set or a map, when using either the less/greater comparator function
object.
The standard containers are abstractions. They are designed so that
implementation details (like whether there is an underlying binary tree) do
not leak. As far as std::set is concerned, there is no depth-first or
breadth-first ordering of the element.
Would anyone know, how I can access the elements of a set or a map in a
level order? I tried specifying the comparator object that I want, but
having it sorted as well as getting what I want (level order access) is
not working out.
Even if the implementation happens to use a tree, it will be hidden from
you, private, and inaccessible.
Further more, if I want all level order elements of that container(either
set or map) maintained via a linked list for traversing, is there an easy
way to get it done ? This would of course mean deriving a custom container
from a Set and adding another pointer that points to a peer Node. I know
there is a _Tree template class(which is a red black tree), the class
template which is used for deriving Map and Set . Maybe I can use that
directly, but I am still not sure how I can still get level order access
using that.


There might be a _Tree class provided by the vendor of the standard library
that you happen to use. Using that class would, however, be highly
non-portable. You are better off running your own tree.

On a related note: if you consider using the _Tree class from your STL
implementation, keep in mind that you have no controll over the insertion
and balancing algorithm that is used. Very likely the level-order of the
elements is completely unrelated to your problem. For that reason, I
actually doubt that you really need to access the particular level-order
that just happens to be generated by that mysterious _Tree class.
Best

Kai-Uwe Bux
Jul 23 '05 #2
> One of the usages that I need is accessing the contents of the container
that I am using in level order( which would mean breadth- first search
approach for a binary tree) as compared to the default depth-first search
approach that happens when we traverse using the default iterator for a
set
or a map, when using either the less/greater comparator function object.

Would anyone know, how I can access the elements of a set or a map in a
level order?
You can't. There is absolutely no guarantee that a binary tree is used, a
different data structure might be used by set/map.
I tried specifying the comparator object that I want, but
having it sorted as well as getting what I want (level order access) is
not
working out.
Further more, if I want all level order elements of that container(either
set or map) maintained via a linked list for traversing, is there an easy
way to get it done?
No.
This would of course mean deriving a custom container
from a Set and adding another pointer that points to a peer Node. I know
there is a _Tree template class(which is a red black tree), the class
template which is used for deriving Map and Set.


That is just implementation detail.
As said there is no guarantee that map/set is implemented as you say.
All that matter fro mthe point of view of standard C++ is that the container
fulfills its pre/post conditions for each operation and complexity
guarantees. You are assuiming too much in what goes on under the hood.

Stephen Howe
Jul 23 '05 #3

"Amit" <am***********@intel.com> wrote in message
news:d6**********@news01.intel.com...
Greetings All.

One of the usages that I need is accessing the contents of the container
that I am using in level order( which would mean breadth- first search
approach for a binary tree) as compared to the default depth-first search
approach that happens when we traverse using the default iterator for a
set
or a map, when using either the less/greater comparator function object.


....

What is it that your really trying to accomplish in the end. Your describing
a particular (non)solution, rather than the eventual goal. That said, as
others have responded, use a tree container. There was some recent activity
concerning tree's on the boost development mailing list. See www.boost.org.
In the short term, a tree is-a graph. So look at
http://www.boost.org/libs/graph/doc/..._contents.html.

Jeff Flinn
Jul 23 '05 #4

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

Similar topics

21
by: Raymond Hettinger | last post by:
I've gotten lots of feedback on the itertools module but have not heard a peep about the new sets module. * Are you overjoyed/outraged by the choice of | and & as set operators (instead of + and...
7
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
1
by: Doug | last post by:
I need to compare two "address" structures within a document, and perform some action if they are not equal. The XML document is a purchase order, with an address at both the header and line...
3
by: Andy Fish | last post by:
Hi, I just found this template in someone else's xslt (it's Microsoft's "word2html" stylesheet to convert WordProcessingML to HTML) <xsl:template match="WX:sect"> <xsl:variable...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
3
by: Krish | last post by:
Hello, This is an issue that I have encountered several times but have just used a workaround to solve. What is the accepted practice for breaking out of nested loops? For example: while(...
11
by: John Salerno | last post by:
I'd like to compare the values in two different sets to test if any of the positions in either set share the same value (e.g., if the third element of each set is an 'a', then the test fails). I...
5
by: Alan Isaac | last post by:
This is an attempt to synthesize Bill and Carsten's proposals. (I'm changing the subject line to better match the topic.) http://docs.python.org/lib/typesmapping.html: for footnote (3) Keys...
6
by: happyhondje | last post by:
Hello everyone, I've got a little issue, both programming and performance-wise. I have a set, containing objects that refer to other sets. For example, in a simple notation: (<a, b, c>, <d, e>)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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,...

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.