Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
February 21st, 2013, 08:48 AM
 so.very.tired
Contributing User

Join Date: Nov 2012
Posts: 127
Time spent in forums: 1 Day 7 h 16 m 14 sec
Reputation Power: 2
Need help with BST and search function

Hi guys.

i need to write a recursive function that takes three parameters: tree's root, key, and a pointer to a function that measure the distance between two given keys, and returns the node that holds the closest key to the given key.
according to my assignment instructions, i'm not allowed to change the function prototype, and i have to deal with what i've got.

so, here's the prototype:
Code:
`node_t FindClosestNode(node_t root, key_t key, dist_func_t distanceFunc );`

node_t and key_t are pointers to void:
Code:
```typedef void* key_t;
typedef void* node_t;```

dist_func_t is a pointer:
Code:
`typedef int (*dist_func_t)(key_t key1, key_t key2);`

distanceFunc returns the distance between two keys (either strings or integers).

as you probably already noticed, it's impossible to compare between the given key and a current.node->key.
i can only measure distance between them. :|
which makes this assignment a big pain. so i can really use some help here.

after countless efforts and attempts, i managed to write something the works (kind of):

Code:
```node_t FindClosestNode(node_t root, key_t key, dist_func_t distance)
{
struct node* iterator=root; //casting

if (distance(iterator->key, key)>distance(iterator->left->key, key)) //if the given key is closer to the left child
return FindClosestNode(iterator->left, key, distance);
else if (distance(iterator->key, key)>distance(iterator->right->key, key)) //if the given key is closer to the right child
return FindClosestNode(iterator->right, key, distance);
else  //else - the given key is closer to current node
return iterator;
}```

my problem is this:
i couldn't think of any exit condition, and - most importantly - i couldn't figure out how to handle the cases where a node has no left/right child.
and that obviously causing a segmentation fault, since "distance" has to measure the distance between non valid keys.

any help would greatly appreciated.
also, i'd love to hear about other ideas on how to implement this function.
thanks in advanced!

Last edited by so.very.tired : February 21st, 2013 at 09:05 AM.

#2
February 21st, 2013, 10:09 AM
 so.very.tired
Contributing User

Join Date: Nov 2012
Posts: 127
Time spent in forums: 1 Day 7 h 16 m 14 sec
Reputation Power: 2
Yay!
i did it.

Code:
```node_t FindClosestNode(node_t root, key_t key, dist_func_t distance)
{
struct node* iterator=root; //casting

if (iterator->left)
if (distance(iterator->key, key)>distance(iterator->left->key, key))
return FindClosestNode(iterator->left, key, distance);
if (iterator->right)
if (distance(iterator->key, key)>distance(iterator->right->key, key))
return FindClosestNode(iterator->right, key, distance);
return iterator;
}```

and i know it's kind of an ugly code, but it works...

Last edited by so.very.tired : February 21st, 2013 at 10:12 AM.

#3
February 22nd, 2013, 09:12 AM
 so.very.tired
Contributing User

Join Date: Nov 2012
Posts: 127
Time spent in forums: 1 Day 7 h 16 m 14 sec
Reputation Power: 2
nope.
it has a major bug in it.

i can really use guidance:
how can i find closest node without the abilty to compare between the keys?

#4
February 22nd, 2013, 09:49 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,264
Time spent in forums: 2 Months 4 Weeks 1 Day 17 h 18 m 9 sec
Reputation Power: 1827
Don't you want something like
Code:
```d1 = distance(iterator->key, key);
d2 = iterator->left ? distance(iterator->left->key, key) : INT_MAX;
d3 = iterator->right ? distance(iterator->right->key, key) : INT_MAX;```

if d1 is smallest, then the current node is the answer.
if d2 < d3, then go left, otherwise go right.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#5
February 22nd, 2013, 01:10 PM
 so.very.tired
Contributing User

Join Date: Nov 2012
Posts: 127
Time spent in forums: 1 Day 7 h 16 m 14 sec
Reputation Power: 2
Hi salem.

what does INT_MAX represent?

#6
February 22nd, 2013, 02:00 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,264
Time spent in forums: 2 Months 4 Weeks 1 Day 17 h 18 m 9 sec
Reputation Power: 1827
Well in terms of the code posted, it means the furthest possible distance.

If you're after what INT_MAX is in C terms, try google.

#7
February 22nd, 2013, 02:14 PM
 requinix
Forgetful

Join Date: Mar 2007
Location: Washington, USA
Posts: 13,502
Time spent in forums: 5 Months 2 Weeks 2 Days 8 h 31 m
Reputation Power: 9259
Consider a tree like
Code:
```    10
/  \
2    12
/ \
1   8```

and searching for 8.

#8
February 22nd, 2013, 02:57 PM
 so.very.tired
Contributing User

Join Date: Nov 2012
Posts: 127
Time spent in forums: 1 Day 7 h 16 m 14 sec
Reputation Power: 2
thanks salem.
but as requinix pointed out, this code won't work.

I was thinking maybe to try adopting some ideas from here, but i just can't make that work without comparing two keys...

#9
February 22nd, 2013, 02:59 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,883
Time spent in forums: 3 Months 1 Day 15 h 47 m 56 sec
Reputation Power: 2199
Quote:
 Originally Posted by so.very.tired Hi salem. what does INT_MAX represent?

Or you could read the limits.h header file.

HINT: with the finite number of bits in an integer type, there are minimum and maximum limits to the values you can represent with that many bits. There are times in your program when you would want to know what those minimum and maximum values are. Also bear in mind that the size of an int can be different with different compilers, so that information is provided to you through the limits.h header file.

#10
February 23rd, 2013, 06:28 AM
 so.very.tired
Contributing User

Join Date: Nov 2012
Posts: 127
Time spent in forums: 1 Day 7 h 16 m 14 sec
Reputation Power: 2
OK.

that's the best i could come up with:

Code:
```node_t FindClosestNode(node_t root, key_t key, dist_func_t distance)
{
struct node* iterator=root, leftNode=NULL, rightNode=NULL;
int dCurrent, dLeft, dRight;
dCurrent=distance(iterator->key, key);

if (iterator->left)
{
leftNode=FindClosestNode(iterator->left, key, distance);
dLeft=distance(leftNode->key, key);
}
if (iterator->right)
{
rightNode=FindClosestNode(iterator->right, key, distance);
dRight=distance(rightNode->key, key);
}

if ((!iterator->right)&&(!iterator->left))
return iterator;
if (!iterator->right)
return dLeft<=dCurrent ? leftNode : iterator;
if (!iterator->left)
return dRight<=dCurrent ? rightNode : iterator;
if ((iterator->right)&&(iterator->left))
{
if (dRight<=dCurrent) return rightNode;
else if (dLeft<=dCurrent) return leftNode;
else return iterator;

}```

if anyone have tips/advices/ideas on how to make this code more efficient, i'd love to hear.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Need help with BST and search function

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap