#include <stdio.h>
#include <stdlib.h>
typedef struct ND {
struct ND * leftchd;
struct ND * rightchd;
int height;
int data;
} node;
typedef struct QUE {
struct ND * cur;
struct QUE * next;
} queue;
typedef node * pn;
node * insertnd(node * np,int a);
node * removnode(node * np,int value);
node * minnode(node * np);
node * LLRotate(node * np);
node * RRRotate(node * np);
int max(node * x,node * y);
int getheight(node * np);
void traverse(queue * front,queue * tail);
int main() {
node * root = NULL;
int n = 0;
scanf("%d",&n);
root = insertnd(root,n);
while(n != -1) {
scanf("%d",&n);
if(n != -1)
root = insertnd(root,n);
}
queue * front = NULL,* tail = NULL;
front = (queue *)malloc(sizeof(queue));
tail = NULL;
tail = front;
front->cur = root;
front->next = NULL;
traverse(front,tail);
root = removnode(root,4);
traverse(front,tail);
return 0;
}
node * insertnd(node * np, int a) {
if(np == NULL) {
np = (node *)malloc(sizeof(node));
np->data = a;
np->leftchd = NULL;
np->rightchd = NULL;
np->height = 1;
return np;
}
if(a < np->data)
np->leftchd = insertnd(np->leftchd,a);
else if(a > np->data) {
np->rightchd = insertnd(np->rightchd,a);
} else return np;
np->height = max(np->leftchd,np->rightchd) + 1;
int balance = getbalance(np);
//LL
if(balance > 1&&getbalance(np->leftchd) > 0)
return LLRotate(np);
//LR
else if(balance > 1&&getbalance(np->leftchd) < 0) {
np->leftchd = RRRotate(np->leftchd);
return LLRotate(np->leftchd);
//RR
} else if(balance < -1&&getbalance(np->rightchd) < 0)
return RRRotate(np);
//RL
else if(balance < -1&&getbalance(np->rightchd) > 0) {
np->rightchd = LLRotate(np->rightchd);
return RRRotate(np);
}
return np;
}
node * removnode(node * np,int value) {
if(np == NULL) {
return np;
}
if(np->data == value) {
if(np->leftchd == NULL||np->rightchd == NULL) {
node * temp = NULL;
if(np->leftchd == temp)
temp = np->rightchd;
else temp = np->leftchd;
if(temp == NULL)
np = NULL;
else np = temp;
} else {
node * temp = NULL;
temp = minnode(np->rightchd);
np->data = temp->data;
np->rightchd = removnode(np->rightchd,temp->data);
}
} else if(value < np->data) {
np->leftchd = removnode(np->leftchd,value);
} else {
np->rightchd = removnode(np->rightchd,value);
}
if(np == NULL) return np;
np->height = max(np->leftchd,np->rightchd) + 1;
int balance = getbalance(np);
//LL
if(balance > 1&&getbalance(np->leftchd) > 0) {
return LLRotate(np);
//LR
} else if(balance > 1&&getbalance(np->leftchd) < 0) {
np->leftchd = RRRotate(np->leftchd);
return LLRotate(np->leftchd);
//RR
} else if(balance < -1&&getbalance(np->rightchd) < 0) {
return RRRotate(np);
//RL
} else if(balance < -1&&getbalance(np->rightchd) > 0) {
np->rightchd = LLRotate(np->rightchd);
return RRRotate(np);
}
return np;
}
int getbalance(node * np) {
return getheight(np->leftchd) - getheight(np->rightchd);
}
int getheight(node * np) {
if(np == NULL) {
return 0;
}
return np->height;
}
int max(node * x,node * y) {
if(x == NULL&&y == NULL)
return 0;
else if(x != NULL&&y == NULL)
return x->height;
else if(y != NULL&&x == NULL)
return y->height;
else
return x->height >= y->height?x->height:y->height;
}
node * minnode(node * np) {
if(np->leftchd != NULL)
minnode(np->leftchd);
else return np;
}
node * LLRotate(node * np) {
node * temp = NULL;
node * temp2 = NULL;
temp = np;
temp2 = np->leftchd;
temp->leftchd = temp2->rightchd;
temp2->rightchd = temp;
temp->height = max(temp->leftchd,temp->rightchd) + 1;
temp2->height = max(temp2->leftchd,temp2->rightchd) + 1;
return temp2;
}
node * RRRotate(node * np) {
node * temp = NULL;
node * temp2 = NULL;
temp = np;
temp2 = np->rightchd;
temp->rightchd = temp2->leftchd;
temp2->leftchd = temp;
temp->height = max(temp->leftchd,temp->rightchd) + 1;
temp2->height = max(temp2->leftchd,temp2->rightchd) + 1;
return temp2;
}
void traverse(queue * front,queue * tail) {
if(front == NULL) return;
if(front->cur->leftchd != NULL) {
tail->next = (queue *)malloc(sizeof(queue));
tail->next->next = NULL;
tail = tail->next;
tail->cur = front->cur->leftchd;
}
if(front->cur->rightchd != NULL) {
tail->next = (queue *)malloc(sizeof(queue));
tail->next->next = NULL;
tail = tail->next;
tail->cur = front->cur->rightchd;
}
printf("%d %d\n",front->cur->data,front->cur->height);
front = front->next;
traverse(front,tail);
}