Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- struct node {
- node *l, *r;
- int val, height;
- node() : height(1) {}
- node (int x) : val(x), height(1) {}
- };
- int height(node* tmp) {
- if (tmp == NULL) return 0;
- return tmp->height;
- }
- int findDifference(node *tmp) {
- if (tmp == NULL) return 0;
- return height(tmp->l) - height(tmp->r);
- }
- /*
- node* lRotate(node *cur) {
- node *ri = cur->r;
- node *riri = ri->l;
- ri->l = cur;
- cur->r = riri;
- ri->height = 1 + max(height(ri->l), height(ri->r));
- cur->height = 1 + max(height(cur->l), height(cur->r));
- return ri;
- }
- node *rRotate(node *cur) {
- node *st = cur->l;
- node *strt = st->r;
- st->r = cur;
- cur->l = strt;
- st->height = 1 + max(height(st->l), height(st->r));
- cur->height = 1 + max(height(cur->l), height(cur->r));
- return st;
- }*/
- node *rRotate(node *y)
- {
- node *x = y->l;
- node *T2 = x->r;
- // Perform rotation
- x->r = y;
- y->l = T2;
- // Update heights
- y->height = max(height(y->l),
- height(y->r)) + 1;
- x->height = max(height(x->l),
- height(x->r)) + 1;
- // Return new root
- return x;
- }
- // A utility function to l
- // rotate subtree rooted with x
- // See the diagram given above.
- node *lRotate(node *x)
- {
- node *y = x->r;
- node *T2 = y->l;
- // Perform rotation
- y->l = x;
- x->r = T2;
- // Update heights
- x->height = max(height(x->l),
- height(x->r)) + 1;
- y->height = max(height(y->l),
- height(y->r)) + 1;
- // Return new root
- return y;
- }
- node* insert(node *tmp, int key) {
- if (tmp == NULL) return new node(key);
- if (key < tmp->val) {
- tmp->l = insert(tmp->l,key);
- }
- else if (key > tmp->val) {
- tmp->r = insert(tmp->r, key);
- }
- else return tmp;
- // update height
- tmp->height = 1 + max(height(tmp->l), height(tmp->r));
- // return tmp;
- int balance = findDifference(tmp);
- if (balance > 1) {
- // for LL
- if (key < tmp->l->val) {
- return rRotate(tmp);
- }
- }
- if (balance < -1) {
- if (key > tmp->r->val) {
- return lRotate(tmp);
- }
- }
- if (balance > 1) {
- if (key > tmp->l->val) {
- tmp->l = lRotate(tmp->l);
- return rRotate(tmp);
- }
- }
- if (balance < -1) {
- if (key < tmp->r->val){
- tmp->r = rRotate(tmp->r);
- return lRotate(tmp);
- }
- }
- return tmp;
- }
- void dfs(node *n) {
- if (n != NULL) {
- dfs(n->l); cout << n->val << ' '; dfs(n->r);
- }
- }
- int main() {
- int t;
- cin >> t;
- node *head = NULL;
- for (int i = 1; i <= 50; ++i) {
- int x;
- cin >> x;
- head = insert(head,x);
- }
- dfs(head);
- }
- /*
- // I can't tell you what it really is,
- // I can only tell you what it feels like.
- #include "bits/stdc++.h"
- using namespace std;
- #define int long long
- const int N = 4e5 + 2;
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int n;
- cin >> n;
- set<int>st;
- while(n--) {
- int x;
- cin >> x;
- st.insert(x);
- }
- for (int x:st)cout << x << ' ';
- return 0;
- }
- // 1). Try forming/rearranging the equations if any
- // 2). If problem asks to count, try counting in reverse(it can be easier sometimes)
- */
Add Comment
Please, Sign In to add comment