Renaud Marlet – ENPC – PRALG 2016-2017 71





 !""#$%!%&"'#
Renaud Marlet – ENPC – PRALG 2016-2017 72
(
)!*
+&,
-
!.%/
0,
,
1"!!#
0./
2
+!*
$
3
4
5
56
78
*
,"

2&*
2!&
9*
.2/
,
x
:5
;
<
3=
>53
5>
= 3<
3754
Renaud Marlet – ENPC – PRALG 2016-2017 73
#!&
// Node of a tree containing an integer at each node
class IntTree {
// Node information
int data;
// Sequence of sons (empty if none)
vector<IntTree*> sons;
public:
// Create a node with given information
IntTree(int d);
// Return information of this node
int getData();
// Set information of this node
void setData(int d);
// Return the number of sons of this node
int nSons();
// Return the son at position pos, if any (considering left-most son is at position 0)
IntTree* getSon(int pos);
// Replace the exisiting son at position pos with newSon (left-most son at position 0)
void setSon(int pos, IntTree* newSon);
// Add newSon as supplementary right-most son of this node
void addAsLastSon(IntTree* newSon);
// Remove right-most son of this node
void removeLastSon();
};
8
=3
5: >
!?0
1
2
sons
data 42
sons
data 15
sons
data
sons
data 8
6
IntTree
!*1+
@"!
2ABC
Renaud Marlet – ENPC – PRALG 2016-2017 74
B%
#pragma once
#include <vector>
// Node of a tree containing an integer at each node
class IntTree {
// Node information
int data;
// Sequence of sons (empty if none)
std::vector<IntTree*> sons;
public:
// Create a node with given information
IntTree(int d);
// Destruct a node and all its descendants
~IntTree();
// Return information of this node
int getData();
// Set information of this node
void setData(int d);
// Return the number of sons of this node
int nSons();
// Return the son at position pos, if any (considering left-most son is at position 0)
IntTree* getSon(int pos);
// Replace the exisiting son at position pos with newSon (left-most son at position 0)
void setSon(int pos, IntTree* newSon);
// Add newSon as supplementary right-most son of this node
void addAsLastSon(IntTree* newSon);
// Remove right-most son of this node
void removeLastSon();
};
Renaud Marlet – ENPC – PRALG 2016-2017 75
5*
5%5/#!#IntTree
!#!#*IntTree.h.?!$!/
IntTree.cpp.D5$!&*E/
$$*!##.27/
5%3/F2
IntTree* root = new IntTree(12);
root->addAsLastSon(new IntTree(8));
root->getSon(0)
->addAsLastSon(new IntTree(4));
...
root->addAsLastSon(new IntTree(23));
root->getSon(1)
->addAsLastSon(new IntTree(17));
...
37
53
>
<= 56
5:
root
1 / 10 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !