Advertisement
Toxotsist

Сиаод 3 Вариант 7

Oct 16th, 2021 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5.     unsigned char data;
  6.     node* left, * right;
  7.     unsigned char height;
  8.     int level;
  9.     node(char in) { data = in; left = right = NULL; height = 1; level = 0; }
  10. };
  11.  
  12. void Print(node* Tree, int l) {
  13.     int i;
  14.     if (Tree != NULL) {
  15.         Print((*Tree).right, l + 1);
  16.  
  17.         for (i = 1; i <= l; i++) cout << "   "; {
  18.             cout << Tree->data << endl;
  19.         }
  20.         Print(((*Tree).left), l + 1);
  21.     }
  22. }
  23.  
  24. void print(node* t, int u) {
  25.     //cout << "PRINT!\n";
  26.     if (t == NULL) {
  27.         //`cout << "T = NULL!\n";
  28.         return;
  29.     }
  30.     else {
  31.         print(t->left, u++);
  32.         for (int i = 0; i < u; i++) cout << "|";
  33.         cout << t->data << endl;
  34.         u--;
  35.     };
  36.     print(t->right, u++);
  37. }
  38.  
  39. unsigned char height(node* t) {
  40.     if (t) {
  41.         return t->height;
  42.     }
  43.     else {
  44.         return 0;
  45.     }
  46. }
  47.  
  48. void fixheight(node* p) {
  49.     unsigned char hl = height(p->left);
  50.     unsigned char hr = height(p->right);
  51.     if (hl > hr) p->height = hl + 1;
  52.     else p->height = hr + 1;
  53. }
  54.  
  55. int bf(node* p) {
  56.     return height(p->right) - height(p->left);
  57. }
  58.  
  59. node* rotateright(node* p) {
  60.     node* q = p->left;
  61.     p->left = q->right;
  62.     q->right = p;
  63.     fixheight(p);
  64.     fixheight(q);
  65.     return q;
  66. }
  67.  
  68. node* rotateleft(node* q) {
  69.     node* p = q->right;
  70.     q->right = p->left;
  71.     p->left = q;
  72.     fixheight(q);
  73.     fixheight(p);
  74.     return p;
  75. }
  76.  
  77. node* balance(node* p) {
  78.     //cout << "BALANCE!\n";
  79.     fixheight(p);
  80.     if (bf(p) == 2) {
  81.         if (bf(p->right) < 0) p->right = rotateright(p->right);
  82.         return rotateleft(p);
  83.     }
  84.     if (bf(p) == -2) {
  85.         if (bf(p->left) > 0) p->left = rotateleft(p->left);
  86.         return rotateright(p);
  87.     }
  88.     return p;
  89. }
  90.  
  91. node* push(unsigned char in, node** t) {
  92.     if ((*t) == NULL) {
  93.         (*t) = new node(in); return balance((*t));
  94.     }
  95.     if (in < (*t)->data) { (*t)->left = push(in, &(*t)->left); }
  96.     else { (*t)->right = push(in, &(*t)->right);;
  97.         }
  98.     return balance((*t));
  99. }
  100.  
  101. void leveller(node* t, int x) {
  102.     t->level = x;
  103.     if (t->left != NULL)
  104.     {
  105.         leveller(t->left, x + 1);
  106.     }
  107.     if (t->right != NULL)
  108.     {
  109.         leveller(t->right, x + 1);
  110.     }
  111. }
  112.  
  113. void search(node* root, unsigned char key) {
  114.     leveller(root, 0);
  115.     if (!root) return;
  116.     while (root->data != key) {
  117.         if (key < root->data)  root = root->left;
  118.         else root = root->right;
  119.         if (root == NULL) break;
  120.     }
  121.     cout << " Уровень узла " << key << " = " << root->level << endl;
  122. }
  123.  
  124. void leftsum(node* t) {
  125.     int count = 0;
  126.     node* tmp = t;
  127.     while (tmp != NULL) {
  128.         count++;
  129.         tmp = tmp->left;
  130.     } count--;
  131.     cout << "Количество символов в левой ветви = " << count << endl;
  132. }
  133.  
  134. int main() {
  135.     setlocale(0, "");
  136.     int n;
  137.     unsigned char s;
  138.     node* tree = NULL;
  139.     cout << "Введите количество элементов > "; cin >> n;
  140.     for (int i = 0; i < n; i++) {
  141.         cout << "Введите число >";
  142.         cin >> s;
  143.         push(s, &tree);
  144.     }
  145.     cout << "Дерево:\n";
  146.     Print(tree, 0);
  147.     n = -1;
  148.     cout << "\nЧтобы вывести дерево вертикально, введите 1.\nЧтобы определить уровень, на котором находится нужное значение, введите 2.\n Чтобы Определить количество цифр в левом поддереве исходного дерева, введите 3.\nЧтобы завершить выполнение программы, введите 0.\n";
  149.     while (n != 0) {
  150.         cin >> n;
  151.         switch (n) {
  152.         case 1:
  153.             print(tree, 0);
  154.             break;
  155.         case 2:
  156.             cout << "Введите значение для поиска: ";
  157.             s = 0;
  158.             cin >> s;
  159.             search(tree, s);
  160.             break;
  161.         case 3:
  162.             leftsum(tree);
  163.             break;
  164.         default:
  165.             break;
  166.         }
  167.     }
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement