Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include "string"
- #include "algorithm"
- using namespace std;
- struct tree
- {
- int info;
- tree *left, *right;
- }*root;
- tree *list(int inf)
- {
- tree *t = new tree;
- t->info = inf;
- t->right = t->left = NULL;
- return t;
- }
- void add(tree *t)
- {
- int key, i;
- tree *prev;
- cin >> key;
- bool f = true;
- if (t == NULL)
- {
- root = list(key);
- }
- else
- {
- prev = t;
- while (t && f)
- {
- prev = t;
- if (key == t->info)
- {
- f = false;
- cout << "kluch use est";
- return;
- }
- else if (key < t->info) t = t->left;
- else t = t->right;
- }
- if (f)
- {
- t = list(key);
- if (key < prev->info) prev->left = t;
- else prev->right = t;
- }
- }
- }
- void view(tree *t, int level)
- {
- string str;
- if (t)
- {
- view(t->right, level + 1);
- for (int i = 0; i < level; i++) str = str + " ";
- cout << str << t->info << endl;
- view(t->left, level + 1);
- }
- }
- void clear(tree *t)
- {
- if (t != NULL)
- {
- clear(t->left);
- clear(t->right);
- delete t;
- }
- }
- int count(tree* tr) {
- if (tr == NULL)
- return 0;
- int n = 1;
- if (tr->right != NULL)
- n += count(tr->right);
- return n;
- }
- void main(void)
- {
- setlocale(LC_CTYPE, "russian");
- int menu, i;
- while (true)
- {
- int n;
- cout << "viberite: \n1.dobavit element \n2.prosmotr \n3.poschitac \n4.ydalic \n0.vihod \n";
- cin >> menu;
- switch (menu)
- {
- case 1:
- cout << "kol elem: ";
- cin >> i;
- cout << "elementii: ";
- while (i-- > 0)
- add(root); break;
- case 2:
- if (root == NULL)
- {
- cout << "pustoe\n";
- break;
- }
- view(root, 0); cout << endl; break;
- case 3:cout << "kol elementov:"; n = count(root->right); cout << n<<"\n\n"; break;
- case 4: clear(root); root = NULL; break;
- case 0: return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement