Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define max(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
  4. struct Node {
  5.     int key, height;
  6.     struct Node *left, *right;
  7. };
  8.  
  9. int height(struct Node *N) {
  10.     if (N == NULL)
  11.         return 0;
  12.     return N->height;
  13. }
  14.  
  15. struct Node *newNode(int key) {
  16.     struct Node* node = (struct Node*)
  17.                         malloc(sizeof(struct Node));
  18.     node->key    = key;
  19.     node->left   = NULL;
  20.     node->right  = NULL;
  21.     node->height = 1;
  22.     return(node);
  23. }
  24.  
  25. struct Node *rightRotate(struct Node *y) {
  26.     struct Node *x = y->left;
  27.     struct Node *T2 = x->right;
  28.  
  29.     x->right = y;
  30.     y->left = T2;
  31.  
  32.     y->height = max(height(y->left), height(y->right))+1;
  33.     x->height = max(height(x->left), height(x->right))+1;
  34.  
  35.     return x;
  36. }
  37.  
  38. struct Node *leftRotate(struct Node *x) {
  39.     struct Node *y = x->right;
  40.     struct Node *T2 = y->left;
  41.  
  42.     y->left = x;
  43.     x->right = T2;
  44.  
  45.     x->height = max(height(x->left), height(x->right))+1;
  46.     y->height = max(height(y->left), height(y->right))+1;
  47.  
  48.     return y;
  49. }
  50.  
  51. int getBalance(struct Node *N) {
  52.     if (N == NULL)
  53.         return 0;
  54.     return height(N->left) - height(N->right);
  55. }
  56.  
  57. struct Node *Balance(struct Node *node, int balance, int key) {
  58.     if (balance > 1 && key < node->left->key)
  59.         return rightRotate(node);
  60.  
  61.     // Right Right Case
  62.     if (balance < -1 && key > node->right->key)
  63.         return leftRotate(node);
  64.  
  65.     // Left Right Case
  66.     if (balance > 1 && key > node->left->key) {
  67.         node->left =  leftRotate(node->left);
  68.         return rightRotate(node);
  69.     }
  70.  
  71.     // Right Left Case
  72.     if (balance < -1 && key < node->right->key) {
  73.         node->right = rightRotate(node->right);
  74.         return leftRotate(node);
  75.     }
  76.  
  77.     return node;
  78. }
  79.  
  80. struct Node* insert(struct Node* node, int key) {
  81.     if (node == NULL)
  82.         return(newNode(key));
  83.  
  84.     if (key < node->key)
  85.         node->left  = insert(node->left, key);
  86.     else if (key > node->key)
  87.         node->right = insert(node->right, key);
  88.     else
  89.         return node;
  90.  
  91.     node->height = 1 + max(height(node->left), height(node->right));
  92.  
  93.     int balance = getBalance(node);
  94.     node = Balance(node, balance, key);
  95.  
  96.     return node;
  97. }
  98.  
  99. struct Node * minValueNode(struct Node* node) {
  100.     struct Node* current = node;
  101.  
  102.     while (current->left != NULL)
  103.         current = current->left;
  104.  
  105.     return current;
  106. }
  107.  
  108. struct Node* deleteNode(struct Node* root, int key) {
  109.     if (root == NULL)
  110.         return root;
  111.  
  112.     if ( key < root->key )
  113.         root->left = deleteNode(root->left, key);
  114.  
  115.     else if( key > root->key )
  116.         root->right = deleteNode(root->right, key);
  117.  
  118.     else {
  119.         // node with only one child or no child
  120.         if( (root->left == NULL) || (root->right == NULL) ) {
  121.             struct Node *temp = root->left ? root->left : root->right;
  122.  
  123.             if (temp == NULL) {
  124.                 temp = root;
  125.                 root = NULL;
  126.             } else {
  127.                 *root = *temp;
  128.             }
  129.             free(temp);
  130.         } else {
  131.             struct Node* temp = minValueNode(root->right);
  132.             root->key = temp->key;
  133.             root->right = deleteNode(root->right, temp->key);
  134.         }
  135.     }
  136.     if (root == NULL)
  137.       return root;
  138.  
  139.     root->height = 1 + max(height(root->left), height(root->right));
  140.  
  141.     int balance = getBalance(root);
  142.     root = Balance(root, balance, key);
  143.  
  144.     return root;
  145. }
  146.  
  147. void print(struct Node *root, int o) {
  148.     if(root != NULL) {
  149.         if(o == 1) printf("%d ", root->key);
  150.         print(root->left, o);
  151.         if(o == 2) printf("%d ", root->key);
  152.         print(root->right, o);
  153.         if(o == 3) printf("%d ", root->key);
  154.     }
  155. }
  156.  
  157. int fastio(char num[]){
  158.     int negative = 0, i = 0, number = 0;
  159.     if (num[0] == '-'){
  160.         negative = 1;
  161.         i++;
  162.     }
  163.  
  164.     for (; num[i] >= 48 && num[i] <= 57 && num[i]; i++)
  165.         number = number * 10 + num[i] - 48;
  166.  
  167.     if (negative)
  168.         number *= -1;
  169.  
  170.     return number;
  171. }
  172.  
  173. int main() {
  174.   struct Node *root = NULL;
  175.  
  176.   long long int x, z;
  177.   char k[100];
  178.   printf("\t\t[MENU]\n_______________________________________\n1 - inserir\n2 - remover\n3 - printar\n0 - sair\n\n"
  179.          "Caso 1 ou 2:\nX para sair\n\nCaso 3:\n1 - preOrder 2 - inOrder 3 - postOrder\n_______________________________________\n\n");
  180.   while(scanf("%lld", &x), x){
  181.       switch(x){
  182.           case 1:
  183.                   while(scanf("%s", k), (k[0] != 'x' && k[0] != 'X')){
  184.                       z = fastio(k);
  185.                       root = insert(root, z);
  186.                   }
  187.                   break;
  188.           case 2:
  189.                   while(scanf("%s", k), (k[0] != 'x' && k[0] != 'X')){
  190.                       z = fastio(k);
  191.                       root = deleteNode(root, z);
  192.                   }
  193.                   break;
  194.           case 3:
  195.                   scanf("%lld", &z);
  196.                   printf("%s: ", z == 1 ? "preOrder\0" : (z == 2 ? "inOrder\0" : "postOrder\0"));
  197.                   print(root, z);
  198.                   printf("\n");
  199.                   break;
  200.       }
  201.   }
  202.  
  203.   free(root);
  204.   return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement