473,791 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing a tree iterator

Suppose I have a class Heap that represents a binary heap in an array. Then
suppose that I have another class called Heap::iterator that moves down the
heap to the left or the right. Here is the code from the header file
describing these two types:

--- CODE ---

class Heap
{
std::vector<int > m_vec;
public:
class iterator {
protected:
Heap *m_heap;
int m_position;
public:
enum Direction {LEFT, RIGHT};

iterator(Heap &myHeap, int position = 0);
iterator(const iterator &other);

int &operator *();

iterator left() const;
iterator right() const;

iterator child(const Direction &dir);
void walk(const Direction &dir);
};

Heap(int depth);

int &operator [](unsigned int index);
const int &operator [](unsigned int index) const;

iterator top();

void write(std::ostr eam &o) const;
};

--- CODE ---

This is not compilable code. How would you recommend that I implement a
const_iterator while reusing code and not just copy and pasting method
bodies?

Thanks,

JFA1
Jul 23 '05 #1
2 3928
On 2005-03-28, James Aguilar <jf**@cec.wustl .edu> wrote:
Suppose I have a class Heap that represents a binary heap in an array. Then
suppose that I have another class called Heap::iterator that moves down the
heap to the left or the right. Here is the code from the header file
describing these two types:
[snip] This is not compilable code.
Why not ? It would make it easier to address the issue if you'd say why.
How would you recommend that I implement a
const_iterator while reusing code and not just copy and pasting method
bodies?


The same way you always use when you want to write the same code for different
types: templates.

Make a templated version of an iterator, have one with non-const template
parameters and one where you use const. You may need more than one template
argument. Then make iterator and const_iterator typedefs. e.g. iterator
could be iter<T,T*> and const_iterator could be iter<T, const T*>

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2

"Donovan Rebbechi" <ab***@aol.co m> wrote in message
news:sl******** **********@pani x2.panix.com...
Why not ? It would make it easier to address the issue if you'd say why.
I didn't think it would be necessary.
The same way you always use when you want to write the same code for
different
types: templates.

Make a templated version of an iterator, have one with non-const template
parameters and one where you use const. You may need more than one
template
argument. Then make iterator and const_iterator typedefs. e.g. iterator
could be iter<T,T*> and const_iterator could be iter<T, const T*>


And it seems it wasn't! This is very helpful! Now, why didn't I think of
that . . .

- JFA1
Jul 23 '05 #3

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

Similar topics

3
14036
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) The technical requirements: 1) Store tree data in the database so that it can be extracted as a tree structure. For test purposes,
0
2444
by: Gregory Nans | last post by:
hello, i need some help to 'tree-ify' a string... for example i have strings such as : s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i need))""" and i need to parse them to have a tree representation such as :
15
2674
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
4
4937
by: Mitchel Haas | last post by:
Hello, Feeling a need for a generic tree container to supplement the available containers in the STL, I've created a generic tree container library (TCL). The library usage is very much like the containers in the STL, including iterator usage. Info on the library can be found here.. http://www.visuallandlord.com/developers_corner/open_source/tree_container_library/overview.php The library and sample code can be downloaded from this...
4
2254
by: Amit Bhatia | last post by:
User-Agent: OSXnews 2.081 Xref: number1.nntp.dca.giganews.com comp.lang.c++:817424 Hi, I have posted this post also for the thread "vector of lists" but since it is about something else (though related) I am posting it again under new thread. I want to make a tree of nodes as follows, but am not sure now if it will work:
1
3250
by: Amit Bhatia | last post by:
User-Agent: OSXnews 2.081 Xref: number1.nntp.dca.giganews.com comp.lang.c++:817435 Hi, I searched online to see if this is valid or not (since my classes are still very incomplete, I am unable to check it using compilation) but did not find anything that answers my question. I am trying to implement a tree as a doubly linked list.
9
7833
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every chess position has around 30 moves, it would mean every node of the tree would have 30 branches (on average), which in turn themselves would average about 30 branches each. I can think of a variety of ways of implementing this, including a series...
2
3822
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
2
2662
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.