AVLCommands.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "AVLCommand.h"
  2. // examples of AVL tree algorithms:
  3. // https://tfetimes.com/c-avl-tree/
  4. // https://stackoverflow.com/questions/29866315/class-for-avl-trees-c
  5. // http://www.sanfoundry.com/cpp-program-implement-avl-trees/
  6. // AVL explanation:
  7. // http://www.btechsmartclass.com/DS/U5_T2.html
  8. // combination of geeksforgeeks AVL insert and delete code
  9. // written for C
  10. // => convert into a C++ class
  11. //constructor: builds an AVL tree from BST tree bst
  12. AVLCommands::AVLCommands() {
  13. BST bst(); // initialize BST with root null and sized 0
  14. }
  15. // An AVL tree node
  16. struct Node
  17. {
  18. int key;
  19. struct Node *left;
  20. struct Node *right;
  21. int height;
  22. };
  23. // A utility function to get maximum of two integers
  24. int max(int a, int b);
  25. // A utility function to get height of the tree
  26. int height(struct Node *N) {
  27. if (N == NULL)
  28. return 0;
  29. return N->height;
  30. }
  31. // A utility function to get maximum of two integers
  32. int max(int a, int b) {
  33. return (a > b)? a : b;
  34. }
  35. /* Helper function that allocates a new node with the given key and
  36. NULL left and right pointers. */
  37. struct Node* newNode(int key)
  38. {
  39. struct Node* node = (struct Node*)
  40. malloc(sizeof(struct Node));
  41. node->key = key;
  42. node->left = NULL;
  43. node->right = NULL;
  44. node->height = 1; // new node is initially added at leaf
  45. return(node);
  46. }
  47. // A utility function to right rotate subtree rooted with y
  48. // See the diagram given above.
  49. struct Node *rightRotate(struct Node *y)
  50. {
  51. struct Node *x = y->left;
  52. struct Node *T2 = x->right;
  53. // Perform rotation
  54. x->right = y;
  55. y->left = T2;
  56. // Update heights
  57. y->height = max(height(y->left), height(y->right))+1;
  58. x->height = max(height(x->left), height(x->right))+1;
  59. // Return new root
  60. return x;
  61. }
  62. // A utility function to left rotate subtree rooted with x
  63. // See the diagram given above.
  64. struct Node *leftRotate(struct Node *x)
  65. {
  66. struct Node *y = x->right;
  67. struct Node *T2 = y->left;
  68. // Perform rotation
  69. y->left = x;
  70. x->right = T2;
  71. // Update heights
  72. x->height = max(height(x->left), height(x->right))+1;
  73. y->height = max(height(y->left), height(y->right))+1;
  74. // Return new root
  75. return y;
  76. }
  77. // Get Balance factor of node N
  78. int getBalance(struct Node *N)
  79. {
  80. if (N == NULL)
  81. return 0;
  82. return height(N->left) - height(N->right);
  83. }
  84. // Recursive function to insert key in subtree rooted
  85. // with node and returns new root of subtree.
  86. struct Node* insert(struct Node* node, int key)
  87. {
  88. /* 1. Perform the normal BST insertion */
  89. if (node == NULL)
  90. return(newNode(key));
  91. if (key < node->key)
  92. node->left = insert(node->left, key);
  93. else if (key > node->key)
  94. node->right = insert(node->right, key);
  95. else // Equal keys are not allowed in BST
  96. return node;
  97. /* 2. Update height of this ancestor node */
  98. node->height = 1 + max(height(node->left),
  99. height(node->right));
  100. /* 3. Get the balance factor of this ancestor
  101. node to check whether this node became
  102. unbalanced */
  103. int balance = getBalance(node);
  104. // If this node becomes unbalanced, then
  105. // there are 4 cases
  106. // Left Left Case
  107. if (balance > 1 && key < node->left->key)
  108. return rightRotate(node);
  109. // Right Right Case
  110. if (balance < -1 && key > node->right->key)
  111. return leftRotate(node);
  112. // Left Right Case
  113. if (balance > 1 && key > node->left->key)
  114. {
  115. node->left = leftRotate(node->left);
  116. return rightRotate(node);
  117. }
  118. // Right Left Case
  119. if (balance < -1 && key < node->right->key)
  120. {
  121. node->right = rightRotate(node->right);
  122. return leftRotate(node);
  123. }
  124. /* return the (unchanged) node pointer */
  125. return node;
  126. }
  127. /* Given a non-empty binary search tree, return the
  128. node with minimum key value found in that tree.
  129. Note that the entire tree does not need to be
  130. searched. */
  131. struct Node * minValueNode(struct Node* node)
  132. {
  133. struct Node* current = node;
  134. /* loop down to find the leftmost leaf */
  135. while (current->left != NULL)
  136. current = current->left;
  137. return current;
  138. }
  139. // Recursive function to delete a node with given key
  140. // from subtree with given root. It returns root of
  141. // the modified subtree.
  142. struct Node* deleteNode(struct Node* root, int key)
  143. {
  144. // STEP 1: PERFORM STANDARD BST DELETE
  145. if (root == NULL)
  146. return root;
  147. // If the key to be deleted is smaller than the
  148. // root's key, then it lies in left subtree
  149. if ( key < root->key )
  150. root->left = deleteNode(root->left, key);
  151. // If the key to be deleted is greater than the
  152. // root's key, then it lies in right subtree
  153. else if( key > root->key )
  154. root->right = deleteNode(root->right, key);
  155. // if key is same as root's key, then This is
  156. // the node to be deleted
  157. else
  158. {
  159. // node with only one child or no child
  160. if( (root->left == NULL) || (root->right == NULL) )
  161. {
  162. struct Node *temp = root->left ? root->left :
  163. root->right;
  164. // No child case
  165. if (temp == NULL)
  166. {
  167. temp = root;
  168. root = NULL;
  169. }
  170. else // One child case
  171. *root = *temp; // Copy the contents of
  172. // the non-empty child
  173. free(temp);
  174. }
  175. else
  176. {
  177. // node with two children: Get the inorder
  178. // successor (smallest in the right subtree)
  179. struct Node* temp = minValueNode(root->right);
  180. // Copy the inorder successor's data to this node
  181. root->key = temp->key;
  182. // Delete the inorder successor
  183. root->right = deleteNode(root->right, temp->key);
  184. }
  185. }
  186. // If the tree had only one node then return
  187. if (root == NULL)
  188. return root;
  189. // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
  190. root->height = 1 + max(height(root->left),
  191. height(root->right));
  192. // STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to
  193. // check whether this node became unbalanced)
  194. int balance = getBalance(root);
  195. // If this node becomes unbalanced, then there are 4 cases
  196. // Left Left Case
  197. if (balance > 1 && getBalance(root->left) >= 0)
  198. return rightRotate(root);
  199. // Left Right Case
  200. if (balance > 1 && getBalance(root->left) < 0)
  201. {
  202. root->left = leftRotate(root->left);
  203. return rightRotate(root);
  204. }
  205. // Right Right Case
  206. if (balance < -1 && getBalance(root->right) <= 0)
  207. return leftRotate(root);
  208. // Right Left Case
  209. if (balance < -1 && getBalance(root->right) > 0)
  210. {
  211. root->right = rightRotate(root->right);
  212. return leftRotate(root);
  213. }
  214. return root;
  215. }