суть в том, что дерево заполняется символами/буквами на рандоме, потом нужно посчитать общее количество цифр в дереве.
нужно добавить собственно функцию заполнения и посмотреть сам код на ошибки только начинаю изучать эту тему, прошу тапками не кидаться)))
C++ |
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
| struct tree
{
char data;
tree *left;
tree *right;
};
void leaves(char data, tree *&derevo) {
if (NULL == derevo) {
derevo = new tree;
derevo->data = data;
derevo->left = derevo->right = NULL;
}
if (data > derevo->data) {
if (derevo->right != NULL) leaves(data, derevo->right);
else {
derevo->right = new tree;
derevo->right->left = derevo->right->right = NULL;
derevo->right->data = data;
}
}
if (data < derevo->data) {
if (derevo->left != NULL) leaves(data, derevo->left);
else {
derevo->left = new tree;
derevo->left->left = derevo->left->right = NULL;
derevo->left->data = data;
}
}
}
void show(tree *&t) {
if (t != NULL) {
show(t->left);
cout << t->data << "-";
show(t->right);
}
}
int last_one(tree *t, int summ) {
if (t == NULL) return -1;
while (t->right) {
if (t->data > '0' && t->data <= '9') {
summ += 1;
}
};
while (t->left) {
if (t->data > '0' && t->data <= '9') {
summ += 1;
}
};
cout << "col-vo cifr: " << summ << endl;
return summ;
}
int main() {
} |
|
Добавлено через 20 часов 53 минуты
C++ |
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
| #include <conio.h>
#include <iostream>
#include <ctime>
#include <cctype>
using namespace std;
struct tree
{
char data;
tree *left;
tree *right;
};
void leaves(char data, tree *&derevo) {
if (NULL == derevo) {
derevo = new tree;
derevo->data = data;
derevo->left = derevo->right = NULL;
}
if (data > derevo->data) {
if (derevo->right != NULL) leaves(data, derevo->right);
else {
derevo->right = new tree;
derevo->right->left = derevo->right->right = NULL;
derevo->right->data = data;
}
}
if (data < derevo->data) {
if (derevo->left != NULL) leaves(data, derevo->left);
else {
derevo->left = new tree;
derevo->left->left = derevo->left->right = NULL;
derevo->left->data = data;
}
}
}
void show(tree *&t) {
if (t != NULL) {
show(t->left);
cout << t->data << "-";
show(t->right);
}
}
char last_one(tree *t) {
int summ=0;
if (t == NULL) return -1;
while (t->right) {
if (t->data > '0' && t->data <= '9') {
summ ++;
}
};
while (t->left) {
if (t->data > '0' && t->data <= '9') {
summ += 1;
}
};
if (summ != 0) {
cout << "col-vo cifr: " << summ << endl;
}
else cout << "no numerals found!" << endl;
return summ;
}
int main() {
tree *t = NULL;
int iter;
char ch,zx;
srand(time(NULL));
cout << "vvedi kol-vo elementov:" << endl;
cin >> iter;
for (int i = 1; i < iter; i++) {
ch = rand() % 127;
if (isalnum(ch))
{
zx = ch;
leaves(zx, t);
}
}
show(t);
cout << endl;
last_one(t);
cin.get();
return 0;
} |
|
теперь код выглядит так, но поиск не работает...