473,657 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem adding into a tree

New
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(N ode)))==NULL )
{
printf("No memory Left\n");
}
else
{
tree->address.digi t1 = ip.digit1;
tree->address.digi t2 = ip.digit2;
tree->address.digi t3 = ip.digit3;
tree->address.digi t4 = ip.digit4;
tree->portNo=portNum ber;
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,portN umber);
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,portN umber);
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,portN umber);
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,portN umber);
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,portN umber);
return tree;
}
return tree;
}
Nov 14 '05 #1
2 1631
"New" <a@a.co> 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. Nodeptr add(Nodeptr tree, IPAddress ip, int portNumber)
{
if(tree==NULL)
{
{
}
else
{
}
}
Your code could do with some more consistent indentation. As for your
error...
else if(ip.digit1 < tree->address.digit1 )
{
}
else if(ip.digit1 >= tree->address.digit1 )


When, do you think, are _both_ of those ifs going to fail? IOW, when is
the code reached which does the comparison based on digit2 to digit4?

Richard
Nov 14 '05 #2
In article <41********@new s.iprimus.com.a u>, "New" <a@a.co> wrote:
Why does this code insert a node into a binary search tree correctly? If I ^
I assume there's a "n't" missing here...
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.


First, I'd recommend re-writing this as:

static int
ipaddr_port_cmp (IPAddress *left_ip, int left_portNumber ,
IPAddress *right_ip, int right_portNumbe r)
{
/*
* put your comparisons here, return -1 if left < right,
* 1 if left > right, and 0 if they are equal.
*/
}

Nodeptr add(Nodeptr tree, IP...)
{
if (tree == NULL) {
...
} else if (ipaddr_port_cm p(&ip, portNumber,
&tree->addr, tree->portNo) < 0)
tree->left = add(tree->left, ip, portNumber);
} else {
tree->right = add(tree->right, ip, portNumber);
}
return (tree);
}

since the ipaddr_port_cmp () function will presumably be useful
elsewhere, and your code will be much less repetitive (and maintainable).
To figure out the problem with your code, consider the case of:

ip matches the root node's addr
portNumber < the root node's port

walk through the code for that case, and the problem should become
clear. You could also try turning on your compiler's warnings, since it
may be able to find the problem.

Cheers,
- jonathan
Nov 14 '05 #3

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

Similar topics

0
1412
by: paul b | last post by:
Hello, I am writing some LDAP-scripts in PHP. One of the scripts has to add new objects(urls) to the tree using the ldap_add command. The problem is that I cannot add objects of the type "labeledURIObjec" to the tree. The part of the script posing problems is the following one: $entry="labeledURIObject"; $entry="http://www.test.com";
5
3333
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run 3 times to get all three bad chars out of the file name. The passes look like this:
3
6057
by: Saradhi | last post by:
Hi All, Here I am facing a performance problem with the TreeView Node renaming. I am displaying a hierarchy Data in a treeview in my Windows C# Application. My tree view represents an hierarchical view of Parent Nodes and projects where in a projectnode can be added to any ParentNode and hence we may have a project node added to 100 Parent nodes. In this one, I have an operation of Renaming a Project Node. So whenever I am doing the...
6
7004
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone changes box1, I want to set the selected index in box2 = 0. When I do this, I dont get an error, but when the page loads, it still has the selected value and not 0. It seems that it is getting the selected value from the viewstate
4
5033
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a GridView that needs to be dynamically designed, depending on what collection of fields our uses want to edit for their product data. We have 400+ fields of information per product so they're selecting a subset of those fields to edit.
4
2302
by: pmcguire | last post by:
I have a treeview with a lot of nodes. I want to load only the nodes that are initially visible when the form loads, and then continue to populate it in background and/or when the nodes are required by the user either scrolling or performing some other action that would move the treeview window to a particular "unloaded" node in the treeview Any advice on how to go about this? It seems I need a way of sensing a scroll event in the treeview...
3
2351
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
2
4229
by: Jarod | last post by:
Hey I change backColor of linkButton in my Page_Load: lbDoSth.BackColor = Color.Red; But I have multiView on this page. On one of the views I have detailsView. When I add a new row a set Visible = false on this detailsView. And rebound appropriate gridView. Then I press the button and I have exception: Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save...
0
2401
by: raylopez99 | last post by:
I have a minor problem locating an image file to load as a Bitmap. A resource file "NudeSusan.jpeg" (which I think is actually a bmp file, originally taken from a jpeg of model Susan Spears) is loaded onto the "Resources" tree of the Visual Studio Solutions Explorer, and correctly works. However, another file, "MyImageFile.png" (which is also a bmp file, sorry for the confusion, but originally was a png file), does not work. How can...
0
8325
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8621
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.