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

tree problem

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;
}

Nov 14 '05 #1
5 1962
On Mon, 11 Oct 2004 20:39:52 +1000
"Mike" <mi****@iprimus.com.au> wrote:
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.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #2
"Mike" <mi****@iprimus.com.au> wrote:
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/groups?threadm=414eb9a3_1%40news.iprimus.com.au>

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

Richard
Nov 14 '05 #3
[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.
Nov 14 '05 #4
Method Man <a@b.c> spoke thus:
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...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5
Christopher Benson-Manica wrote:
Method Man <a@b.c> spoke thus:
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...


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6

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

Similar topics

4
by: Jean-Christophe Michel | last post by:
Hi, In a complex merging of two (non ordered) xml files i need to keep track of the elements of the second tree that were already merged with first tree, to copy only unused elements at the end....
2
by: New | last post by:
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...
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...
6
by: sathyashrayan | last post by:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct tree { int data; struct tree *left,*right; }; void init(struct tree *node)
4
by: Ken | last post by:
I have a binary tree in VB NET and insertions seem to be slow. The program receives data from one source and inserts it into the tree. The program receives data from another source and...
1
by: Satish.Talyan | last post by:
hi, i want to create a dynamic tree hierarchy in javascript.there are two parts in tree, group & user.when we click on group then users come under that's tree category will be opened.problem is...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.