#!/usr/bin/perl

# Also see http://www.plover.com/~mjd/perl/BTree/, Mark-Jason Dominus'
# article in The Perl Journal #7.

# This requires the BTree module, BTree.pm, which is available at
# the aforementioned URL, from http://tpj.com/tpj/programs, or
# from ch04.{tar.gz,zip} from http://orwant.www.media.mit.edu/algorithms-examples.
#
use BTree;  

my $tree = BTree->new( B => 20 );

# Insert a few items.

%hash = (color => "red", name => "apple", stemmed => 1);

while ( my ( $key, $value ) = each %hash ) {
    $tree->B_search(
        Key    => $key,
        Data   => $value,
        Insert => 1 );
}

# Test whether some items are in the tree.
@test = qw(color height);
foreach ( @test ) {
    defined $tree->B_search( Key => $_ )
        ? print "$_ is in the tree.\n"	
        : print "$_ is not in the tree.\n";
}

# Update an item only if it exists, do nothing if it doesn't.
$tree->B_search(
    Key     => 'some key',
    Data    => 'new value',
    Replace => 1 );

# Create or update an item whether it exists or not.
print $tree->B_search(
		      Key     => 'another key',
		      Data    => 'a value',
		      Insert  => 1,
		      Replace => 1 ), "\n";


