AVLTree-private-inl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*Dylan Jeffers
  2. *Tahmid Rahman
  3. *founders of RahmROMJeffers ltd.
  4. */
  5. /*
  6. * This recursive helper function inserts a key-value pair into a subtree
  7. * of the tree, or throws a runtime_error if the key is already present.
  8. */
  9. template <typename K, typename V>
  10. AVLTreeNode<K,V>*
  11. AVLTree<K,V>::insertInSubtree(AVLTreeNode<K,V>* current, K key, V value) {
  12. if (current == NULL){
  13. size++;
  14. return new AVLTreeNode<K, V>(key, value);
  15. }
  16. else if (key == current->key){
  17. throw std::runtime_error("AVLTree::insertInSubtree" \
  18. "called on key already in tree.");
  19. }
  20. else if (key < current->key){
  21. current->left = insertInSubtree(current->left, key, value);
  22. }
  23. else if (key > current->key){
  24. current->right = insertInSubtree(current->right, key, value);
  25. }
  26. return balance(current);
  27. }
  28. /**
  29. * This recursive helper function updates key-value pair in the subtree
  30. * of the tree, or throws a runtime_error if the key is not present.
  31. */
  32. template <typename K, typename V>
  33. void AVLTree<K,V>::updateInSubtree(AVLTreeNode<K,V>* current, K key, V value) {
  34. if (current == NULL){
  35. throw std::runtime_error("Key not found in AVLTree::updateInSubtree.");
  36. }
  37. else if (key == current->key){
  38. current->value = value;
  39. }
  40. else if (key < current->key){
  41. updateInSubtree(current->left, key, value);
  42. }
  43. else if (key > current->key){
  44. updateInSubtree(current->right, key, value);
  45. }
  46. return;
  47. }
  48. /**
  49. * This recursive helper function removes a key-value pair from a subtree
  50. * of the tree, or throws a runtime_error if that key was not present.
  51. *
  52. * It returns a pointer to the root of the subtree. This root is often
  53. * the node that was passed as an argument to the function (current) but
  54. * might be a different node if current contains the key we are removing
  55. * from the tree.
  56. */
  57. template <typename K, typename V>
  58. AVLTreeNode<K,V>*
  59. AVLTree<K,V>::removeFromSubtree(AVLTreeNode<K,V>* current,
  60. K key) {
  61. if (current == NULL) {
  62. throw std::runtime_error("AVLTree::remove called on key not in tree.");
  63. }
  64. else if (key == current->key) { // We've found the node to remove
  65. if ((current->left == NULL) && (current->right == NULL)) {
  66. size--;
  67. delete current;
  68. return NULL;
  69. }
  70. else if (current->left == NULL) {
  71. AVLTreeNode<K,V>* tempNode = current->right;
  72. delete current;
  73. size--;
  74. return balance(tempNode);
  75. }
  76. else if (current->right == NULL) {
  77. AVLTreeNode<K,V>* tempNode = current->left;
  78. delete current;
  79. size--;
  80. return balance(tempNode);
  81. }
  82. else {
  83. AVLTreeNode<K,V>* minimum = current->right;
  84. while (minimum->left != NULL) {
  85. minimum = minimum->left;
  86. }
  87. current->key = minimum->key;
  88. current->value = minimum->value;
  89. current->right = removeFromSubtree(current->right, current->key);
  90. }
  91. }
  92. else if (key < current->key) {
  93. current->left = removeFromSubtree(current->left, key);
  94. }
  95. else {
  96. current->right = removeFromSubtree(current->right, key);
  97. }
  98. return balance(current);
  99. }
  100. /**
  101. * Returns true if a key is contained in a subtree of the tree, and
  102. * false otherwise.
  103. */
  104. template <typename K, typename V>
  105. bool AVLTree<K,V>::containsInSubtree(AVLTreeNode<K,V>* current, K key) {
  106. if (current == NULL){
  107. return false;
  108. }
  109. else if (key == current->key){
  110. return true;
  111. }
  112. else if (key < current->key){
  113. return containsInSubtree(current->left, key);
  114. }
  115. else {
  116. return containsInSubtree(current->right, key);
  117. }
  118. }
  119. /**
  120. * Given a key, returns the value for that key from a subtree of the tree.
  121. * Throws a runtime_error if the key is not in the subtree.
  122. */
  123. template <typename K, typename V>
  124. V AVLTree<K,V>::findInSubtree(AVLTreeNode<K,V>* current, K key) {
  125. if (current == NULL) {
  126. throw std::runtime_error("LinkedBS::findInSubtree called on an empty tree");
  127. }
  128. else if (key == current->key) {
  129. return current->value;
  130. }
  131. else if (key < current->key) {
  132. return findInSubtree(current->left, key);
  133. }
  134. else {
  135. return findInSubtree(current->right, key);
  136. }
  137. }
  138. /**
  139. * Returns the largest key in a subtree of the tree.
  140. */
  141. template <typename K, typename V>
  142. K AVLTree<K,V>::getMaxInSubtree(AVLTreeNode<K,V>* current) {
  143. if (current->right == NULL) {
  144. return current->key;
  145. }
  146. return getMaxInSubtree(current->right);
  147. }
  148. /**
  149. * Returns the smallest key in a subtree of the tree.
  150. */
  151. template <typename K, typename V>
  152. K AVLTree<K,V>::getMinInSubtree(AVLTreeNode<K,V>* current) {
  153. if (current->left == NULL) {
  154. return current->key;
  155. }
  156. return getMinInSubtree(current->left);
  157. }
  158. /*
  159. * Returns the height of a subtree of the tree, or -1 if the subtree
  160. * is empty.
  161. *
  162. template <typename K, typename V>
  163. int AVLTree<K,V>::getHeightOfSubtree(AVLTreeNode<K,V>* current) {
  164. if (current == NULL) {
  165. return -1;
  166. }
  167. int l = getHeightOfSubtree(current->left);
  168. int r = getHeightOfSubtree(current->right);
  169. if (l >= r) {
  170. return ++l;
  171. }
  172. else
  173. return ++r;
  174. }
  175. */
  176. /**
  177. * Recursively builds a post-order iterator for a subtree of the tree.
  178. */
  179. template <typename K, typename V>
  180. void AVLTree<K,V>::buildPostOrder(AVLTreeNode<K,V>* current,
  181. Queue< Pair<K,V> >* it) {
  182. if (current == NULL) {
  183. return;
  184. }
  185. buildPostOrder(current->left, it);
  186. buildPostOrder(current->right, it);
  187. it->enqueue( Pair<K,V>(current->key, current->value) );
  188. }
  189. /**
  190. * Recursively builds a pre-order iterator for a subtree of the tree.
  191. */
  192. template <typename K, typename V>
  193. void AVLTree<K,V>::buildPreOrder(AVLTreeNode<K,V>* current,
  194. Queue< Pair<K,V> >* it) {
  195. if (current == NULL){
  196. return;
  197. }
  198. it->enqueue( Pair<K,V>(current->key, current->value) );
  199. buildPreOrder(current->left, it);
  200. buildPreOrder(current->right, it);
  201. }
  202. /**
  203. * Recursively builds an in-order iterator for a subtree of the tree.
  204. */
  205. template <typename K, typename V>
  206. void AVLTree<K,V>::buildInOrder(AVLTreeNode<K,V>* current,
  207. Queue< Pair<K,V> >* it) {
  208. if (current == NULL){
  209. return;
  210. }
  211. buildInOrder(current->left, it);
  212. it->enqueue( Pair<K,V>(current->key, current->value) );
  213. buildInOrder(current->right, it);
  214. }
  215. /**
  216. * Performs a post-order traversal of the tree, deleting each node from the
  217. * heap after we have already traversed its children.
  218. */
  219. template <typename K, typename V>
  220. void AVLTree<K,V>::traverseAndDelete(AVLTreeNode<K,V>* current) {
  221. if (current == NULL) {
  222. return; //nothing to delete
  223. }
  224. traverseAndDelete(current->left);
  225. traverseAndDelete(current->right);
  226. delete current;
  227. }
  228. /**
  229. * Returns true if balance factor of subtree is between -1 and 1 (inclusive)
  230. * If a child is NULL, we treat the child's height as -1
  231. */
  232. template<typename K, typename V>
  233. bool AVLTree<K,V>::isBalancedInSubtree(AVLTreeNode<K,V>* current) {
  234. int leftHeight, rightHeight;
  235. if (current == NULL){
  236. return true;
  237. }
  238. else{
  239. if (current->left == NULL) {
  240. leftHeight = -1;
  241. }
  242. else {
  243. leftHeight = current->left->height;
  244. }
  245. if (current->right == NULL) {
  246. rightHeight = -1;
  247. }
  248. else {
  249. rightHeight = current->right->height;
  250. }
  251. int balanceFactor = leftHeight - rightHeight;
  252. if (balanceFactor >= 2 || balanceFactor <= -2) {
  253. return false;
  254. }
  255. else {
  256. return true;
  257. }
  258. }
  259. }
  260. /**
  261. * Computes height of a node from heights of children
  262. * BROUGHT TO YOU BY A HEALTHY COMPUTATION FROM RAHMROMJEFFERS
  263. * If a child is NULL, we treat the child's height as -1
  264. */
  265. template<typename K, typename V>
  266. void AVLTree<K,V>::computeHeightFromChildren(AVLTreeNode<K,V>* current) {
  267. int leftHeight, rightHeight;
  268. if (current->left == NULL) {
  269. leftHeight = -1;
  270. }
  271. else {
  272. leftHeight = current->left->height;
  273. }
  274. if (current->right == NULL) {
  275. rightHeight = -1;
  276. }
  277. else {
  278. rightHeight = current->right->height;
  279. }
  280. if (leftHeight >= rightHeight) {
  281. current->height = leftHeight + 1;
  282. }
  283. else {
  284. current->height = rightHeight + 1;
  285. }
  286. }
  287. /* The four rotations needed to fix each of the four possible imbalances
  288. * in an AVLTree
  289. * (1) Right rotation for a left-left imbalance
  290. * (2) Left rotation for a right-right imbalance
  291. * (3) LeftRight rotation for left-right imbalance
  292. * (4) RightLeft rotation for a right-left imbalance
  293. */
  294. template<typename K, typename V>
  295. AVLTreeNode<K,V>* AVLTree<K,V>::rightRotate(AVLTreeNode<K,V>* current) {
  296. AVLTreeNode<K,V>* b = current;
  297. AVLTreeNode<K,V>* d = current->left;
  298. current = d;
  299. b->left = d->right;
  300. d->right = b;
  301. computeHeightFromChildren(b);
  302. computeHeightFromChildren(current);
  303. return current;
  304. }
  305. template<typename K, typename V>
  306. AVLTreeNode<K,V>* AVLTree<K,V>::leftRightRotate(AVLTreeNode<K,V>* current) {
  307. current->left = leftRotate(current->left);
  308. return rightRotate(current);
  309. }
  310. template<typename K, typename V>
  311. AVLTreeNode<K,V>* AVLTree<K,V>::leftRotate(AVLTreeNode<K,V>* current) {
  312. AVLTreeNode<K,V>* b = current;
  313. AVLTreeNode<K,V>* d = current->right;
  314. current = d;
  315. b->right = d->left;
  316. d->left = b;
  317. computeHeightFromChildren(b);
  318. computeHeightFromChildren(current);
  319. return current;
  320. }
  321. template<typename K, typename V>
  322. AVLTreeNode<K,V>* AVLTree<K,V>::rightLeftRotate(AVLTreeNode<K,V>* current) {
  323. current->right = rightRotate(current->right);
  324. return leftRotate(current);
  325. }
  326. /**
  327. * Balances subtree with current as root, identifying the imbalance and calling
  328. * the proper rotation method
  329. */
  330. template<typename K, typename V>
  331. AVLTreeNode<K,V>* AVLTree<K,V>::balance(AVLTreeNode<K,V>* current) {
  332. if (current == NULL) {
  333. return current;
  334. }
  335. else {
  336. computeHeightFromChildren(current);
  337. int leftHeight, rightHeight, balanceFactor;
  338. if (current->left == NULL) {
  339. leftHeight = -1;
  340. }
  341. else {
  342. leftHeight = current->left->height;
  343. }
  344. if (current->right == NULL) {
  345. rightHeight = -1;
  346. }
  347. else {
  348. rightHeight = current->right->height;
  349. }
  350. balanceFactor = leftHeight - rightHeight;
  351. if (balanceFactor >= 2){ // left imbalance
  352. int left_right, left_left;
  353. if (current->left->left == NULL) {
  354. left_left = -1;
  355. }
  356. else {
  357. left_left = current->left->left->height;
  358. }
  359. if (current->left->right == NULL) {
  360. left_right = -1;
  361. }
  362. else {
  363. left_right = current->left->right->height;
  364. }
  365. if (left_left >= left_right){
  366. return rightRotate(current);
  367. }
  368. else{
  369. return leftRightRotate(current);
  370. }
  371. }
  372. else if (balanceFactor <= -2) { // right imbalance
  373. int right_right, right_left;
  374. if (current->right->right == NULL) {
  375. right_right = -1;
  376. }
  377. else {
  378. right_right = current->right->right->height;
  379. }
  380. if (current->right->left == NULL) {
  381. right_left = -1;
  382. }
  383. else {
  384. right_left = current->right->left->height;
  385. }
  386. if(right_right >= right_left){
  387. return leftRotate(current);
  388. }
  389. else{
  390. return rightLeftRotate(current);
  391. }
  392. }
  393. }
  394. return current;
  395. }