blob: 6492dd474447ace9613936a96a4f42f7c695605a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/usr/bin/perl -w
use strict;
use warnings;
use feature qw(say);
use Tree::Binary;
=head2
TASK #2 › Binary Tree to Linked List
Submitted by: Mohammad S Anwar
You are given a binary tree.
Write a script to represent the given binary tree as an object and flatten it to a linked list object. Finally print the linked list object.
Example:
Input:
1
/ \
2 3
/ \
4 5
/ \
6 7
Output:
1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3
=cut
my($btree) = Tree::Binary->new('1')->setLeft( Tree::Binary->new('2')->setLeft(Tree::Binary->new('4'))->setRight(Tree::Binary->new('5')->setLeft(Tree::Binary->new('6'))->setRight( Tree::Binary->new('7') ) ))->setRight(Tree::Binary->new('3'));
$btree->traverse
(
sub
{
my($tree) = @_;
print " -> " unless $tree->isRoot;
print $tree->getNodeValue;
}
);
print "\n";
__END__
perl ch-2.pl
1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3
|