AVLTree-private-inl.h 11 KB

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