#!/usr/bin/perl -w use strict; use Tree::Simple; #use Tree::Simple 'use_weak_refs'; use Data::Dumper; #use Devel::Trace; $Data::Dumper::Indent=1; #$Devel::Trace::TRACE=0; local $|=1; my $CR="\n"; my $WS="-----------------------------------------------------------------$CR"; my @array = qw/ 112.75 850.22 100.65 /; #for (my $j=0;$j<1;$j++) # { #always the same code to execute my $i=0; my $tree = Tree::Simple->new("root tree",Tree::Simple->ROOT); printleaf(\$tree); $tree->addChild(Tree::Simple->new("$array[$i]")); printleaf(\$tree->getChild(0)); my @_auxarray=@array; splice(@_auxarray,$i,1); foreach my $elem (@_auxarray) { printleaf(\Tree::Simple->new("$elem",$tree->getChild(0))); } undef @_auxarray; print "${WS}DATA DUMPER: $CR"; print Dumper $tree; print "${WS}"; FULLY_DESTROY($tree); ###########################STEP 3############################ print "${WS}DATA DUMPER: $CR"; print Dumper $tree; #$tree->traverse(sub { my($_tree)=@_; #print (("\t" x $_tree->getDepth()), $_tree->getNodeValue(),"\n"); #}); undef $tree; # } ; #pause sub printleaf{ my ($_leaf)=$_[0]; print("VALUE: ",$$_leaf->getNodeValue()," | ROOT?: ",$$_leaf->isRoot() ? "true" : "false"," | DEPTH: ",$$_leaf->getDepth()," | INDEX: ",$$_leaf->getIndex(),"$CR"); } sub FULLY_DESTROY{ my ($self)=$_[0]; print ($self->getNodeValue()," "); while(1) { ###########################STEP 4############################ if(!ref($self->{_parent}) && $self->{_parent} eq 'root' && scalar(@{$self->{_children}})==0) { $self->{_parent} = undef; print ("called last to end the sub routine$CR"); ###########################STEP 1############################ last; } if(defined($self->{_children}) && $self->getChildCount!=0) { print ("childs ",$self->getChildCount(),"$CR"); FULLY_DESTROY($self->getAllChildren()); } #if has no childs saves the father reference my $ref; my $index=$self->getIndex(); print("index ",$index," "); if(($index+1) == ($self->getParent()->getChildCount())) #last in the branch { print ("childs 0 last in branch$CR"); $ref = \$self->getParent(); $self->{_parent} = undef; @{$$ref->{_children}}=(); FULLY_DESTROY($$ref); } else { print ("childs 0 not last in branch$CR"); $ref = \$self->getSibling($index+1); $self->{_parent} = undef; FULLY_DESTROY($$ref); } } ###########################STEP 2############################ print "end should return to main function$CR$CR"; return; }