Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef long long int int64;
- typedef struct node{
- int64 key;
- struct node *left, *right;
- }node;
- int64 count1 = 0, count = 0;
- node *newNode(int64 item){
- node *temp = (node *) malloc(sizeof(node));
- temp->key = item;
- temp->left = temp->right = NULL;
- return temp;
- }
- node *BST_insert(node* root, int64 key){
- if(root == NULL){
- count1++;
- return newNode(key);
- }
- if(key < root->key)
- root->left = BST_insert(root->left, key);
- else if(key > root->key)
- root->right = BST_insert(root->right, key);
- return root;
- }
- node *minValueNode(node* root){
- node *atual = root;
- while(atual->left != NULL)
- atual = atual->left;
- return atual;
- }
- node *BST_delete(node* root, int64 key){
- if (root == NULL) return root;
- if (key < root->key)
- root->left = BST_delete(root->left, key);
- else if (key > root->key)
- root->right = BST_delete(root->right, key);
- else{
- if (root->left == NULL){
- node *temp = root->right;
- free(root);
- return temp;
- }
- else if (root->right == NULL){
- node *temp = root->left;
- free(root);
- return temp;
- }
- node* temp = minValueNode(root->right);
- root->key = temp->key;
- root->right = BST_delete(root->right, temp->key);
- }
- return root;
- }
- node* BST_search(node *root, int64 key){
- node *atual = root;
- while(atual->key != key){
- if(atual != NULL) {
- if(atual->key > key){
- atual = atual->left;
- }
- else {
- atual = atual->right;
- }
- if(atual == NULL){
- return NULL;
- }
- }
- }
- return atual;
- }
- int64 pivot(int64 *d, int64 n){
- int64 pivot[3] = { 0 }, aux = 0, x = 0, y = 0;
- if(n < 3)
- return d[0];
- else if(n == 0)
- return 2;
- else{
- pivot[0] = d[0], pivot[1] = d[(n-1)/2], pivot[2] = d[n-1];
- for(x = 0; x < 2; x++){
- for(y = x + 1; y < 3; y++){
- if(pivot[x] < pivot[y]){
- aux = pivot[x];
- pivot[x] = pivot[y];
- pivot[y] = aux;
- }
- }
- }
- return pivot[1];
- }
- }
- int64 height(node* root){
- if(root == NULL)
- return 0;
- else{
- int64 l = height(root->left);
- int64 r = height(root->right);
- if(l > r)
- return(l+1);
- else return(r+1);
- }
- }
- int64 *congruente(int64 a, int64 c, int64 n, int64 d[], int64 m, int64 seedD){
- int64 linear = 0, x = 0;
- d[0] = seedD;
- for(x = 1; x < n; x++){
- linear = (a*d[x-1] + c)%m;
- d[x] = linear;
- }
- return d;
- }
- int64 *aloca(long long int size){
- int64 *vetor;
- vetor = (int64 *) malloc(sizeof(int64)*size);
- return vetor;
- }
- void D_insert_tree(node *root, int64 *D, int64 n, int64 pivo){
- int64 *s1, *s2, aux = 0, auxi = 0, x = 0, y = 0, aux3 = 0, aux4 = 0;
- s1 = aloca(n);
- s2 = aloca(n);
- if(n == 1){
- root = BST_insert(root, D[0]);
- return;
- }
- else if(n == 2){
- root = BST_insert(root, D[0]);
- root = BST_insert(root, D[1]);
- return;
- }
- for(x = 0; x < n; x++){
- if(pivo > D[x]){
- s2[aux3] = D[x];
- aux3++;
- }
- else{
- if(pivo < D[x]){
- s1[aux4] = D[x];
- aux4++;
- }
- }
- }
- aux = pivot(s1, aux4);
- if(aux == 2)
- return;
- else{
- root = BST_insert(root, aux);
- D_insert_tree(root, s1, aux4, aux);
- }
- auxi = pivot(s2, aux3);
- if(auxi == 2)
- return;
- else{
- root = BST_insert(root, auxi);
- D_insert_tree(root, s2, aux3, auxi);
- }
- }
- void printPostorder(node* root, int64 l, int64 r){
- if (root == NULL)
- return;
- if(l <= r && r < root->key){
- printPostorder(root->left, l, r);
- l++;
- count++;
- }
- else if(l <= r && r > root->key){
- printPostorder(root->right, l, r);
- l++;
- count++;
- }
- root = BST_delete(root, l);
- }
- int main(){
- int64 congru = 0, m = 0, a = 0, c = 0, seedD = 0, *D, pivo = 0, n = 0, q = 0, x = 0, y = 0, l = 0, r = 0, add = 0, aux1 = 0, aux2 = 0;
- char oper[4];
- node *root = NULL, *del = NULL;
- scanf("%lld %lld %lld %lld %lld", &n, &m, &seedD, &a, &c);
- scanf("%lld", &q);
- D = aloca(n);
- D = congruente(a, c, n, D, m, seedD);
- pivo = pivot(D, n);
- root = BST_insert(root, pivo);
- D_insert_tree(root, D, n, pivo);
- printf("0: %lld\n", height(root));
- for(x = 0; x < q; x++){
- scanf("%s", oper);
- if(strcmp(oper, "DEL") == 0){
- scanf("%lld %lld", &l, &r);
- printPostorder(root, l, r);
- /* for(y = l; y <= r; y++){
- del = BST_search(root, y);
- if(del != NULL){
- root = BST_delete(root, y);
- count++;
- }
- }*/
- printf("%lld: %lld\n", x+1, count);
- }
- else{
- scanf("%lld", &add);
- //temp = BST_search(root, add);
- root = BST_insert(root, add);
- if(count1 != 0)
- printf("%lld: 1\n", x+1);
- else{
- printf("%lld: 0\n", x+1);
- }
- }
- count = 0;
- count1 = 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement