473,785 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linked list/Tree using iterators?

User-Agent: OSXnews 2.081
Xref: number1.nntp.dc a.giganews.com comp.lang.c++:8 17435
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.

So, I have a class Node,

class Node
{
// some stuff ctors, dtors, etc, BUT NO Default ctor;

Node &parent_node ; // I want to avoid copying. ??
vector< list<Node>::ite rator child_nodes; // ??
};

class Tree
{
//again ctors, dtors, and some other stuff;
list< Nodetreeofnodes ;
};

So are the above declarations correct? I don't have a default ctor for
the Node class so am not sure if above would work.
One more question(if above WORKS):

can is do something like this:

list<Node &treeofnodes ; //??

The advantage that I see in this is that I won't have to waste time
copying an instance of class Node when I insert it into the list..
If none of this is supposed to work, then I guess I have no other
option but to resort to pointers and use
class Node
{
// some stuff ctors, dtors, etc, BUT NO Default ctor;

Node *parent_node; //
vector< Node * child_nodes; //
};

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node*treeofnode s;
};

thanks,
--a.
Aug 20 '06 #1
1 3248
Amit Bhatia wrote:
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.

So, I have a class Node,

class Node
{
// some stuff ctors, dtors, etc, BUT NO Default ctor;

Node &parent_node ; // I want to avoid copying. ??
vector< list<Node>::ite rator child_nodes; // ??
};

class Tree
{
//again ctors, dtors, and some other stuff;
list< Nodetreeofnodes ;
I think it would be easier if you just make a simple assumption: a Tree
is represented by its root Node (and every node of a Tree is just another
Tree, only smaller), so in that sense you ought to make your 'Node'
a special (extended) form of Tree, the one that has a parent. At least
I probably would make it that way (if you have to use a reference for
the parent). Otherwise, you could just make 'parent_node' a pointer to
a node and keep it null for the root node of a Tree.

Another note: with your current scheme a subtree cannot be pruned/grafted
on another tree.
};

So are the above declarations correct? I don't have a default ctor for
the Node class so am not sure if above would work.
It doesn't need a default c-tor. Besides, it probably can't have it,
since your 'Node' objects cannot move, once attached to a tree.
One more question(if above WORKS):

can is do something like this:

list<Node &treeofnodes ; //??
No, you cannot have a list of references. References are not objects.
The advantage that I see in this is that I won't have to waste time
copying an instance of class Node when I insert it into the list..
If none of this is supposed to work, then I guess I have no other
option but to resort to pointers and use
class Node
{
// some stuff ctors, dtors, etc, BUT NO Default ctor;

Node *parent_node; //
vector< Node * child_nodes; //
};

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node*treeofnode s;
};
In that case you just need to do

class Tree : public Node {
public:
Tree() : Node(NULL) {} // no parent
};

(assuming you have a c-tor that attaches a Node to another one).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 21 '06 #2

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

Similar topics

5
7958
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value' => 'aaa') , array( 'id' => 'B', 'parent_id' => 'A', 'value' => 'bbb') , array( 'id' => 'C', 'parent_id' => 'B', 'value' => 'ccc') , array( 'id' => 'D', 'parent_id' => 'A', 'value' => 'ddd')
9
2553
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences, but try to use an iterator of a list that you're mutating and watch it all fall to pieces. That is, if you change the length of a section of the list through which the iterator has already passed, it will lose track of where it is. I think...
3
2872
by: Pushkar Pradhan | last post by:
I need to pass the STL linked list to a function, this function should modify the linked list. So do I need to pass it by address, this is how I do it: void qhull(PARTICLE S, int len, list<PARTICLE> &hull, list<PARTICLE>::iterator &iter1, list<PARTICLE>::iterator &iter2, PARTICLE a, PARTICLE b, int rank, int numtasks) I think that's working but I have another problem: I need to pass two
14
5633
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
5
1810
by: Shane | last post by:
Thanks in advance for the help. I'm new to the STL and havig a bit of trouble figuring out the whole iterator thing. I am using the <list> template and I can insert elements into the list just fine. I just can't get them out. It's like the roach motel! (uh, you can go in, but don't come out... anyway...) Actually, I have been able to get some help from http://www.sgi.com/tech/stl/List.html
12
3954
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
23
3865
by: Just Another Victim of the Ambient Morality | last post by:
I'm looking for a linked list implementation. Something iterable with constant time insertion anywhere in the list. I was wondering if deque() is the class to use or if there's something else. Is there? Thank you...
4
2941
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I have rewritten myself is the getword function. I am using it because there are many concepts involved in this program that I want to understand like: 1.) degree of efficiency of this program as compared to sort using hash-table based approach. 2.) Keeping the program well structured so...
0
2205
by: mac | last post by:
I found that with memory allocating techniques used nowadays (addresses alignment, eg. on 32bit machines) one can detect loops in a tree structure very fast, without using extra memory. This is due to a possibility of storing extra information in unused bits of the tree pointers (it works for linked lists too). One can say it is dangerous or obvious, but I haven't seen it anywhere, and maybe it will be useful for someone. The procedure...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10085
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
9947
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...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.