IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

Arbre et fonction récursive


Sujet :

Langage Perl

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Avatar de Jasmine80
    Femme Profil pro
    Bioinformaticienne
    Inscrit en
    Octobre 2006
    Messages
    3 157
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 45
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Bioinformaticienne
    Secteur : Santé

    Informations forums :
    Inscription : Octobre 2006
    Messages : 3 157
    Par défaut Arbre et fonction récursive
    J'ai un objet Bio::Tree::Node que j'aimerais parcourir de façon récursive.

    Code non récursif
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    	foreach my $node (@nodes){
     
    		if (defined $node->id()){
    			print "\tid : ".$node->id()."\n";	
    		}
    		if (defined $node->branch_length()){
    			print "\tbranche length : ".$node->branch_length()."\n";
    		}
    		if (defined $node->ancestor()){
    			print "\tancestor : ".$node->ancestor()."\n";
    		}
    		if (defined $node->_creation_id()){			
    			# a private method signifying the internal creation order
    			print "\t_creation_id : ".$node->_creation_id()."\n";
    		}
     
     
    		my @nodes = $node->get_all_Descendents;
     
    		foreach my $n (@nodes){
    			if (defined $n->id()){
    				print "\tid : ".$n->id()."\n";	
    			}
    			if (defined $n->branch_length()){
    				print "\tbranche length : ".$n->branch_length()."\n";
    			}
    			if (defined $n->ancestor()){
    				print "\tancestor : ".$n->ancestor()."\n";
    			}
    			if (defined $n->_creation_id()){			
    				# a private method signifying the internal creation order
    				print "\t_creation_id : ".$n->_creation_id()."\n";
    			}
    			if (defined $n->get_all_Descendents){			
    				my @nodes = $n->get_all_Descendents;
    				# ...
    			}
    		}
     
    		print "\n\n";	
     
    	}
    A chaque fois qu'on s'enfonce plus en profondeur, on décale l'affichage d'une tabulation.


    Voici le code que j'ai essayé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    tree_reader(\@nodes, qq{});
     
     
     
     
     
     
     
     
    sub tree_reader {
     
    	my $ref_nodes = $_[0];
    	my $tree_shift = $_[1];
     
    	$tree_shift .= "\t";
     
    	my $next_branches;
     
    	foreach my $node (@{$ref_nodes}){
     
    		if (defined $node->id()){
    			print $tree_shift."\tid : ".$node->id()."\n";	
    		}
    		if (defined $node->branch_length()){
    			print $tree_shift."branche length : ".$node->branch_length()."\n";
    		}
    		if (defined $node->ancestor()){
    			print $tree_shift."ancestor : ".$node->ancestor()."\n";
    		}
    		if (defined $node->_creation_id()){			
    			# a private method signifying the internal creation order
    			print $tree_shift."_creation_id : ".$node->_creation_id()."\n";
    		}
    		if (defined $node->get_all_Descendents){
    			my @nodes = $node->get_all_Descendents;
    			$next_branches = \@nodes;
    		}
    		print "\n\n";
    	}
     
    	if (defined $next_branches){
    		tree_reader($next_branches, $tree_shift);
    	}
    }

    Voici le contenu du @node de départ
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    $VAR1 = bless( {
                     '_adding_descendent' => 0,
                     '_root_cleanup_methods' => [
                                                  sub { "DUMMY" }
                                                ],
                     '_creation_id' => 7,
                     '_id' => 'TRICHOTOMY',
                     '_root_verbose' => 0,
                     '_desc' => {
                                  '6' => bless( {
                                                  '_ancestor' => $VAR1,
                                                  '_id' => 'P11c',
                                                  '_desc' => {},
                                                  '_root_cleanup_methods' => [
                                                                               $VAR1->{'_root_cleanup_methods'}[0]
                                                                             ],
                                                  '_branch_length' => '0.88041',
                                                  '_creation_id' => 6,
                                                  '_root_verbose' => 0,
                                                  '_height' => undef
                                                }, 'Bio::Tree::Node' ),
                                  '0' => bless( {
                                                  '_ancestor' => $VAR1,
                                                  '_id' => 'P3A',
                                                  '_desc' => {},
                                                  '_root_cleanup_methods' => [
                                                                               $VAR1->{'_root_cleanup_methods'}[0]
                                                                             ],
                                                  '_branch_length' => '0.62174',
                                                  '_creation_id' => 0,
                                                  '_root_verbose' => 0,
                                                  '_height' => undef
                                                }, 'Bio::Tree::Node' ),
                                  '5' => bless( {
                                                  '_ancestor' => $VAR1,
                                                  '_id' => '573',
                                                  '_desc' => {
                                                               '4' => bless( {
                                                                               '_ancestor' => $VAR1->{'_desc'}{'5'},
                                                                               '_id' => 'P9e',
                                                                               '_desc' => {},
                                                                               '_root_cleanup_methods' => [
                                                                                                            $VAR1->{'_root_cleanup_methods'}[0]
                                                                                                          ],
                                                                               '_branch_length' => '0.62480',
                                                                               '_creation_id' => 4,
                                                                               '_root_verbose' => 0,
                                                                               '_height' => undef
                                                                             }, 'Bio::Tree::Node' ),
                                                               '3' => bless( {
                                                                               '_ancestor' => $VAR1->{'_desc'}{'5'},
                                                                               '_id' => '779',
                                                                               '_desc' => {
                                                                                            '1' => bless( {
                                                                                                            '_ancestor' => $VAR1->{'_desc'}{'5'}{'_desc'}{'3'},
                                                                                                            '_id' => 'P4a',
                                                                                                            '_desc' => {},
                                                                                                            '_root_cleanup_methods' => [
                                                                                                                                         $VAR1->{'_root_cleanup_methods'}[0]
                                                                                                                                       ],
                                                                                                            '_branch_length' => '0.51661',
                                                                                                            '_creation_id' => 1,
                                                                                                            '_root_verbose' => 0,
                                                                                                            '_height' => undef
                                                                                                          }, 'Bio::Tree::Node' ),
                                                                                            '2' => bless( {
                                                                                                            '_ancestor' => $VAR1->{'_desc'}{'5'}{'_desc'}{'3'},
                                                                                                            '_id' => 'P10f',
                                                                                                            '_desc' => {},
                                                                                                            '_root_cleanup_methods' => [
                                                                                                                                         $VAR1->{'_root_cleanup_methods'}[0]
                                                                                                                                       ],
                                                                                                            '_branch_length' => '0.60976',
                                                                                                            '_creation_id' => 2,
                                                                                                            '_root_verbose' => 0,
                                                                                                            '_height' => undef
                                                                                                          }, 'Bio::Tree::Node' )
                                                                                          },
                                                                               '_root_cleanup_methods' => [
                                                                                                            $VAR1->{'_root_cleanup_methods'}[0]
                                                                                                          ],
                                                                               '_adding_descendent' => 0,
                                                                               '_branch_length' => '0.10722',
                                                                               '_creation_id' => 3,
                                                                               '_root_verbose' => 0,
                                                                               '_height' => undef
                                                                             }, 'Bio::Tree::Node' )
                                                             },
                                                  '_root_cleanup_methods' => [
                                                                               $VAR1->{'_root_cleanup_methods'}[0]
                                                                             ],
                                                  '_adding_descendent' => 0,
                                                  '_branch_length' => '0.05625',
                                                  '_creation_id' => 5,
                                                  '_root_verbose' => 0,
                                                  '_height' => undef
                                                }, 'Bio::Tree::Node' )
                                },
                     '_height' => undef
                   }, 'Bio::Tree::Node' );
    Merci pour votre aide.

  2. #2
    Membre éprouvé
    Avatar de Jasmine80
    Femme Profil pro
    Bioinformaticienne
    Inscrit en
    Octobre 2006
    Messages
    3 157
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 45
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Bioinformaticienne
    Secteur : Santé

    Informations forums :
    Inscription : Octobre 2006
    Messages : 3 157
    Par défaut
    Je viens de me rendre compte que j'ai mal placé l'appel de la fonction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    sub tree_reader {
     
    	my $ref_nodes = $_[0];
    	my $tree_shift = $_[1];
     
    	$tree_shift .= "\t";
     
    	my $next_branches;
     
    	foreach my $node (@{$ref_nodes}){
     
    		if (defined $node->id()){
    			print $tree_shift."id : ".$node->id()."\n";	
    		}
    		if (defined $node->branch_length()){
    			print $tree_shift."branche length : ".$node->branch_length()."\n";
    		}
    		if (defined $node->ancestor()){
    			print $tree_shift."ancestor : ".$node->ancestor()."\n";
    		}
    		if (defined $node->_creation_id()){			
    			# a private method signifying the internal creation order
    			print $tree_shift."_creation_id : ".$node->_creation_id()."\n";
    		}
    		print "\n";
     
    		if (defined $node->get_all_Descendents){
    			my @nodes = $node->get_all_Descendents;
    			tree_reader(\@nodes, $tree_shift);
    		}
    	}
    }
    Voici ce qui s'affiche à l'écran
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    	id : TRICHOTOMY
    	_creation_id : 7
     
    		id : P3A
    		branche length : 0.62174
    		ancestor : Bio::Tree::Node=HASH(0x2296c1c)
    		_creation_id : 0
     
    		id : 573
    		branche length : 0.05625
    		ancestor : Bio::Tree::Node=HASH(0x2296c1c)
    		_creation_id : 5
     
    			id : 779
    			branche length : 0.10722
    			ancestor : Bio::Tree::Node=HASH(0x2336e08)
    			_creation_id : 3
     
    				id : P4a
    				branche length : 0.51661
    				ancestor : Bio::Tree::Node=HASH(0x2339f78)
    				_creation_id : 1
     
    				id : P10f
    				branche length : 0.60976
    				ancestor : Bio::Tree::Node=HASH(0x2339f78)
    				_creation_id : 2
     
    			id : P4a
    			branche length : 0.51661
    			ancestor : Bio::Tree::Node=HASH(0x2339f78)
    			_creation_id : 1
     
    			id : P10f
    			branche length : 0.60976
    			ancestor : Bio::Tree::Node=HASH(0x2339f78)
    			_creation_id : 2
     
    			id : P9e
    			branche length : 0.62480
    			ancestor : Bio::Tree::Node=HASH(0x2336e08)
    			_creation_id : 4
     
    		id : 779
    		branche length : 0.10722
    		ancestor : Bio::Tree::Node=HASH(0x2336e08)
    		_creation_id : 3
     
    			id : P4a
    			branche length : 0.51661
    			ancestor : Bio::Tree::Node=HASH(0x2339f78)
    			_creation_id : 1
     
    			id : P10f
    			branche length : 0.60976
    			ancestor : Bio::Tree::Node=HASH(0x2339f78)
    			_creation_id : 2
     
    		id : P4a
    		branche length : 0.51661
    		ancestor : Bio::Tree::Node=HASH(0x2339f78)
    		_creation_id : 1
     
    		id : P10f
    		branche length : 0.60976
    		ancestor : Bio::Tree::Node=HASH(0x2339f78)
    		_creation_id : 2
     
    		id : P9e
    		branche length : 0.62480
    		ancestor : Bio::Tree::Node=HASH(0x2336e08)
    		_creation_id : 4
     
    		id : P11c
    		branche length : 0.88041
    		ancestor : Bio::Tree::Node=HASH(0x2296c1c)
    		_creation_id : 6
     
    	id : P3A
    	branche length : 0.62174
    	ancestor : Bio::Tree::Node=HASH(0x2296c1c)
    	_creation_id : 0
     
    	id : 573
    	branche length : 0.05625
    	ancestor : Bio::Tree::Node=HASH(0x2296c1c)
    	_creation_id : 5
     
    		id : 779
    		branche length : 0.10722
    		ancestor : Bio::Tree::Node=HASH(0x2336e08)
    		_creation_id : 3
     
    			id : P4a
    			branche length : 0.51661
    			ancestor : Bio::Tree::Node=HASH(0x2339f78)
    			_creation_id : 1
     
    			id : P10f
    			branche length : 0.60976
    			ancestor : Bio::Tree::Node=HASH(0x2339f78)
    			_creation_id : 2
     
    		id : P4a
    		branche length : 0.51661
    		ancestor : Bio::Tree::Node=HASH(0x2339f78)
    		_creation_id : 1
     
    		id : P10f
    		branche length : 0.60976
    		ancestor : Bio::Tree::Node=HASH(0x2339f78)
    		_creation_id : 2
     
    		id : P9e
    		branche length : 0.62480
    		ancestor : Bio::Tree::Node=HASH(0x2336e08)
    		_creation_id : 4
     
    	id : 779
    	branche length : 0.10722
    	ancestor : Bio::Tree::Node=HASH(0x2336e08)
    	_creation_id : 3
     
    		id : P4a
    		branche length : 0.51661
    		ancestor : Bio::Tree::Node=HASH(0x2339f78)
    		_creation_id : 1
     
    		id : P10f
    		branche length : 0.60976
    		ancestor : Bio::Tree::Node=HASH(0x2339f78)
    		_creation_id : 2
     
    	id : P4a
    	branche length : 0.51661
    	ancestor : Bio::Tree::Node=HASH(0x2339f78)
    	_creation_id : 1
     
    	id : P10f
    	branche length : 0.60976
    	ancestor : Bio::Tree::Node=HASH(0x2339f78)
    	_creation_id : 2
     
    	id : P9e
    	branche length : 0.62480
    	ancestor : Bio::Tree::Node=HASH(0x2336e08)
    	_creation_id : 4
     
    	id : P11c
    	branche length : 0.88041
    	ancestor : Bio::Tree::Node=HASH(0x2296c1c)
    	_creation_id : 6
    ... il y a beaucoup trop de valeurs

  3. #3
    Membre éprouvé
    Avatar de Jasmine80
    Femme Profil pro
    Bioinformaticienne
    Inscrit en
    Octobre 2006
    Messages
    3 157
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 45
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Bioinformaticienne
    Secteur : Santé

    Informations forums :
    Inscription : Octobre 2006
    Messages : 3 157
    Par défaut
    get_all_Descendents

    Title : get_all_Descendents
    Usage : my @nodes = $node->get_all_Descendents;
    Function: Recursively fetch all the nodes and their descendents
    *NOTE* This is different from each_Descendent
    Returns : Array or Bio::Tree::NodeI objects
    Args : none
    Quand j'utilise la fonction get_all_Descendents, cela me donne accès à un nouvel objet Bio::Tree::Node, d'où les nouvelles valeurs n'apparaissant pas auparavant.
    Comment puis-je m'assurer que l'entièreté des données est bien parcourue?
    Si get_all_Descendents renvoie un array, comment récupérer ses valeurs?

    Merci,

  4. #4
    Membre éprouvé
    Avatar de Jasmine80
    Femme Profil pro
    Bioinformaticienne
    Inscrit en
    Octobre 2006
    Messages
    3 157
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 45
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Bioinformaticienne
    Secteur : Santé

    Informations forums :
    Inscription : Octobre 2006
    Messages : 3 157
    Par défaut
    J'aimerais simplement récupérer les données de @nodes de départ. Ce que je dois récupérer récursivement correspond à '_desc' la fonction get_all_Descendents ne semble pas récupérer son contenu et aucune méthode desc ou _desc ne fonctionne. La méthode description ne renvoie pas non plus les sous-groupes.

  5. #5
    Membre éprouvé
    Avatar de Jasmine80
    Femme Profil pro
    Bioinformaticienne
    Inscrit en
    Octobre 2006
    Messages
    3 157
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 45
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Bioinformaticienne
    Secteur : Santé

    Informations forums :
    Inscription : Octobre 2006
    Messages : 3 157
    Par défaut
    J'ai trouvé la solution, au cas où ça pourrait aider quelqu'un :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    	# chaque valeur est un objet Bio::Tree::Node
    	my @nodes = $tree->get_nodes;
    	my @sub_nodes = $nodes[0]->each_Descendent;
    	tree_reader(\@sub_nodes, qq{});
     
    sub tree_reader {
     
    	my $ref_nodes = $_[0];
    	my $tree_shift = $_[1];
     
    	$tree_shift .= "\t";
     
    	my $next_branches;
     
    	foreach my $node (@{$ref_nodes}){
     
    		if (defined $node->id()){
    			print $tree_shift."id : ".$node->id()."\n";	
    		}
    		if (defined $node->branch_length()){
    			print $tree_shift."branche length : ".$node->branch_length()."\n";
    		}
     
    		if (defined $node->_creation_id()){			
    			# a private method signifying the internal creation order
    			print $tree_shift."_creation_id : ".$node->_creation_id()."\n";
    		}
    		print "\n";
     
    		if (defined $node->each_Descendent){
    			my @nodes = $node->each_Descendent;
    			tree_reader(\@nodes, $tree_shift);
    		}
    	}
    }

    Résultat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    	id : TRICHOTOMY
    	_creation_id : 7
     
    		id : P3A
    		branche length : 0.62174
    		_creation_id : 0
     
    		id : 573
    		branche length : 0.05625
    		_creation_id : 5
     
    			id : 779
    			branche length : 0.10722
    			_creation_id : 3
     
    				id : P4a
    				branche length : 0.51661
    				_creation_id : 1
     
    				id : P10f
    				branche length : 0.60976
    				_creation_id : 2
     
    			id : P9e
    			branche length : 0.62480
    			_creation_id : 4
     
    		id : P11c
    		branche length : 0.88041
    		_creation_id : 6
    je suis contente, c'est la première fonction récursive que j'arrive à créer

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fonction récursive pour obtenir un arbre
    Par TerrorOnCity dans le forum Langage
    Réponses: 9
    Dernier message: 31/05/2013, 00h49
  2. Réponses: 6
    Dernier message: 12/04/2007, 20h30
  3. Fonctions récursives pour parcourir un arbre
    Par mikedavem dans le forum C
    Réponses: 4
    Dernier message: 05/06/2006, 12h00
  4. Fonction récursive renvoi sur page d'erreur
    Par peck dans le forum Langage
    Réponses: 1
    Dernier message: 23/12/2005, 10h08
  5. Problème de fonction récursive avec un TcxDBTreeList
    Par isachat666 dans le forum Composants VCL
    Réponses: 1
    Dernier message: 05/12/2005, 13h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo