From d338d647b1227eefd71e8e7fcb8e8063d04a20af Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 18 Mar 2021 20:29:48 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=2019=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D1=83=20=D0=B2=20=D0=B4=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B2=D1=8C=D1=8F=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin-trees/Makefile | 12 +++++----- bin-trees/task19.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 bin-trees/task19.cpp diff --git a/bin-trees/Makefile b/bin-trees/Makefile index d4dffe5..4d62434 100644 --- a/bin-trees/Makefile +++ b/bin-trees/Makefile @@ -5,8 +5,8 @@ COMPILE=$(CXX) $(CFLAGS) task17: $(COMPILE) -o task.out task17.cpp test17: task17 - @printf "\n" | ./task.out - @printf "Answer: \n" + @printf "10\n4 5 3 7 8 6 9 1 2 0\n2\n" | ./task.out + @printf "Answer: 3\n" task18: $(COMPILE) -o task.out task18.cpp @@ -17,14 +17,14 @@ test18: task18 task19: $(COMPILE) -o task.out task19.cpp test19: task19 - @printf "\n" | ./task.out - @printf "Answer: \n" + @printf "10\n4 5 3 7 8 6 9 1 2 0\n" | ./task.out + @printf "Answer: 3.75\n" task14: $(COMPILE) -o task.out task14.cpp test14: task14 - @printf "\n" | ./task.out - @printf "Answer: \n" + @printf "10\n4 5 3 7 8 6 9 1 2 0\n2\n" | ./task.out + @printf "Answer: 12\n" clean: rm -f task.out *.zip 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 +#include + +#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; +} -- cgit v1.2.3