Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. Node *left;
  2. Node *right;
  3. Node *parent;
  4. int height; // add this one
  5.  
  6.  
  7. int max(int a, int b)
  8. {
  9.     return (a > b)? a : b;
  10. }
  11.  
  12. Node * minValueNode(Node* node)
  13. {
  14.     Node* current = node;
  15.  
  16.     while (current->left != NULL)
  17.         current = current->left;
  18.  
  19.     return current;
  20. }
  21.  
  22. int getBalance(Node *N)
  23. {
  24.     if (N == NULL)
  25.         return 0;
  26.     return height(N->left) - height(N->right);
  27. }
  28.  
  29. Node *leftRotate(Node *x)
  30. {
  31.     Node *y = x->right;
  32.     Node *T2 = y->left;
  33.  
  34.     y->left = x;
  35.     x->right = T2;
  36.  
  37.     x->height = max(height(x->left), height(x->right)) + 1;
  38.     y->height = max(height(y->left), height(y->right)) + 1;
  39.  
  40.     return y;
  41. }
  42.  
  43. Node *rightRotate(Node *y)
  44. {
  45.     Node *x = y->left;
  46.     Node *T2 = x->right;
  47.  
  48.     x->right = y;
  49.     y->left = T2;
  50.  
  51.     y->height = max(height(y->left),
  52.                     height(y->right)) + 1;
  53.     x->height = max(height(x->left),
  54.                     height(x->right)) + 1;
  55.  
  56.     return x;
  57. }
  58.  
  59. Node* deleteNodes(Node* root, int key)
  60. {
  61.     if (root == NULL)
  62.         return root;
  63.  
  64.     if ( key < root->key )
  65.         root->left = deleteNodes(root->left, key);
  66.     else if( key > root->key )
  67.         root->right = deleteNodes(root->right, key);
  68.     else
  69.     {
  70.         if( (root->left == NULL) ||
  71.             (root->right == NULL) )
  72.         {
  73.             Node *temp = root->left ? root->left : root->right;
  74.  
  75.             if (temp == NULL)
  76.             {
  77.                 temp = root;
  78.                 root = NULL;
  79.             }
  80.             else
  81.             *root = *temp;
  82.             free(temp);
  83.         }
  84.         else
  85.         {
  86.             Node* temp = minValueNode(root->right);
  87.  
  88.             root->key = temp->key;
  89.  
  90.             root->right = deleteNodes(root->right, temp->key);
  91.         }
  92.     }
  93.  
  94.     if (root == NULL)
  95.     return root;
  96.  
  97.     root->height = 1 + max(height(root->left), height(root->right));
  98.  
  99.     int balance = getBalance(root);
  100.  
  101.     if (balance > 1 && getBalance(root->left) >= 0)
  102.         return rightRotate(root);
  103.  
  104.     if (balance > 1 && getBalance(root->left) < 0)
  105.     {
  106.         root->left = leftRotate(root->left);
  107.         return rightRotate(root);
  108.     }
  109.  
  110.     if (balance < -1 && getBalance(root->right) <= 0)
  111.         return leftRotate(root);
  112.  
  113.     if (balance < -1 && getBalance(root->right) > 0)
  114.     {
  115.         root->right = rightRotate(root->right);
  116.         return leftRotate(root);
  117.     }
  118.  
  119.     return root;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement