Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3.  
  4. struct node {
  5.     int key;
  6.     node *left_child;
  7.     node *right_child;
  8.     node *parent;
  9. };
  10.  
  11. int a[200001][3];
  12. node *root;
  13. node *nNode;
  14. int newIndex = 1;
  15. int maxhL, maxhR;
  16.  
  17.  
  18. node* rotateright(node* currNode) {
  19.     node* left = currNode->left_child;
  20.     currNode->left_child = left->right_child;
  21.     left->right_child = currNode;
  22.     return left;
  23. }
  24.  
  25. node* rotateleft(node* q) {
  26.     node* p = q->right_child;
  27.     q->right_child = p->left_child;
  28.     p->left_child = q;
  29.     return p;
  30. }
  31.  
  32.  
  33.  
  34. void insert(node *curr, int k) {
  35.     if (curr == NULL) {
  36.         nNode = new node;
  37.         nNode->key = k;
  38.         nNode->left_child = 0;
  39.         nNode->right_child = 0;
  40.     } else if (curr->key > k) {
  41.         insert(curr->left_child, k);
  42.         if (curr->left_child == NULL) {
  43.             curr->left_child = nNode;
  44.             nNode->parent = curr;
  45.         }
  46.     } else {
  47.         insert(curr->right_child, k);
  48.         if (curr->right_child == NULL) {
  49.             curr->right_child = nNode;
  50.             nNode->parent = curr;
  51.         }
  52.     }
  53. }
  54.  
  55. void changeNumbers(node* curr, int currNum) {
  56.     a[currNum][0] = curr->key;
  57.     if (curr->left_child != NULL) {
  58.         newIndex++;
  59.         a[currNum][1] = newIndex;
  60.         changeNumbers(curr->left_child, newIndex);
  61.     } else {
  62.         a[currNum][1] = 0;
  63.     }
  64.     if (curr->right_child != NULL) {
  65.         newIndex++;
  66.         a[currNum][2] = newIndex;
  67.         changeNumbers(curr->right_child, newIndex);
  68.     } else {
  69.         a[currNum][2] = 0;
  70.     }
  71. }
  72.  
  73. void heightL(int key, int h) {
  74.     if (a[key][1] != 0) {
  75.         heightL(a[key][1], h + 1);
  76.     } else if (h > maxhL) {
  77.         maxhL = h;
  78.     }
  79.     if (a[key][2] != 0) {
  80.         heightL(a[key][2], h + 1);
  81.     } else if (h > maxhL) {
  82.         maxhL = h;
  83.     }
  84. }
  85.  
  86. void heightR(int key, int h) {
  87.     if (a[key][1] != 0) {
  88.         heightR(a[key][1], h + 1);
  89.     } else if (h > maxhR) {
  90.         maxhR = h;
  91.     }
  92.     if (a[key][2] != 0) {
  93.         heightR(a[key][2], h + 1);
  94.     } else if (h > maxhR) {
  95.         maxhR = h;
  96.     }
  97. }
  98.  
  99. int getBalance(int key) {
  100.     if ((a[key][2] == 0) && (a[key][1] != 0)) {
  101.         maxhL = 1;
  102.         heightL(a[key][1], 1);
  103.         return 0 - maxhL;
  104.     } else if ((a[key][2] != 0) && (a[key][1] == 0)) {
  105.         maxhR = 1;
  106.         heightR(a[key][2], 1);
  107.         return maxhR;
  108.     } else if ((a[key][1] == 0) && (a[key][2] == 0)) {
  109.         return 0;
  110.     } else {
  111.         maxhL = 1;
  112.         maxhR = 1;
  113.         heightL(a[key][1], 1);
  114.         heightR(a[key][2], 1);
  115.         return maxhR - maxhL;
  116.     }
  117. }
  118.  
  119. void balance(node* p, int key) {
  120.     if ((p->left_child != 0) || (p->right_child != 0)) {
  121.         if (getBalance(a[key][2]) < 0)
  122.             root = rotateright(p);
  123.         if (getBalance(a[key][1]) < 0)
  124.             root = rotateleft(p);
  125.     }
  126. }
  127.  
  128.  
  129.  
  130. int main() {
  131.     ifstream fin("rotation.in");
  132.     ofstream fout("rotation.out");
  133.     int n, k;
  134.     fin >> n;
  135.  
  136.     for (int i = 1; i <= n; i++) {
  137.         fin >> a[i][0];
  138.         fin >> a[i][1];
  139.         fin >> a[i][2];
  140.         insert(root, a[i][0]);
  141.  //       if (root == NULL) root = nNode;
  142.     }
  143.     fin >> k;
  144.     insert(root, k);
  145.     n ++;
  146.     for (int i = n; i > 0; i--) {
  147.         balance(root, i);
  148.     }
  149.     fout << n << endl;
  150.     changeNumbers(root, newIndex);
  151.  
  152.     for (int i = 1; i <= n ; i++) {
  153.         fout << a[i][0] << " ";
  154.         fout << a[i][1] << " ";
  155.         fout << a[i][2] << endl;
  156.     }
  157.     fin.close();
  158.     fout.close();
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement