Advertisement
atishay11

DAA_Assignment

Mar 6th, 2022 (edited)
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Defining node class
  5.  
  6. class Node {
  7.     public:
  8.     int X, Y;
  9.     Node *left;
  10.     Node *right;
  11.     int height;
  12. };
  13.  
  14. // Height calculation
  15.  
  16. int height(Node *N)
  17. {
  18.     if (N == NULL)
  19.         return 0;
  20.     return N->height;
  21. }
  22.  
  23. //New node creation
  24.  
  25. Node* newNode(int X, int Y)
  26. {
  27.     Node* node = new Node();
  28.     node->X = X;
  29.     node->Y = Y;
  30.     node->left = NULL;
  31.     node->right = NULL;
  32.     node->height = 1;
  33.     return(node);
  34. }
  35.  
  36. //Perform left rotation incase of leftt imbalance i.e. the height of the left subtree is more than the right subtree
  37.  
  38. Node *rightRotate(Node *y)
  39. {
  40.     Node *x = y->left;
  41.     Node *T2 = x->right;
  42.    
  43.     x->right = y;
  44.     y->left = T2;
  45.    
  46.     y->height = max(height(y->left),
  47.                     height(y->right)) + 1;
  48.     x->height = max(height(x->left),
  49.                     height(x->right)) + 1;
  50.                    
  51.     return x;
  52. }
  53.  
  54. //Perform left rotation incase of right imbalance i.e. the height of the right subtree is more than the left subtree
  55.  
  56. Node *leftRotate(Node *x)
  57. {
  58.     Node *y = x->right;
  59.     Node *T2 = y->left;
  60.  
  61.     y->left = x;
  62.     x->right = T2;
  63.  
  64.     x->height = max(height(x->left),
  65.                     height(x->right)) + 1;
  66.     y->height = max(height(y->left),
  67.                     height(y->right)) + 1;
  68.  
  69.     return y;
  70. }
  71.  
  72. /*
  73. Balancing Factor calculation
  74. BF = Height of left subtree - Height of right subtree
  75. Tree is said to be balnced incases where BF lies in the following set : {-1,0,1}
  76. */
  77.  
  78.  
  79. int getBalance(Node *N)
  80. {
  81.     if (N == NULL)
  82.         return 0;
  83.     return height(N->left) - height(N->right);
  84. }
  85.  
  86. // Inserting new point as node while maintaining the balanced nature of the tree
  87.  
  88. Node* insert(Node* node, int X, int Y)
  89. {
  90.     if (node == NULL)
  91.         return(newNode(X, Y));
  92.    
  93.     //Insertion based on x and y values
  94.    
  95.     if (Y < node->Y || Y == node->Y && X > node->X)
  96.         node->left = insert(node->left, X, Y);
  97.     else if (Y > node->Y)
  98.         node->right = insert(node->right, X, Y);
  99.     else
  100.         return node;
  101.    
  102.     // Height recalculation
  103.    
  104.     node->height = 1 + max(height(node->left),
  105.                         height(node->right));
  106.  
  107.     //BF calculation
  108.    
  109.     int balance = getBalance(node);
  110.  
  111.     //Roations based on BF
  112.    
  113.     if (balance > 1 && (Y < node->left->Y || Y == node->left->Y && X > node->left->X))
  114.         return rightRotate(node);
  115.  
  116.     if (balance < -1 && Y > node->right->Y)
  117.         return leftRotate(node);
  118.  
  119.     if (balance > 1 && Y > node->left->Y)
  120.     {
  121.         node->left = leftRotate(node->left);
  122.         return rightRotate(node);
  123.     }
  124.  
  125.     if (balance < -1 && (Y < node->right->Y || Y == node->right->Y && X > node->right->X))
  126.     {
  127.         node->right = rightRotate(node->right);
  128.         return leftRotate(node);
  129.     }
  130.  
  131.     return node;
  132. }
  133.  
  134. //Pre-order traversal of the tree
  135.  
  136. void preOrder(Node *root)
  137. {
  138.     if(root != NULL)
  139.     {
  140.         cout << root->X << " " << root->Y << endl;
  141.         preOrder(root->left);
  142.         preOrder(root->right);
  143.     }
  144. }
  145.  
  146. int main()
  147. {
  148.     Node *root = NULL;
  149.    
  150.     root = insert(root, 10, 1);
  151.     root = insert(root, 20, 2);
  152.     root = insert(root, 30, 3);
  153.     root = insert(root, 40, 4);
  154.     root = insert(root, 50, 3);
  155.     root = insert(root, 25, 2);
  156.  
  157.     preOrder(root);
  158.    
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement