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
|
#include <iostream>
// arbres binaires, où l'arbre vide est NULL
class Node {
public:
int elt;
Node *left, *right;
Node(Node *left, int elt, Node *right);
};
Node::Node(Node *left, int elt, Node *right) {
this->left = left;
this->elt = elt;
this->right = right;
}
int tree_size(Node *t) {
if (t == NULL) return 0;
return 1 + tree_size(t->left) + tree_size(t->right);
}
Node *tree_add(Node *t, int x) {
if (t == NULL) return new Node(NULL, x, NULL);
if (x < t->elt) t->left = tree_add(t->left, x);
else if (x > t->elt) t->right = tree_add(t->right, x);
return t;
}
void tree_add_ref(Node* &t, int x) {
if (t == NULL ) t = new Node(NULL, x, NULL);
else if (x < t->elt) tree_add_ref(t->left, x);
else if (x > t->elt) tree_add_ref(t->right, x);
}
void tree_print(Node *t) {
if (t == NULL) return;
std::cout << "(";
tree_print(t->left);
std::cout << t->elt;
tree_print(t->right);
std::cout << ")";
}
// encapsulation dans une classe
class BST {
public:
Node *root;
BST();
int size();
void add1(int x);
void add2(int x);
void print();
};
BST::BST() {
this->root = NULL;
}
int BST::size() {
return tree_size(this->root);
}
void BST::add1(int x) {
this->root = tree_add(this->root, x);
}
void BST::add2(int x) {
tree_add_ref(this->root, x);
}
void BST::print() {
tree_print(this->root);
std::cout << "\n";
}
// tests
int main() {
BST t;
t.add1(2);
t.add2(3);
t.add1(1);
t.print();
t.add2(7);
t.add1(0);
t.print();
BST *u = new BST();
int i;
for (i = 0; i < 10; i++)
u->add1((31 * i) % 7);
u->print();
for (i = 0; i < 10; i++)
u->add2((29 * i) % 13);
u->print();
return 0;
}
|