473,804 Members | 3,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating tree of objects w/o using pointers.

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

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

Node &parent_node ; // Could use pointer here but want to avoid it if
possible. vector< Node & child_nodes; //Could use pointer here but
want to avoid it if possible.};

In tree.h

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &treeofnodes ;
};
I don't want to create duplicate copies of nodes, and want everything to
point correctly to elements in the global list treeofnodes.
I am not sure though if above can be done, as there needs to be some
kind of default ctor required and I am using references also..
thanks,
--a.

--
Aug 20 '06 #1
4 2254
User-Agent: OSXnews 2.081
Xref: number1.nntp.dc a.giganews.com comp.lang.c++:8 17425
I just figured that if I also want to delete elements from the tree I
am going to need something like pointers and storing references as below
will not work probably. (??)
However, can I use iterators instead? And then when I want to delete
some of them can I just pass the iterator to the erase function of the list
w/o any problems?

Amit Bhatia<am****** **********@nosp am.gmail.comwro te:
>
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:
In node.h

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

Node &parent_node ; // Could use pointer here but want to avoid it if
possible. vector< Node & child_nodes; //Could use pointer here but
want to avoid it if possible.};

In tree.h

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &treeofnodes ;
};
I don't want to create duplicate copies of nodes, and want everything to
point correctly to elements in the global list treeofnodes.
I am not sure though if above can be done, as there needs to be some
kind of default ctor required and I am using references also..
thanks,
--a.

--



--
Aug 20 '06 #2
Note: "Node &" is not a pointer type define instead it stands for reference
variable.
"Node *" is a pointer type define.

"Amit Bhatia" <am************ ****@nospam.gma il.comдÈëÏûϢРÂÎÅ:ec********* *@zinnia.noc.uc la.edu...
>
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:
In node.h

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

Node &parent_node ; // Could use pointer here but want to avoid it if
possible. vector< Node & child_nodes; //Could use pointer here but
want to avoid it if possible.};

In tree.h

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &treeofnodes ;
};
I don't want to create duplicate copies of nodes, and want everything to
point correctly to elements in the global list treeofnodes.
I am not sure though if above can be done, as there needs to be some
kind of default ctor required and I am using references also..
thanks,
--a.

--


Aug 20 '06 #3

"Amit Bhatia" <am************ ****@nospam.gma il.comskrev i
meddelandet news:ec******** **@zinnia.noc.u cla.edu...
>
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:
In node.h

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

Node &parent_node ; // Could use pointer here but want to avoid it if
possible. vector< Node & child_nodes; //Could use pointer here but
want to avoid it if possible.};
You cannot have a vector of references. The vector must be able to
copy and assign to the elements. That doesn't work for references.
>
In tree.h

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &treeofnodes ;
};
I don't want to create duplicate copies of nodes,
Why? Are they *extremely* expensive to copy?
and want everything to
point correctly to elements in the global list treeofnodes.
I am not sure though if above can be done, as there needs to be some
kind of default ctor required and I am using references also..
thanks,
--a.
I think you try to do this more complicated that it actually has to
be. The general rule is to go ahead and use the standard containers,
until you see that your program runs way too slow. Then it is time to
look into exactly where it takes too much time. Most often you will
never have to.

If you store your Nodes by value in a vector, or a list, the container
will take care of all the problems of ownership and destruction. Using
prewritten library code saves you a lot of work.
Bo Persson
Aug 20 '06 #4
User-Agent: OSXnews 2.081
Xref: number1.nntp.dc a.giganews.com comp.lang.c++:8 17487
"Bo Persson" <bo*@gmb.dkwrot e:
>
"Amit Bhatia" <am************ ****@nospam.gma il.comskrev i
meddelandet news:ec******** **@zinnia.noc.u cla.edu...
>>
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:
In node.h

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

Node &parent_node ; // Could use pointer here but want to avoid it if
possible. vector< Node & child_nodes; //Could use pointer here but
want to avoid it if possible.};

You cannot have a vector of references. The vector must be able to
copy and assign to the elements. That doesn't work for references.
I guess then that I should use pointers. so it would be something like

Node{
//; ;
Node *parent_node;
list<Node*child _nodes;
};

Tree{
//; ;
list<Node *treeofnodes;
};

OR (NOT SURE IF THIS WOULD WORK?)

Node{
//; ;
Node *parent_node;
list<Node*child _nodes;
};

Tree{
//; ;
list<Nodetreeof nodes;
};
But here is another potential problem that I don't want. When I want to
delete a particular Node, I can just use free on its pointer or
something. But, if I want this pointer entry to be also deleted from the list
"treeofnode s" in tree class above, there doesn't seem to be an easy way to
do it. (?)
However, I am just wondering if I could use iterators in place of
pointers above. Stl Lists do have function called erase which takes an
iterator argument to remove it from the list. And iterators are not
invalidated if I remove some other ones from the list.So,

Node{
//; ;
Node &parent;
list< list<Node>:: iterator child_nodes;
};

Tree{
//; ;
list<Node treeofnodes;
};

But the question is: is this allowed?
>>
In tree.h

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &treeofnodes ;
};
I don't want to create duplicate copies of nodes,

Why? Are they *extremely* expensive to copy?
>and want everything to
point correctly to elements in the global list treeofnodes.
I am not sure though if above can be done, as there needs to be some
kind of default ctor required and I am using references also..
thanks,
--a.

I think you try to do this more complicated that it actually has to
be. The general rule is to go ahead and use the standard containers,
until you see that your program runs way too slow. Then it is time to
look into exactly where it takes too much time. Most often you will
never have to.

If you store your Nodes by value in a vector, or a list, the container
will take care of all the problems of ownership and destruction. Using
prewritten library code saves you a lot of work.
Bo Persson



--
Aug 20 '06 #5

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

Similar topics

14
5636
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
4090
by: nandor.sieben | last post by:
I'm working on a project analyzing a game and I am in need of a tree template library. I found a few implementations on the web but all of them were too complicated for me to understand. I wrote something on my own. I used an STL vector to store children of a node. My major concern is memory leak. I am not sure if doing a pop_back() to cut a tree branch would result in memory leak. I would appreciate some advise on this. Also is there a...
1
232
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes = new Airplane("N12345"); Actually, I create an array of Airplane-pointers first and then create the
1
3251
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.
31
3210
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are some of the most powerful programming tools I have found. Often times as my program grows so do my objects. Often times I look back and see that my objects could be broken down int several smaller more re-usable module pieces of code. Is it a...
8
1706
by: Travis | last post by:
I have a tree of pointers and I need the ability to search the tree for a specific node. The problem is I can't provide that pointer for the node I'm looking for as criteria. The only critieria I can provide is a node that is the same and will == true. So naturally this would work if my tree was not of pointers but of objects. Since it's of pointers, it compares my node criteria against every node in the tree and the memory addresses...
1
1468
by: xian83 | last post by:
I am trying all day to make some things straight but nothing helps... Me and my collegue have built a c++ tree of a class VO. I tend to refer to the structure as a tree and not a simple list because of its complexity. We have reached the point where a button generates an event that returns the place of the node in the tree and we need to delete that node and everything below it...i think that this is pretty much a standard procedure. ...
0
2206
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...
2
2664
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
9705
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
10567
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...
0
10323
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
10310
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
9138
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
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.