Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Binary Search Tree Debug Question

by FunkyMonk (Canon)
on Aug 13, 2011 at 13:56 UTC ( #920171=note: print w/replies, xml ) Need Help??


in reply to Binary Search Tree Debug Question

if ( $tree->{VALUE} > $value ) { insert( $tree->{LEFT}, $value ) } elsif ( $tree->{VALUE} < $value ) { insert( $tree->{RIGHT}, $value )
What if the data includes duplicated values? The Perl CookBook (1st and 2nd editions) had an additional line:
if ( $tree->{VALUE} > $value ) { insert( $tree->{LEFT}, $value ) } elsif ( $tree->{VALUE} < $value ) { insert( $tree->{RIGHT}, $value ) else { warn "$value duplicated" }

or, if you want to include the duplicates, change either '>' to '>=', or '<' to '<=':

if ( $tree->{VALUE} >= $value ) { insert( $tree->{LEFT}, $value ) } elsif ( $tree->{VALUE} < $value ) { insert( $tree->{RIGHT}, $value )

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://920171]
help
Chatterbox?
[choroba]: Tux: What kind of dead-key diacriticals do you use? It works for me: ůěščö
[choroba]: Do the keys work at any other Tk application?

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-09-25 19:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (288 votes). Check out past polls.

    Notices?