code-ninja has asked for the wisdom of the Perl Monks concerning the following question:

I tried implementing a binary tree in Perl without using any external library. My code follows. The problem is, it says:

"Use of uninitialized value in numeric eq (==) at Tree.pl line 42. Use of uninitialized value in numeric eq (==) at Tree.pl line 42."
in the traverse() function. I took help from algorithms in Perl book.
use strict; use warnings; sub search { my ($tree, $element) = @_; # we pass the root node and the element + to be found my $node; while($node = $$tree) { # $tree is a reference so we dereference i +t using $$. if($element == $node->{val}) { return($tree, $node); # return the subtree where the node +was found. } else { if($element < $node->{val}) { # go left $tree = \$node->{left}; # note that $tree is a referen +ce so we assign it a reference with '\' } else { $tree = \$node->{right}; } } } # we reach here iff there is no $element to be found # we politely suggest user that they may add the element at the gi +ven position. return ($tree, undef); } sub insert { my ($tree, $element) = @_; my $found; ($tree, $found) = search($tree, $element); unless($found) { $found = { left => undef, right => undef, val => $element, }; $$tree = $found; } return $found; } sub traverse { my $tree = shift; if($$tree->{left} == undef || $$tree->{right} == undef) { return; } traverse(\$tree->{left}); print $$tree->{val}; print "\n"; traverse(\$tree->{right}); } my @ele = [1, 2, 3, 4, 5]; my ($tree, $stat, $link); for my $ele (@ele) { ($stat, $link) = insert(\$tree, $ele); } traverse(\$tree);

As per my (limited) knowledge of Perl, I guess the search() and insert() function are correct, but I'm not sure.

Replies are listed 'Best First'.
Re: Tree Data Structure
by kcott (Archbishop) on Dec 02, 2013 at 06:47 UTC

    G'day code-ninja,

    Use the defined function instead of attempting a numeric comparison. Here's some examples:

    $ perl -Mwarnings -le 'my $x; print 1 if $x == undef' Use of uninitialized value in numeric eq (==) at -e line 1. Use of uninitialized value $x in numeric eq (==) at -e line 1. 1
    $ perl -Mwarnings -le 'my $x; print 1 if defined $x'
    $ perl -Mwarnings -le 'my $x; print 1 if ! defined $x' 1

    -- Ken

Re: Tree Data Structure
by Laurent_R (Canon) on Dec 02, 2013 at 07:06 UTC

    Try to change this:

    if($$tree->{left} == undef || $$tree->{right} == undef) { return; }
    to something like this:
    return unless defined $$tree->{left} and defined $$tree->{right};

Re: Tree Data Structure
by code-ninja (Scribe) on Dec 04, 2013 at 07:19 UTC

    Taking kcott and Laurent_R's suggestions, I changed my traverse function to look like this:

    sub traverse { my $tree = shift; return unless defined $$tree->{left} and defined $$tree->{right}; traverse(\$tree->{left}); print $$tree->{val}; print "\n"; traverse(\$tree->{right}); }

    The code runs fine, but there is no output. I guess I made a very basic mistake in the insert function but I can't figure it out. Can someone point me to an implementation of binary tree in Perl? I do not intend to use the Tree::Binary module.