Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <chrono>
- using namespace std;
- class node {
- public:
- int data;
- string info;
- node* left;
- node* right;
- int height;
- };
- node* newNode(int key, string info)
- {
- node* t = new node();
- t->data = key;
- t->info = info;
- t->left = NULL;
- t->right = NULL;
- t->height = 1;
- return t;
- }
- int height(node* t) {
- if (t == NULL) return 0;
- return t->height;
- }
- int max(int a, int b) {
- return (a > b) ? a : b;
- }
- node* min(node* t) {
- node* cur = t;
- while (cur->left != NULL) cur = cur->left;
- return cur;
- }
- int balance(node* t) {
- if (t == NULL) return 0;
- return height(t->left) - height(t->right);
- }
- node* rotateright(node* y)
- {
- node* x = y->left;
- node* b = x->right;
- x->right = y;
- y->left = b;
- y->height = max(height(y->left),
- height(y->right)) + 1;
- x->height = max(height(x->left),
- height(x->right)) + 1;
- return x;
- }
- node* rotateleft(node* t)
- {
- node* y = t->right;
- node* b = y->left;
- y->left = t;
- t->right = b;
- t->height = max(height(t->left),
- height(t->right)) + 1;
- y->height = max(height(y->left),
- height(y->right)) + 1;
- return y;
- }
- node* push(node* t, int in, string info) {
- if (t == NULL) return newNode(in, info);
- if (in < t->data)
- t->left = push(t->left, in, info);
- else if (in > t->data)
- t->right = push(t->right, in, info);
- else
- return t;
- t->height = 1 + max(height(t->left), height(t->right));
- if (balance(t) > 1 && in < t->left->data)
- return rotateright(t);
- if (balance(t) < -1 && in > t->right->data)
- return rotateleft(t);
- if (balance(t) > 1 && in > t->left->data) {
- t->left = rotateleft(t->left);
- return rotateright(t);
- }
- if (balance(t) < -1 && in < t->right->data) {
- t->right = rotateright(t->right);
- return rotateleft(t);
- }
- return t;
- }
- node* deleteNode(node* t, int in) {
- if (t == NULL) return t;
- if (in < t->data) t->left = deleteNode(t->left, in);
- else if (in > t->data) t->right = deleteNode(t->right, in);
- else {
- if (t->left == NULL || t->right == NULL) {
- node* tmp = t->left ? t->left : t->right;
- if (tmp == NULL) {
- tmp = t;
- t = NULL;
- }
- else {
- *t = *tmp;
- }
- free(tmp);
- }
- else {
- node* tmp = min(t->right);
- t->data = tmp->data;
- t->right = deleteNode(t->right, tmp->data);
- }
- }
- if (t == NULL) return t;
- t->height = 1 + max(height(t->left), height(t->right));
- if (balance(t) > 1 && balance(t->left) >= 0) return rotateright(t);
- if (balance(t) > 1 && balance(t->left) < 0) { t->left = rotateleft(t->left); return rotateright(t); }
- if (balance(t) < -1 && balance(t->right) <= 0) return rotateleft(t);
- if (balance(t) < -1 && balance(t->right) > 0) { t->right = rotateright(t->right); return rotateleft(t); }
- return t;
- }
- node* search(node* root, int key) {
- auto start = std::chrono::steady_clock::now();
- int count = 0;
- if (!root) return 0;
- while (root->data != key) {
- count++;
- if (key < root->data) root = root->left;
- else root = root->right;
- if (root == NULL) break;
- }
- cout << endl << count;
- auto end = std::chrono::steady_clock::now();
- std::chrono::duration<double> elapsed_seconds = end - start;
- std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
- return root;
- }
- void Print(node* Tree, int l) {
- int i = 0;
- if (Tree != NULL) {
- Print((*Tree).right, l + 1);
- for (i = 1; i <= l; i++) cout << " "; {
- cout << Tree->data << endl;
- }
- Print(((*Tree).left), l + 1);
- }
- }
- node* nodeGetter(int i, node* t) {
- node* tmp = new node();
- string path = "C:/Users/Pavel/Documents/Visual Studio 2019/Projects/SIAOD1.1/data.txt";
- ifstream f(path);
- string output = "";
- string output2 = "";
- for (int x = 0; x < i; x++) {
- getline(f, output, ' ');
- getline(f, output2, ';');
- }
- cout << output << " " << output2;
- return push(t, stoi(output), output2);
- }
- node* addall(node* t) {
- node* tmp = new node();
- string path = "C:/Users/Pavel/Desktop/table.txt";
- ifstream aboba(path);
- string output = "";
- string output2 = "";
- while (!aboba.eof()) {
- getline(aboba, output, ' ');
- getline(aboba, output2, ';');
- return push(t, stoi(output), output2);
- }
- }
- int main() {
- setlocale(0, "");
- cout << "\nЧтобы вывести дерево, введите 1.\nЧтобы найти нужное значение, введите 2.\nЧтобы добавить значение в дерево, нажмите 3.\nЧтобы удалить значение из дерева, нажмите 4.\n";
- int n = -1; int s = 0; node* tmp;
- node* tree = NULL;
- while (n != 0) {
- cin >> n;
- switch (n) {
- case 1:
- cout << "Дерево: " << endl;
- Print(tree, 0);
- printf("\n");
- break;
- case 2:
- cout << "Введите значение для поиска: ";
- s = 0;
- cin >> s;
- tmp = search(tree, s);
- cout << "Вот, что удалось найти: " << tmp->data << " " << tmp->info << endl;
- printf("\n");
- break;
- case 3:
- cout << "Введите ключ ячейки, которую хотите добавить: ";
- s = 0;
- cin >> s;
- tree = nodeGetter(s, tree);
- printf("\n");
- break;
- case 4:
- cout << "Введите ключ ячейки, которую хотите удалить: ";
- cin >> s;
- tree = deleteNode(tree, s);
- break;
- case 5:
- cin >> s;
- for (int i = 1; i < s; i++) {
- tree = nodeGetter(i, tree);
- }
- break;
- default:
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment