diff options
| author | Andrew <saintruler@gmail.com> | 2021-03-18 19:51:56 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2021-03-18 19:51:56 +0400 |
| commit | db8aa3da4db1bd311e014ff6b1f27e7a1c79950f (patch) | |
| tree | 15afb19d0bec6680e405265123d6f97d90e4c781 /bin-trees/tree.h | |
| parent | 14447430a5fae172e0eeff0fd0a484a0fd65e193 (diff) | |
Добавил 18 задание в деревьях
Diffstat (limited to 'bin-trees/tree.h')
| -rw-r--r-- | bin-trees/tree.h | 65 |
1 files changed, 65 insertions, 0 deletions
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 <iostream> +#include <queue> +#include <cmath> + +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<tree*> 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--; + } + } + } +} |