We don't bite newbies here... much PerlMonks

### Comment on

 Need Help??

Why not to code B-Tree yourself?

```#!/usr/bin/perl

use strict;
use warnings;

my %distances = (
100 => 'London',
200 => 'Paris',
300 => 'Rome',
600 => 'Berlin'
);

my @distances = sort {\$a<=>\$b} keys %distances;

sub make_btree {
my \$first = shift;
my \$nof = shift;

if ( \$nof == 1 ) {
return { Value => \$distances[\$first] },
}
else {
my \$m = int(\$nof/2);
my \$val = ( \$distances[\$first+\$m-1] + \$distances[\$first+\$m] )/
+2;

return {
Value => \$val,
Left => make_btree(\$first, \$m),
Right => make_btree(\$first+\$m, \$nof-\$m),
}
}
}

my \$BTREE = make_btree(0, scalar @distances);

#use Data::Dumper;
#print Dumper( \$BTREE );

sub closest_city {
my \$key = shift;

my \$btree = \$BTREE;
while ( exists \$btree->{Left} ) {
my \$v = \$btree->{Value};
\$btree = \$btree->{ \$key<\$v ? 'Left' : 'Right' }
}

\$distances{ \$btree->{Value} };
}

print q{closest_city(5)="}, closest_city(5), qq{"\n};
print q{closest_city(149)="}, closest_city(149), qq{"\n};
print q{closest_city(150)="}, closest_city(150), qq{"\n};
print q{closest_city(203)="}, closest_city(203), qq{"\n};
print q{closest_city(290)="}, closest_city(290), qq{"\n};
print q{closest_city(500)="}, closest_city(500), qq{"\n};
print q{closest_city(5000)="}, closest_city(5000), qq{"\n};
Output:
```closest_city(5)="London"
closest_city(149)="London"
closest_city(150)="Paris"
closest_city(203)="Paris"
closest_city(290)="Rome"
closest_city(500)="Berlin"
closest_city(5000)="Berlin"
Following B-Tree will be generated in this example:
```              250
/   \
150     450
/   |     |  \
100   200   300  600
|     |     |    |
London  Paris  Rome  Berlin

In reply to Re: Lookup closest hash key by nif
in thread Lookup closest hash key by Just in

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2017-10-20 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My fridge is mostly full of:

Results (258 votes). Check out past polls.

Notices?