Thread Rekursion mittels Array (7 answers)
Opened by peterb at 2017-12-05 11:03

peterb
 2017-12-05 14:26
#187677 #187677
User since
2010-05-19
42 Artikel
BenutzerIn
[default_avatar]
Uff :-)

Über die Mittagspause hab ich feinsten Spaghetticode geschrieben :-)

Denkst Du kann man das noch ein wenig verbessern?

Code: (dl )
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use strict;
use warnings;

my @cons = (
["1A", "2A"],
["1A", "2B"],
["2A", "4A"],
["3A", "2A"],
["4A", "5A"],
["2B", "3A"],
["3A", "4A"]
);


print "Gib das Element ein, ab welchen der Baum zurueck gegeben werde soll?\n";
my $input_id = <stdin>;
chomp($input_id);
print "--------------------------------\n";
print "--- \n";
print "--- \n";
print "--- Mein input_id war:\n";
print "--- \n";
print "--- $input_id \n";
print "--- \n";
print "--- \n";
print "--------------------------------\n ";



print "IST ---------------\n";
for (my $i = 0; $i <= $#cons; $i++){
my $id = $cons[$i][0];
my $rootid = $cons[$i][1];


print "$id --> $rootid\n";


}

print "\n" x 3;


my @tree_duplicates = ();
&abwaerts($input_id);
my %seen;
my @tree = grep { ! $seen{join(q{,}, @$_)}++ } @tree_duplicates;
for (my $i = 0; $i <= $#tree; $i++){
my $i_id = $tree[$i][0];
my $i_rootid = $tree[$i][1];
print "$i_id, $i_rootid\n";
}


sub abwaerts{

my $id = shift;
# print "--> Meine EinstiegsID: $id\n";

for (my $i = 0; $i <= $#cons; $i++){
my $i_id = $cons[$i][0];
my $i_rootid = $cons[$i][1];
# print "ooo $i_id, $i_rootid\n";

if ($id eq $i_id){
# print "--> in Array iterierend: $i_id\n";
&abwaerts($i_rootid);
print "-->CON $id --> $i_rootid\n";
push @tree_duplicates, [$id, $i_rootid];
}
}
}


Danke für den Tipp!

LG
Peter

View full thread Rekursion mittels Array