From 84a9b5950b63ab1f664ff3bfce8da126439c7963 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 18 Mar 2021 20:14:35 +0400 Subject: =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=2017?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin-trees/task17.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'bin-trees') diff --git a/bin-trees/task17.cpp b/bin-trees/task17.cpp index cc917a9..d2cf414 100644 --- a/bin-trees/task17.cpp +++ b/bin-trees/task17.cpp @@ -20,34 +20,38 @@ int solve(tree *&t) return count; } - -void create(tree *&t, int n) +void create(tree *&t, int n, tree *parent) { if (n > 0) { int x; cin >> x; t = node(x); + t->parent = parent; // Дерево строится справа налево, чтобы ситуация из задания могла - // случиться (иначе, ответ всегда будет 0) + // случиться хотя бы один раз (иначе, при создании дерева с помощью + // этого алгоритма ответ всегда будет 0) int nr = n / 2; int nl = n - nr - 1; - create(t->right, nr); - create(t->left, nl); + create(t->right, nr, t); + create(t->left, nl, t); } } int main() { int n; + cout << "Введите количество узлов: "; cin >> n; tree *t = new tree; - create(t, n); + cout << "Введите содержимое узлов:" << endl; + create(t, n, NULL); + + print(t, log(n) / log(2)); - // Ответ всегда 0 или 1 int x = solve(t); - cout << x << endl; + cout << "Количество узлов, имеющих только правого потомка: " << x << endl; return 0; } -- cgit v1.2.3