summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2021-03-18 20:29:48 +0400
committerAndrew <saintruler@gmail.com>2021-03-18 20:29:48 +0400
commitd338d647b1227eefd71e8e7fcb8e8063d04a20af (patch)
tree3aa768b811c8e8b9dacb8d7232e82f04d5b5cadc
parent84a9b5950b63ab1f664ff3bfce8da126439c7963 (diff)
Добавил 19 задачу в деревьях
-rw-r--r--bin-trees/Makefile12
-rw-r--r--bin-trees/task19.cpp62
2 files changed, 68 insertions, 6 deletions
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 <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;
+}