diff options
Diffstat (limited to 'bin-trees/task19.cpp')
| -rw-r--r-- | bin-trees/task19.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/bin-trees/task19.cpp b/bin-trees/task19.cpp new file mode 100644 index 0000000..c2f40bb --- /dev/null +++ b/bin-trees/task19.cpp @@ -0,0 +1,62 @@ +#include <iostream> +#include <string> + +#include "tree.h" + +using namespace std; + +void helper(tree *&t, int *sum, int *cnt) +{ + if (!t) return; + + if (t->left == NULL && t->right == NULL) + { + *sum += t->inf; + (*cnt)++; + } + + helper(t->left, sum, cnt); + helper(t->right, sum, cnt); +} + +float solve(tree *&t) +{ + int sum = 0; + int cnt = 0; + helper(t, &sum, &cnt); + return sum * 1.0 / cnt; +} + +void create(tree *&t, int n, tree *parent) +{ + if (n > 0) + { + int x; + cin >> x; + t = node(x); + t->parent = parent; + + int nl = n / 2; + int nr = n - nl - 1; + + create(t->left, nl, t); + create(t->right, nr, t); + } +} + +int main() +{ + int n; + cout << "Введите количество узлов: "; + cin >> n; + tree *t = new tree; + cout << "Введите содержимое узлов:" << endl; + create(t, n, NULL); + + print(t, log(n) / log(2)); + + float res = solve(t); + cout << "Среднее арифметическое листьев равно " << res << endl; + + return 0; +} |