From db8aa3da4db1bd311e014ff6b1f27e7a1c79950f Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 18 Mar 2021 19:51:56 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=2018=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B2=20=D0=B4?= =?UTF-8?q?=D0=B5=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/tree.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'bin-trees/tree.h') diff --git a/bin-trees/tree.h b/bin-trees/tree.h index 0fa61b1..95e380a 100644 --- a/bin-trees/tree.h +++ b/bin-trees/tree.h @@ -1,4 +1,9 @@ #pragma once +#include +#include +#include + +using namespace std; struct tree { @@ -17,3 +22,63 @@ tree *node(int x) n->left = NULL; return n; } + +void find(tree *&tr, int x, tree *&res) +{ + if (tr) + { + if (tr->inf == x) + { + res = tr; + } + else + { + find(tr->left, x, res); + find(tr->right, x, res); + } + } +} + +void print(tree *tr, int k) +{ + if (!tr) cout << "Empty tree" << endl; + else + { + queue cur, next; + tree *r = tr; + cur.push(r); + int j = 0; + while (cur.size()) + { + if (j == 0) + { + for (int i = 0; i < (int) pow(2.0, k) - 1; i++) + cout << ' '; + } + tree *buf = cur.front(); + cur.pop(); + j++; + if (buf) + { + cout << buf->inf; + next.push(buf->left); + next.push(buf->right); + for (int i = 0; i < (int) pow(2.0, k + 1) - 1; i++) + cout << ' '; + } + if (!buf) + { + for (int i = 0; i < (int) pow(2.0, k + 1) - 1; i++) + cout << ' '; + cout << ' '; + } + if (cur.empty()) + { + cout << endl; + swap(cur, next); + j = 0; + k--; + } + } + } +} -- cgit v1.2.3