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

Tree of pointers

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 aren't the same so
it's not found.

If I force the tree to deference the node before comparing it works
but then what if my tree was of objects, not pointers, dereferencing
and object would blow up right?

Any suggestions, solutions?

Jun 29 '07 #1
8 1691
Travis wrote:
I have a tree
Did you write it yourself or did you use some third-party library?
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.
<blank stare>
>
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 aren't the same so
it's not found.
Pardon me... WHO compares?
>
If I force the tree to deference the node before comparing it works
but then what if my tree was of objects, not pointers, dereferencing
and object would blow up right?
<blank stare>
>
Any suggestions, solutions?
I believe it's covered in FAQ 5.8.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '07 #2
It's a tree that I wrote.

The tree is templated and can store anything of NODETYPE.

When I instantiate the tree, I make each node a pointer to my custom
class.

In my custom class I have == overloaded.

When I do a search for a node I provide it a NODETYPE to find.

It doesn't find it because even though the NODETYPE I provide as
search criteria is the same as a node in the tree, they aren't
occupying the same memory so the == operator never goes true. I have
to dereference int the NODETYPE and it works fine.

My concern is the reusabilty of the tree. If someone else comes along
and makes NODETYPE some object (not a pointer), then the dereferencing
in my find routine will bomb right?

Can you elaborate a little more on how the STL handles this?

Jun 29 '07 #3
Travis wrote:
It's a tree that I wrote.

The tree is templated and can store anything of NODETYPE.

When I instantiate the tree, I make each node a pointer to my custom
class.

In my custom class I have == overloaded.

When I do a search for a node I provide it a NODETYPE to find.

It doesn't find it because even though the NODETYPE I provide as
search criteria is the same as a node in the tree, they aren't
occupying the same memory so the == operator never goes true. I have
to dereference int the NODETYPE and it works fine.

My concern is the reusabilty of the tree. If someone else comes along
and makes NODETYPE some object (not a pointer), then the dereferencing
in my find routine will bomb right?

Can you elaborate a little more on how the STL handles this?
The moral is don't store pointers in your tree. Store NODETYPE objects
instead.

An STL container that stored pointers would have exactly the same issues.

john
Jun 29 '07 #4
Travis wrote:
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 aren't the same so
it's not found.

If I force the tree to deference the node before comparing it works
but then what if my tree was of objects, not pointers, dereferencing
and object would blow up right?

Any suggestions, solutions?
If you are implementing a binary search tree, then don't do it at all
because the STL set and map containers are tree based. Just use set or
map for your container needs.

If you are implementing arbitrary tree structure, I'd suggest you check
boost::graph because a tree is a graph with a root note and leaves...

If you insist on implementing a tree as an exercise, do some research
how STL implements set and map. It'll help a lot.

Fei
Jun 29 '07 #5
Travis wrote:
It's a tree that I wrote.

The tree is templated and can store anything of NODETYPE.

When I instantiate the tree, I make each node a pointer to my custom
class.

In my custom class I have == overloaded.

When I do a search for a node I provide it a NODETYPE to find.

It doesn't find it because even though the NODETYPE I provide as
search criteria is the same as a node in the tree, they aren't
occupying the same memory so the == operator never goes true. I have
to dereference int the NODETYPE and it works fine.

My concern is the reusabilty of the tree. If someone else comes along
and makes NODETYPE some object (not a pointer), then the dereferencing
in my find routine will bomb right?

Can you elaborate a little more on how the STL handles this?
One way of going about this, is to define the nodes in terms of the template
parameter. E.g.:
template < typename T >
struct tree {

typedef T value_type;

private:

struct tree_node {

value_type the_value;
tree_node * the_parent;
tree_node * the_first_child;
tree_node * the_next_sibling;
tree_node * the_prev_sibling;

};

tree_node * the_root_node;

// more stuff
}; // tree
Best

Kai-Uwe Bux
Jun 29 '07 #6
On Fri, 29 Jun 2007 13:36:57 -0700, Travis wrote:
It's a tree that I wrote.

The tree is templated and can store anything of NODETYPE.

When I instantiate the tree, I make each node a pointer to my custom
class.

In my custom class I have == overloaded.

When I do a search for a node I provide it a NODETYPE to find.

It doesn't find it because even though the NODETYPE I provide as
search criteria is the same as a node in the tree, they aren't
occupying the same memory so the == operator never goes true. I have
to dereference int the NODETYPE and it works fine.
Perhaps the 'someone else' mentioned below actually wants that kind of
behaviour, ie address comparison.
My concern is the reusabilty of the tree. If someone else comes along
and makes NODETYPE some object (not a pointer), then the dereferencing
in my find routine will bomb right?

Can you elaborate a little more on how the STL handles this?
std::set<int*m;

The default comparison for 'm' will also only compare addresses.
I need to provide my own comparison operator if I want something else.

struct s {
bool operator()(int * a, int * b) {
return a[5] < b[5];
}
};
std::set<int*, sms;

--
Obnoxious User
Jun 29 '07 #7
Travis wrote:
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 aren't the same so
it's not found.

If I force the tree to deference the node before comparing it works
but then what if my tree was of objects, not pointers, dereferencing
and object would blow up right?

Any suggestions, solutions?
You'll do much better in this newsgroup if you ask clear and precise
questions. Read over your first paragraph, pretend you don't already
know the problem you're asking about, and see if it makes any sense to you.

As to your question, as best as I can understand it. First of all, as
others have pointed out, you seem to be reinventing the wheel. The STL
has set and map classes which likely do what you're doing, only much
better. If you insist upon making your own class, then at least look at
the design choices made in the STL and see how you can use those for
yourself. For example, the set and map classes use a template parameter
to specify how comparisons are performed. By default, operator< is
used, but if that is not sufficient, the user may specify their own
function.

Mark
Jun 29 '07 #8
Your search function must allow as a parameter
a 'predicate' function or function object that
determines what 'finding' means...rather than using
operator==.

This is fairly common in e.g. STL containers
and used by the STL find_if, etc....

This allows the guy calling the search function
to determine what it means to find an item in the
tree and makes your searching not only possible but
much more flexible.

If it were me I'd probably be using an STL set
instead of rolling my own templated tree.
Ron
Travis wrote:
It's a tree that I wrote.

The tree is templated and can store anything of NODETYPE.

When I instantiate the tree, I make each node a pointer to my custom
class.

In my custom class I have == overloaded.

When I do a search for a node I provide it a NODETYPE to find.

It doesn't find it because even though the NODETYPE I provide as
search criteria is the same as a node in the tree, they aren't
occupying the same memory so the == operator never goes true. I have
to dereference int the NODETYPE and it works fine.

My concern is the reusabilty of the tree. If someone else comes along
and makes NODETYPE some object (not a pointer), then the dereferencing
in my find routine will bomb right?

Can you elaborate a little more on how the STL handles this?
Jul 4 '07 #9

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

Similar topics

14
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...
5
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...
6
by: Zri Man | last post by:
I'm relatively new to DB2 and was reasonably amused to see the REVERSE SCAN availability for Indexes. My assumptions are as follows: DB2/UDB uses B-Tree for indexing by default and is likely...
25
by: prabhat143 | last post by:
Hi, Given a singly linked, null terminated list, how can it be converted to tree? Each node in the list has three attributes: it's ID, it's parent ID and of course, the next node it's pointing...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
4
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...
4
by: whitehatmiracle | last post by:
Hello I have written a program for a binary tree. On compiling one has to first chose option 1 and then delete or search. Im having some trouble with the balancing function. It seems to be going...
0
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...
13
by: Bill Cunningham | last post by:
pgs 140-1 of kandr2 talk about recursion. I tried typing in the code char by char and couldn't find the bugs so I just read the code and a new concept to me jumped out. Maybe this is what it means...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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
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...
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...

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.