Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer Problems... Argh

Status
Not open for further replies.

ronnyjljr

IS-IT--Management
Nov 23, 2003
249
US
Ok Here Goes...

I have a tree. I want to delete an item from that tree.
I have function that will return the pointer to the specific node in the tree.

I then get the left and right pointers of that node and create a new node from those. I want to now set the location of the node I found equal to the node I formed.


Going through the debugger, it says that when I do Storage = meld(Left,Right) that the Storage location gets the values.

But then why does this not reflect in the tree? It modifies the memory directly, does it not?

For instance, I looked in the debugged and the memory address 001x is getting the value of the new node. But then if I go through the tree in the debugger, the value at 001x is still the original value of Storage and not the newly formed node. What do I have to do to save the modifications directly in memory so my tree can see them?



Code:
[blue]
void Delete(int Key, TreeNode *mTree)
{
		//Declare Storage
		TreeNode *Storage = NULL;	
		
		//Find Node, stores the Pointer in Storage
		FindNode(Key,mTree,Storage);

		//Node Not Found
		if (Storage->Priority==-1) return;

		//Create New Tree from left and right nodes
		//Insert them in to the spot of the original node
		Storage = meld(Storage->Left,Storage->Right);
		
[green]//Storage gets the correct value, but the value in the tree is not updated? Why?[/green]
} //End Delete


[/blue]

typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
What's TreeNode class? What's meld()?
And so on...
(Do you think that we are all telepathics here;-)
 
I'm not telepathic, but I assume the comment in the code tells what it does ;)

Anyway...you assign the node to 'Storage'.

So, what is 'Storage' then? It is a local variable in the Delete function. You should ask yourself:
Why should an assigment to a local variable be reflected in the tree?

/Per
[sub]
www.perfnurt.se[/sub]
 
Storage just points to a node into the tree. If you just modify Storage, then it points to other place, but the original contents remain on your tree.

You need to modify the contents of the tree by using Storage as a pointer.

Storage->left = whatever

Cheers,
Dian
 
I can't be sure without seeing more code, but here's a wild guess... Shouldn't the definition for FindNode() be like this:
Code:
FindNode( int, TreeNode*, TreeNode** );
Then instead of just passing Storage to FindNode() you'd pass &Storage...?

Maybe it would work better if TreeNode had get/set methods to change the nodes.
 
Hi Guys,

Thanks for the replies. I ended up figuring this out and decided to post the answer.

The tree need to be passed a reference-pointer variable. I think cpjust's attempt at using the ** operator would work as well but I haven't tried.

Code:
[blue]
void Delete(int Key, TreeNode [red]*&[/red]mTree);
[/blue]

Thanks,
Ron

typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
Great. The only reason I didn't recommend using a reference to a pointer is because some people get totally confused when they see that. ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top