Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <cmath>
- using namespace std;
- struct node {
- int key;
- node *l, *r, *p;
- int h;
- };
- struct things {
- int key, l, r;
- node *q;
- int lh, rh, h;
- int flag;
- };
- int balf(vector < things > &arr, int i)
- {
- if ( i < 0)
- {
- return -1;
- }
- arr[i].lh = 1 + balf(arr, arr[i].l);
- arr[i].rh = 1 + balf(arr, arr[i].r);
- arr[i].h = arr[i].rh - arr[i].lh;
- arr[i].q->h = max(arr[i].lh, arr[i].rh);
- return max(arr[i].lh, arr[i].rh);
- }
- int mheight(node *peak)
- {
- if ( peak == NULL)
- {
- return -1;
- }
- return max(1 + mheight(peak->l), 1 + mheight(peak->r));
- }
- void bigleft(node *peak, node *&root)
- {
- if (peak->p == NULL || peak == root)//root
- {
- root = peak->r->l;
- peak->r->l->p = NULL;
- } else if (peak->p->l == peak) //wl
- {
- peak->p->l = peak->r->l;
- peak->r->l->p = peak->p;
- } else if (peak->p->r == peak) //wr
- {
- peak->p->r = peak->r->l;
- peak->r->l->p = peak->p;
- }
- node *tmpl = peak->r->l->l;
- node *tmpr = peak->r->l->r;
- peak->r->l->l = peak;
- peak->r->l->r = peak->r;
- peak->p = peak->r->l;
- peak->r->p = peak->r->l;
- peak->p->r->l = tmpr;
- peak->p->l->r = tmpl;
- }
- void smallleft(node *peak, node *&root)
- {
- if (peak->p == NULL || peak == root) //root
- {
- root = peak->r;
- peak->r->p = NULL;
- } else if (peak->p->l == peak) //wl
- {
- peak->p->l = peak->r;
- peak->r->p = peak->p;
- } else if (peak->p->r == peak) //wr
- {
- peak->p->r = peak->r;
- peak->r->p = peak->p;
- }
- node *tmpl = peak->r->l;
- peak->r->l = peak;
- peak->p = peak->r;
- peak->r = tmpl;
- }
- void setf(node *peak, vector < things > &res, int i)
- {
- if (peak != NULL)
- {
- if (peak->l != NULL)
- {
- res[2*i + 1].key = peak->l->key;
- res[2*i + 1].flag = 0;
- if (peak->l->l == NULL)
- {
- res[2*i + 1].l = -1;
- } else {
- res[2*i + 1].l = 2*(2*i+1)+1;
- }
- if (peak->l->r == NULL)
- {
- res[2*i + 1].r = -1;
- } else {
- res[2*i + 1].r = 2*(2*i+1)+2;
- }
- setf(peak->l, res, i + 1);
- }
- if (peak->r != NULL)
- {
- res[2*i + 2].key = peak->r->key;
- res[2*i + 2].flag = 0;
- if (peak->r->l == NULL)
- {
- res[2*i + 2].l = -1;
- } else {
- res[2*i + 2].l = 2*(2*i+2)+1;
- }
- if (peak->r->r == NULL)
- {
- res[2*i + 2].r = -1;
- } else {
- res[2*i + 2].r = 2*(2*i+2)+2;
- }
- setf(peak->r, res, i + 2);
- }
- }
- }
- int arrsize(int loop)
- {
- int c = 0;
- int remain = 1;
- if (loop == 0)
- {
- return remain;
- } else {
- while (c != loop)
- {
- c++;
- remain = remain + pow(2, c);
- }
- return remain;
- }
- }
- int main()
- {
- ifstream fin("rotation.in");
- ofstream fout("rotation.out");
- vector < things > arr;
- int n;
- int m;
- fin >> n;
- m = n;
- if (n > 1)
- {
- arr.resize(n);
- int key, num1, num2;
- node *parent = NULL;
- for (int i = 0; i < n; i++)
- {
- fin >> key >> num1 >> num2;
- arr[i].key = key;
- arr[i].l = --num1;
- arr[i].r = --num2;
- node *q = NULL;
- q = new node();
- q->key = key;
- q->l = NULL;
- q->r = NULL;
- q->p = parent;
- arr[i].q = q;
- parent = q;
- }
- for (int i = 0; i < n; i++)
- {
- arr[i].q->l = arr[arr[i].l].q;
- arr[i].q->r = arr[arr[i].r].q;
- }
- node *root = arr[0].q;
- for (int i = 0; i < n; i++)
- {
- // cout << arr[i].q->key<< " "<< arr[i].l<< " "<< arr[i].r<<endl;
- }
- // cout << "array filled\n";
- balf(arr, 0);
- for (int i = 0; i < n; i++)
- {
- if (arr[i].h > 1)
- {
- if (arr[arr[i].r].h == -1)
- {
- bigleft(root, root);
- } else {
- smallleft(root, root);
- }
- i = n +1;
- }
- }
- int size = arrsize(mheight(root));
- things some;
- some.flag = 1;
- vector < things > res (size, some);
- setf(root, res, 0);
- fout << n << endl;
- res[0].key = root->key;
- res[0].flag = 0;
- if (root->l == NULL && root->l == NULL)
- {
- res[0].l = -1;
- res[0].l = -1;
- } else {
- res[0].l = 1;
- res[0].r = 2;
- if (root->l == NULL)
- {
- res[0].l = -1;
- }
- if (root->r == NULL)
- {
- res[0].r = -1;
- }
- }
- int j = 3;
- int c = 0;
- for (int i = 0; i < size; i++)
- {
- if (res[i].flag == 0)
- {
- c++;
- if (i > 0 && c <= (n/2))
- {
- if (res[i].l != -1 && res[i].r != -1)
- {
- res[i].l = j;
- res[i].r = j + 1;
- j = j + 2;
- } else if (res[i].l == -1 && res[i].r != -1)
- {
- res[i].r = j;
- j = j + 1;
- } else if (res[i].l != -1 && res[i].r == -1)
- {
- res[i].l = j;
- j = j + 1;
- }
- }
- fout << res[i].key<<" "<< ++res[i].l<<" " << ++res[i].r <<endl;
- }
- }
- } else {
- if (n == 0)
- {
- fout << 0<<endl;
- } else if (n == 1)
- {
- fout << n<<endl;
- int key;
- fin >> key;
- fout<< key<<" "<<0<<" "<<0<<endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement