tree problem

M

Mike

Why does this code insert a node into a binary search tree correctly? If I
only inserting going by first digit it works properly but when I try
inserting going by the whole ip and the port number the inserts are totally
out of order.

where
IPAddress is four ints
Node is an IPAddress, portNumber, left pointer and right pointer
Nodeptr is a pointer to a Node

Nodeptr add(Nodeptr tree, IPAddress ip, int portNumber)
{
if(tree==NULL)
{
if((tree = malloc(sizeof(Node)))==NULL )
{
printf("No memory Left\n");
}
else
{
tree->address.digit1 = ip.digit1;
tree->address.digit2 = ip.digit2;
tree->address.digit3 = ip.digit3;
tree->address.digit4 = ip.digit4;
tree->portNo=portNumber;
tree->left = tree->right = NULL;
}
}

else if(ip.digit1 < tree->address.digit1)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}
else if(ip.digit1 >= tree->address.digit1)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
else if(ip.digit2 < tree->address.digit2)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}
else if(ip.digit2 >= tree->address.digit2)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
else if(ip.digit3 < tree->address.digit3)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}
else if(ip.digit3 >= tree->address.digit3)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
else if(ip.digit4 < tree->address.digit4)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}
else if(ip.digit4 >= tree->address.digit4)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
else if(portNumber < tree->portNo)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}
else if(portNumber >= tree->portNo)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
return tree;
}
 
F

Flash Gordon

Why does this code insert a node into a binary search tree correctly?
If I only inserting going by first digit it works properly but when I
try inserting going by the whole ip and the port number the inserts
are totally out of order.

where
IPAddress is four ints
Node is an IPAddress, portNumber, left pointer and right pointer
Nodeptr is a pointer to a Node

You should have included everything to make this a compilable example
rather than just describing things. Sometimes the error is in the
implementation of the bits described and sometimes we need to compile
and test, so leaving out this information reduces the help people can
provide.
Nodeptr add(Nodeptr tree, IPAddress ip, int portNumber)

I don't like using typedefs for pointers.
{
if(tree==NULL)
{
if((tree = malloc(sizeof(Node)))==NULL )

if((tree = malloc(sizeof *tree))==NULL )
There is less chance of error. Also, it no longer matters if you change
the type of tree.

{
printf("No memory Left\n");
}
else
{
tree->address.digit1 = ip.digit1;
tree->address.digit2 = ip.digit2;
tree->address.digit3 = ip.digit3;
tree->address.digit4 = ip.digit4;
tree->portNo=portNumber;
tree->left = tree->right = NULL;
}
}

else if(ip.digit1 < tree->address.digit1)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}

If ip.digit1 is not less than tree->address.digit1 it MUST logically be
greater than or equal to it. So the if below will always test true if it
is reached.
else if(ip.digit1 >= tree->address.digit1)

else if(ip.digit1 > tree->address.digit1)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}

So with my change above, it will actually execute the else clause here
when ip.digit1 == tree->address.digit1
else if(ip.digit2 < tree->address.digit2)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}

Here we have the same error.
else if(ip.digit2 >= tree->address.digit2)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
else if(ip.digit3 < tree->address.digit3)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}

and again
else if(ip.digit3 >= tree->address.digit3)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
else if(ip.digit4 < tree->address.digit4)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}

and again
else if(ip.digit4 >= tree->address.digit4)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
else if(portNumber < tree->portNo)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}

and again
else if(portNumber >= tree->portNo)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
return tree;
}

So this time you were lucky and the error was obvious in the snippet you
provided without compilation.
 
R

Richard Bos

Mike said:
Why does this code insert a node into a binary search tree correctly?
Nodeptr add(Nodeptr tree, IPAddress ip, int portNumber)
{
else if(ip.digit1 < tree->address.digit1)
{
tree->left = add(tree->left, ip,portNumber);
return tree;
}
else if(ip.digit1 >= tree->address.digit1)
{
tree->right = add(tree->right,ip,portNumber);
return tree;
}
else if(ip.digit2 < tree->address.digit2)

You know, this problem sounded strangely familiar. And true enough,
after rummaging through my outbox for a while, I found this:
<http://www.google.com/[email protected]>

Before looking for answers for homework problems, a little trip to
Google is often enough to prevent being found out.

Richard
 
M

Method Man

[snip]
I don't like using typedefs for pointers.

I can't stand when someone typedef's a pointer. I see it way too often in
people's code for my liking. It's as if the programmer is going out of
his/her way to make the code obsfucated by disguising the pointer. All just
to save typing a single '*' character.
 
C

Christopher Benson-Manica

Method Man said:
I can't stand when someone typedef's a pointer. I see it way too often in
people's code for my liking. It's as if the programmer is going out of
his/her way to make the code obsfucated by disguising the pointer. All just
to save typing a single '*' character.

At our company, we have

typedef const char *CPCHAR;

Needless to say, I avoid using it like the plague. Unfortunately I'm
the only one who does so...
 
C

CBFalconer

Christopher said:
character.

At our company, we have

typedef const char *CPCHAR;

Needless to say, I avoid using it like the plague. Unfortunately
I'm the only one who does so...

You might point out it is both ugly and misnamed. It creates a
pointer to a constant char, not a constant pointer to a char.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

problem adding into a tree 2
correct tree 4
data tree 3
generating key for AVL tree 0
Basic question in binary tree node insertion 2
tree errors 95
tree 6
Didn't get Ben Pfaff code (chapter 12 of The Book). 15

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top