Toxotsist

СДП на файле

Nov 6th, 2021 (edited)
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <chrono>
  5. using namespace std;
  6.  
  7. class node {
  8. public:
  9.     int data;
  10.     string info;
  11.     node* left;
  12.     node* right;
  13.     int height;
  14. };
  15.  
  16. node* newNode(int key, string info)
  17. {
  18.     node* t = new node();
  19.     t->data = key;
  20.     t->info = info;
  21.     t->left = NULL;
  22.     t->right = NULL;
  23.     t->height = 1;
  24.     return t;
  25. }
  26.  
  27. int height(node* t) {
  28.     if (t == NULL) return 0;
  29.     return t->height;
  30. }
  31.  
  32. int max(int a, int b) {
  33.     return (a > b) ? a : b;
  34. }
  35.  
  36. node* min(node* t) {
  37.     node* cur = t;
  38.     while (cur->left != NULL) cur = cur->left;
  39.     return cur;
  40. }
  41.  
  42. int balance(node* t) {
  43.     if (t == NULL) return 0;
  44.     return height(t->left) - height(t->right);
  45. }
  46.  
  47. node* rotateright(node* y)
  48. {
  49.     node* x = y->left;
  50.     node* b = x->right;
  51.     x->right = y;
  52.     y->left = b;
  53.     y->height = max(height(y->left),
  54.         height(y->right)) + 1;
  55.     x->height = max(height(x->left),
  56.         height(x->right)) + 1;
  57.     return x;
  58. }
  59.  
  60. node* rotateleft(node* t)
  61. {
  62.     node* y = t->right;
  63.     node* b = y->left;
  64.     y->left = t;
  65.     t->right = b;
  66.     t->height = max(height(t->left),
  67.         height(t->right)) + 1;
  68.     y->height = max(height(y->left),
  69.         height(y->right)) + 1;
  70.     return y;
  71. }
  72.  
  73. node* push(node* t, int in, string info) {
  74.     if (t == NULL) return newNode(in, info);
  75.     if (in < t->data)
  76.         t->left = push(t->left, in, info);
  77.     else if (in > t->data)
  78.         t->right = push(t->right, in, info);
  79.     else
  80.         return t;
  81.  
  82.     t->height = 1 + max(height(t->left), height(t->right));
  83.     if (balance(t) > 1 && in < t->left->data)
  84.         return rotateright(t);
  85.     if (balance(t) < -1 && in > t->right->data)
  86.         return rotateleft(t);
  87.     if (balance(t) > 1 && in > t->left->data) {
  88.         t->left = rotateleft(t->left);
  89.         return rotateright(t);
  90.     }
  91.     if (balance(t) < -1 && in < t->right->data) {
  92.         t->right = rotateright(t->right);
  93.         return rotateleft(t);
  94.     }
  95.     return t;
  96. }
  97.  
  98. node* deleteNode(node* t, int in) {
  99.     if (t == NULL) return t;
  100.  
  101.     if (in < t->data) t->left = deleteNode(t->left, in);
  102.     else if (in > t->data) t->right = deleteNode(t->right, in);
  103.     else {
  104.         if (t->left == NULL || t->right == NULL) {
  105.             node* tmp = t->left ? t->left : t->right;
  106.             if (tmp == NULL) {
  107.                 tmp = t;
  108.                 t = NULL;
  109.             }
  110.             else {
  111.                 *t = *tmp;
  112.             }
  113.             free(tmp);
  114.         }
  115.         else {
  116.             node* tmp = min(t->right);
  117.             t->data = tmp->data;
  118.             t->right = deleteNode(t->right, tmp->data);
  119.         }
  120.     }
  121.     if (t == NULL) return t;
  122.     t->height = 1 + max(height(t->left), height(t->right));
  123.     if (balance(t) > 1 && balance(t->left) >= 0) return rotateright(t);
  124.     if (balance(t) > 1 && balance(t->left) < 0) { t->left = rotateleft(t->left); return rotateright(t); }
  125.     if (balance(t) < -1 && balance(t->right) <= 0) return rotateleft(t);
  126.     if (balance(t) < -1 && balance(t->right) > 0) { t->right = rotateright(t->right); return rotateleft(t); }
  127.     return t;
  128. }
  129.  
  130. node* search(node* root, int key) {
  131.     auto start = std::chrono::steady_clock::now();
  132.     int count = 0;
  133.     if (!root) return 0;
  134.     while (root->data != key) {
  135.         count++;
  136.         if (key < root->data)  root = root->left;
  137.         else root = root->right;
  138.         if (root == NULL) break;
  139.     }
  140.     cout << endl << count;
  141.     auto end = std::chrono::steady_clock::now();
  142.     std::chrono::duration<double> elapsed_seconds = end - start;
  143.     std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
  144.     return root;
  145. }
  146.  
  147. void Print(node* Tree, int l) {
  148.     int i = 0;
  149.     if (Tree != NULL) {
  150.         Print((*Tree).right, l + 1);
  151.  
  152.         for (i = 1; i <= l; i++) cout << "   "; {
  153.             cout << Tree->data << endl;
  154.         }
  155.         Print(((*Tree).left), l + 1);
  156.     }
  157. }
  158.  
  159. node* nodeGetter(int i, node* t) {
  160.     node* tmp = new node();
  161.     string path = "C:/Users/Pavel/Documents/Visual Studio 2019/Projects/SIAOD1.1/data.txt";
  162.     ifstream f(path);
  163.     string output = "";
  164.     string output2 = "";
  165.     for (int x = 0; x < i; x++) {
  166.         getline(f, output, ' ');
  167.         getline(f, output2, ';');
  168.     }
  169.    
  170.     cout << output << " " << output2;
  171.     return push(t, stoi(output), output2);
  172. }
  173.  
  174. node* addall(node* t) {
  175.     node* tmp = new node();
  176.     string path = "C:/Users/Pavel/Desktop/table.txt";
  177.     ifstream aboba(path);
  178.     string output = "";
  179.     string output2 = "";
  180.     while (!aboba.eof()) {
  181.         getline(aboba, output, ' ');
  182.         getline(aboba, output2, ';');
  183.         return push(t, stoi(output), output2);
  184.     }
  185. }
  186.  
  187. int main() {
  188.     setlocale(0, "");
  189.     cout << "\nЧтобы вывести дерево, введите 1.\nЧтобы найти нужное значение, введите 2.\nЧтобы добавить значение в дерево, нажмите 3.\nЧтобы удалить значение из дерева, нажмите 4.\n";
  190.     int n = -1; int s = 0; node* tmp;
  191.     node* tree = NULL;
  192.     while (n != 0) {
  193.         cin >> n;
  194.         switch (n) {
  195.         case 1:
  196.             cout << "Дерево: " << endl;
  197.             Print(tree, 0);
  198.             printf("\n");
  199.             break;
  200.         case 2:
  201.             cout << "Введите значение для поиска: ";
  202.             s = 0;
  203.             cin >> s;
  204.            
  205.             tmp = search(tree, s);
  206.             cout << "Вот, что удалось найти: " << tmp->data << " " << tmp->info << endl;
  207.             printf("\n");
  208.             break;
  209.         case 3:
  210.             cout << "Введите ключ ячейки, которую хотите добавить: ";
  211.             s = 0;
  212.             cin >> s;
  213.             tree = nodeGetter(s, tree);
  214.             printf("\n");
  215.             break;
  216.         case 4:
  217.             cout << "Введите ключ ячейки, которую хотите удалить: ";
  218.             cin >> s;
  219.             tree = deleteNode(tree, s);
  220.             break;
  221.         case 5:
  222.             cin >> s;
  223.             for (int i = 1; i < s; i++) {
  224.                 tree = nodeGetter(i, tree);
  225.             }
  226.             break;
  227.         default:
  228.             break;
  229.         }
  230.     }
  231. }
Add Comment
Please, Sign In to add comment