To make it simple, I am trying to get this heap to print in a tree like format. It's close but I know I am missing stuff, but I just can't wrap my head around this module. I know there is tree::simple and I think just Tree? But I can't really find any tutorials on how to actually use with a list or a array. The heap sort is right, cause it's sorting the list after the tree has been posted but I can't figure how to draw the tree correctly, then again outputting has never been my strong suit on any language. I think it's not grabbing the data from the file? least that is my idea but i'm not confident enough to be sure. here is my code so far.
#!/usr/bin/perl
use 5.006;
use strict;
use warnings;
use Tree::DAG_Node;
process_data(read_file('data.txt'));
process_data((3,1,4,1,5,9,2,6,5,3,6));
sub read_file{
my($filename)=@_;
my @data=();
my @words;
open(my $fh, "<", $filename)
or die "Could not open file: $!\n";
while(<$fh>){
chomp;
@words = split(' ');
foreach my $word(@words) {
push @data, $word;
}
}
close $fh;
return @data;
}
sub heap_sort {
my ($a) = @_;
my $n = @$a;
for (my $i = ($n - 2) / 2; $i >= 0; $i--) {
down_heap($a, $n, $i);
}
for (my $i = 0; $i < $n; $i++) {
my $t = $a->[$n - $i - 1];
$a->[$n - $i - 1] = $a->[0];
$a->[0] = $t;
down_heap($a, $n - $i - 1, 0);
}
}
sub down_heap {
my ($a, $n, $i) = @_;
while (1) {
my $j = max($a, $n, $i, 2 * $i + 1, 2 * $i + 2);
last if $j == $i;
my $t = $a->[$i];
$a->[$i] = $a->[$j];
$a->[$j] = $t;
$i = $j;
}
sub max {
my ($a, $n, $i, $j, $k) = @_;
my $m = $i;
$m = $j if $j < $n && $a->[$j] > $a->[$m];
$m = $k if $k < $n && $a->[$k] > $a->[$m];
return $m;
}
}
sub draw_tree{
my(@data)=@_;
my $root = Tree::DAG_Node->new;
$root->name($_[0]);
$root->new_daughter->name($_) for ('1'..'10');
my @names = @data;
my $count =0;
for my $n ($root->daughters) {
for (split //, $names[$count++]) {
$n->new_daughter->name($_)
}
}
print map "$_\n", @{$root->draw_ascii_tree};
}
sub process_data{
my(@data)=@_;
my @a = @data;
print "@a\n";
print "\n";
heap_sort(\@a);
print "\n";
print "@a\n";
print "\n";
draw_tree(@a);
}
and here is the output I am getting so far.
10,4,5,2,1,7
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
Use of uninitialized value in split at HEAPSORTtree.pl line 77.
|
<10,4,5,2,1,7>
/---------------------------------------+--
-+---+---+---+---+---+---+---\
| |
| | | | | | | |
<1> <2>
<3> <4> <5> <6> <7> <8> <9> <10>
/-----------------+-----------------+---+---+---+---+---+---+---+---+---\
| | | | | | | | | | | |
<1> <Tree::DAG_Node=HASH(0x4b32dc)> <,> <4> <,> <5> <,> <2> <,> <1> <,> <7>
10,4,5,2,1,7
3 1 4 1 5 9 2 6 5 3 6
|
<1>
/---+---+---+---+---+---+---+---+---\
| | | | | | | | | |
<1> <2> <3> <4> <5> <6> <7> <8> <9> <10>
| | | | | | | | | |
<1> <1> <2> <3> <3> <4> <5> <5> <6> <9>
1 1 2 3 3 4 5 5 6 9 6
Press any key to continue . . .
the output I want is similar to this
|
<root>
/-------+-------+-------\
| | | |
<1> <d> <e> <f>
/---+---\ |
| | | <3>
<a> <b> <c> /---+---\
| | |
<g> <h> <i>