PDA

View Full Version : returning a pointer to an element in a binary tree



manish411
August 26th, 2011, 09:59 PM
struct node* rfind(struct node* current,int a)
{

struct node* c = current;

if(current->data!= a && current!=NULL)
{
current = rfind(current->leftchild,a);
if(current == NULL)
current = rfind(current->rightchild,a);
}
return current;
}
struct node *data = rfind(root,3);


initially for a tree inserted in the order 4 5 9 2 16 12 3 1

I am able to find the node 4 , 1 , 2, i.e the left child of any node
but I am getting a segmentation fault if I try to find a node which is a rightchild
of any subtree

Bachstelze
August 26th, 2011, 10:02 PM
if(current == NULL)
current = rfind(current->rightchild,a);

Don't see a problem there? ;)

giowck
August 26th, 2011, 10:08 PM
Did you try to change the following


if(current->data!= a && current!=NULL)

in



if(current!=NULL && (current->data!= a))


?

Because if current is NULL, the "current->data" operation is executed (if the compiler uses the evaluation order from left to right...)

With the new code, if current is NULL, the second expression will not be evaluated because the first already is false....

EDIT: ;P someone was faster than me

Arndt
August 26th, 2011, 11:15 PM
struct node* rfind(struct node* current,int a)
{

struct node* c = current;

if(current->data!= a && current!=NULL)
{
current = rfind(current->leftchild,a);
if(current == NULL)
current = rfind(current->rightchild,a);
}
return current;
}
struct node *data = rfind(root,3);


initially for a tree inserted in the order 4 5 9 2 16 12 3 1

I am able to find the node 4 , 1 , 2, i.e the left child of any node
but I am getting a segmentation fault if I try to find a node which is a rightchild
of any subtree

I recommend debugging with gdb.

Bachstelze
August 26th, 2011, 11:18 PM
I recommend debugging with gdb.

I don't. ;) To me, using gdb for this is like using a calculator to compute 6*15. Those are things you should be able to do on your own, otherwise you will waste massive amounts of time firing up gdb for the simplest things.

Arndt
August 26th, 2011, 11:33 PM
I don't. ;) To me, using gdb for this is like using a calculator to compute 6*15. Those are things you should be able to do on your own, otherwise you will waste massive amounts of time firing up gdb for the simplest things.

I recommend it if it solves his problem, which he apparently couldn't do on his own. And I recommend it when the source code is much larger than this, too.

If you can find the errors faster by visual inspection, of course you should do so.

That aside, I don't understand the "massive amounts of time" part. Firing up gdb takes exactly as long as typing the command that does so.

manish411
August 27th, 2011, 07:24 AM
if(current == NULL)
current = rfind(current->rightchild,a);

Don't see a problem there? ;)

No, I dont see the problem , please can you point it out to me!!!

giowck
August 27th, 2011, 07:43 AM
No, I dont see the problem , please can you point it out to me!!!

If current is NULL, you can't access to a non existent memory space. This causes a segmentation fault.

http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer

manish411
August 27th, 2011, 08:26 AM
If current is NULL, you can't access to a non existent memory space. This causes a segmentation fault.

http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer (http://en.wikipedia.org/wiki/Pointer_%28computing%29#Null_pointer)

Thanks a lot
by making a slight changes I am able to run the program

by making use of another pointer



struct node* c;

and using it to return the null pointer while at the same time
storing the parent pointer in current pointer



struct node* rfind(struct node* current,int a)
{

struct node* c = current;

if(current!=NULL)
{
if(current->data!=a)
{
c = rfind(current->leftchild,a);
if(c == NULL)
c = rfind(current->rightchild,a);
}
}
return c;
}