Advertisement
Toxotsist

Сбалансированное дерево поиска

Nov 6th, 2021
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class node {
  5. public:
  6.     int data;
  7.     node* left;
  8.     node* right;
  9.     int height;
  10.     int level;
  11. };
  12.  
  13. node* newNode(int key)
  14. {
  15.     node* t = new node();
  16.     t->data = key;
  17.     t->left = NULL;
  18.     t->right = NULL;
  19.     t->height = 1;
  20.     t->level = 0;
  21.     return t;
  22. }
  23.  
  24. int height(node* t) {
  25.     if (t == NULL) return 0;
  26.     return t->height;
  27. }
  28.  
  29. int max(int a, int b) {
  30.     return (a > b) ? a : b;
  31. }
  32.  
  33. node* min(node* t) {
  34.     node* cur = t;
  35.     while (cur->left != NULL) cur = cur->left;
  36.     return cur;
  37. }
  38.  
  39. int balance(node* t) {
  40.     if (t == NULL) return 0;
  41.     return height(t->left) - height(t->right);
  42. }
  43.  
  44. node* rotateright(node* y)
  45. {
  46.     node* x = y->left;
  47.     node* b = x->right;
  48.     x->right = y;
  49.     y->left = b;
  50.     y->height = max(height(y->left),
  51.         height(y->right)) + 1;
  52.     x->height = max(height(x->left),
  53.         height(x->right)) + 1;
  54.     return x;
  55. }
  56.  
  57. node* rotateleft(node* t)
  58. {
  59.     node* y = t->right;
  60.     node* b = y->left;
  61.     y->left = t;
  62.     t->right = b;
  63.     t->height = max(height(t->left),
  64.         height(t->right)) + 1;
  65.     y->height = max(height(y->left),
  66.         height(y->right)) + 1;
  67.     return y;
  68. }
  69.  
  70. node* push(node* t, int in) {
  71.     if (t == NULL) return newNode(in);
  72.     if (in < t->data)
  73.         t->left = push(t->left, in);
  74.     else if (in > t->data)
  75.         t->right = push(t->right, in);
  76.     else
  77.         return t;
  78.  
  79.     t->height = 1 + max(height(t->left), height(t->right));
  80.     if (balance(t) > 1 && in < t->left->data)
  81.         return rotateright(t);
  82.     if (balance(t) < -1 && in > t->right->data)
  83.         return rotateleft(t);
  84.     if (balance(t) > 1 && in > t->left->data) {
  85.         t->left = rotateleft(t->left);
  86.         return rotateright(t);
  87.     }
  88.     if (balance(t) < -1 && in < t->right->data) {
  89.         t->right = rotateright(t->right);
  90.         return rotateleft(t);
  91.     }
  92.     return t;
  93. }
  94.  
  95. node* deleteNode(node* t, int in) {
  96.     if (t == NULL) return t;
  97.  
  98.     if (in < t->data) t->left = deleteNode(t->left, in);
  99.     else if (in > t->data) t->right = deleteNode(t->right, in);
  100.     else {
  101.         if (t->left == NULL || t->right == NULL) {
  102.             node* tmp = t->left ? t->left : t->right;
  103.             if (tmp == NULL) {
  104.                 tmp = t;
  105.                 t = NULL;
  106.             }
  107.             else {
  108.                 *t = *tmp;
  109.             }
  110.             free(tmp);
  111.         }
  112.         else {
  113.             node* tmp = min(t->right);
  114.             t->data = tmp->data;
  115.             t->right = deleteNode(t->right, tmp->data);
  116.         }
  117.     }
  118.     if (t == NULL) return t;
  119.     t->height = 1 + max(height(t->left), height(t->right));
  120.     if (balance(t) > 1 && balance(t->left) >= 0) return rotateright(t);
  121.     if (balance(t) > 1 && balance(t->left) < 0) { t->left = rotateleft(t->left); return rotateright(t); }
  122.     if (balance(t) < -1 && balance(t->right) <= 0) return rotateleft(t);
  123.     if (balance(t) < -1 && balance(t->right) > 0) { t->right = rotateright(t->right); return rotateleft(t); }
  124.     return t;
  125. }
  126.  
  127. void leveller(node* t, int x) {
  128.     t->level = x;
  129.     if (t->left != NULL)
  130.     {
  131.         leveller(t->left, x + 1);
  132.     }
  133.     if (t->right != NULL)
  134.     {
  135.         leveller(t->right, x + 1);
  136.     }
  137. }
  138.  
  139. void search(node* root, int key) {
  140.     leveller(root, 0);
  141.     if (!root) return;
  142.     while (root->data != key) {
  143.         if (key < root->data)  root = root->left;
  144.         else root = root->right;
  145.         if (root == NULL) break;
  146.     }
  147.     cout << " Уровень узла " << key << " = " << root->level << endl;
  148. }
  149.  
  150. void Print(node* Tree, int l) {
  151.     int i;
  152.     if (Tree != NULL) {
  153.         Print((*Tree).right, l + 1);
  154.  
  155.         for (i = 1; i <= l; i++) cout << "   "; {
  156.             cout << Tree->data << endl;
  157.         }
  158.         Print(((*Tree).left), l + 1);
  159.     }
  160. }
  161.  
  162. int main() {
  163.     setlocale(0, "");
  164.     node* tree = NULL;
  165.     tree = push(tree, 9);
  166.     tree = push(tree, 5);
  167.     tree = push(tree, 10);
  168.     tree = push(tree, 0);
  169.     tree = push(tree, 6);
  170.     tree = push(tree, 11);
  171.     tree = push(tree, -1);
  172.     tree = push(tree, 1);
  173.     tree = push(tree, 2);
  174.     Print(tree, 0);
  175.     search(tree, -1);
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement