I am brand new to Perl. First time I ever looked at it was a couple days ago and decided to practice implementing some algorithms and data structures to learn. A binary search tree (BST) in this case.
Currently I am using a Node package "Node.pm", a BinarySearchTree package "BST.pm", and an entry point to play around with the code "Main.pl".
My problem arises while inserting the root node of the tree when using a reference to the root property (I know it's just a key => value from hash table, but with conventional OOP terminology I like to refer to it as a property).
Here is the constructor for context:
BST.pm
sub new() {
my $type = shift;
my $this = {};
my $list = shift;
$this->{'unordered_list'} = $list;
$this->{'root'} = undef;
bless($this, $type);
return $this;
}
Constructor takes a list of integers to construct a binary search tree from and the root node is undefined until the first call to "Insert".
In the call to "BuildUnbalancedTree" I am passing a reference to the 'root' property:
Main.pl
$bst = BST->new($nums);
$bst->BuildUnbalancedTree(\$bst->{'root'});
$bst->TraverseInOrder($bst->{'root'});
BST.pm
sub BuildUnbalancedTree(){
my $this = shift;
my $root = shift;
foreach my $key (@{$this->{'unordered_list'}}){
my $newnode = $this->NewNode($key);
$this->Insert($$root, $newnode);
}
}
In the "Insert" method, if I try to insert the root node using the variable passed to it, the tree is partially built except for the root node. The child nodes are all set, but the value of the root node itself is never set.
Here is the method that I expect to work but does not:
sub Insert(){
my $this = shift;
my $root = shift;
my $node = shift;
if(!$root) {
$root = $node;
} elsif($node->{'value'} > $root->{'value'}) {
if(!$root->{'right'}) {
$root->{'right'} = $node;
} else {
$this->Insert($root->{'right'}, $node);
}
} else {
if(!$root->{'left'}){
$root->{'left'} = $node;
} else {
$this->Insert($root->{'left'}, $node)
}
}
}
Assuming that the first condition will only ever be true on the first call to "Insert" I changed this line:
$root = $node;
to :
$this->{'root'} = $node;
And it works, but I'd really like to know why the other approach comes up short. I believe it's because I am dereferencing the root argument in the call to "BuildUnbalancedTree" (And if I don't it breaks everything) but would really appreciate confirmation and maybe some brief expert analysis.