Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- struct node {
- int key;
- node *left_child;
- node *right_child;
- node *parent;
- };
- int a[200001][3];
- node *root;
- node *nNode;
- int newIndex = 1;
- int maxhL, maxhR;
- node* rotateright(node* currNode) {
- node* left = currNode->left_child;
- currNode->left_child = left->right_child;
- left->right_child = currNode;
- return left;
- }
- node* rotateleft(node* q) {
- node* p = q->right_child;
- q->right_child = p->left_child;
- p->left_child = q;
- return p;
- }
- void insert(node *curr, int k) {
- if (curr == NULL) {
- nNode = new node;
- nNode->key = k;
- nNode->left_child = 0;
- nNode->right_child = 0;
- } else if (curr->key > k) {
- insert(curr->left_child, k);
- if (curr->left_child == NULL) {
- curr->left_child = nNode;
- nNode->parent = curr;
- }
- } else {
- insert(curr->right_child, k);
- if (curr->right_child == NULL) {
- curr->right_child = nNode;
- nNode->parent = curr;
- }
- }
- }
- void changeNumbers(node* curr, int currNum) {
- a[currNum][0] = curr->key;
- if (curr->left_child != NULL) {
- newIndex++;
- a[currNum][1] = newIndex;
- changeNumbers(curr->left_child, newIndex);
- } else {
- a[currNum][1] = 0;
- }
- if (curr->right_child != NULL) {
- newIndex++;
- a[currNum][2] = newIndex;
- changeNumbers(curr->right_child, newIndex);
- } else {
- a[currNum][2] = 0;
- }
- }
- void heightL(int key, int h) {
- if (a[key][1] != 0) {
- heightL(a[key][1], h + 1);
- } else if (h > maxhL) {
- maxhL = h;
- }
- if (a[key][2] != 0) {
- heightL(a[key][2], h + 1);
- } else if (h > maxhL) {
- maxhL = h;
- }
- }
- void heightR(int key, int h) {
- if (a[key][1] != 0) {
- heightR(a[key][1], h + 1);
- } else if (h > maxhR) {
- maxhR = h;
- }
- if (a[key][2] != 0) {
- heightR(a[key][2], h + 1);
- } else if (h > maxhR) {
- maxhR = h;
- }
- }
- int getBalance(int key) {
- if ((a[key][2] == 0) && (a[key][1] != 0)) {
- maxhL = 1;
- heightL(a[key][1], 1);
- return 0 - maxhL;
- } else if ((a[key][2] != 0) && (a[key][1] == 0)) {
- maxhR = 1;
- heightR(a[key][2], 1);
- return maxhR;
- } else if ((a[key][1] == 0) && (a[key][2] == 0)) {
- return 0;
- } else {
- maxhL = 1;
- maxhR = 1;
- heightL(a[key][1], 1);
- heightR(a[key][2], 1);
- return maxhR - maxhL;
- }
- }
- void balance(node* p, int key) {
- if ((p->left_child != 0) || (p->right_child != 0)) {
- if (getBalance(a[key][2]) < 0)
- root = rotateright(p);
- if (getBalance(a[key][1]) < 0)
- root = rotateleft(p);
- }
- }
- int main() {
- ifstream fin("rotation.in");
- ofstream fout("rotation.out");
- int n, k;
- fin >> n;
- for (int i = 1; i <= n; i++) {
- fin >> a[i][0];
- fin >> a[i][1];
- fin >> a[i][2];
- insert(root, a[i][0]);
- // if (root == NULL) root = nNode;
- }
- fin >> k;
- insert(root, k);
- n ++;
- for (int i = n; i > 0; i--) {
- balance(root, i);
- }
- fout << n << endl;
- changeNumbers(root, newIndex);
- for (int i = 1; i <= n ; i++) {
- fout << a[i][0] << " ";
- fout << a[i][1] << " ";
- fout << a[i][2] << endl;
- }
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement