SPLAVL-inl.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* SPLAVL-inl.h
  2. * Tahmid Rahman
  3. * Dylan Jeffers
  4. *
  5. * SPLAVL Tree implementation.
  6. */
  7. #include <stdexcept>
  8. #include "library/arrayQueue.h"
  9. /*SPLAVLNode Implementation */
  10. //default constructor
  11. template <typename K, typename V>
  12. SPLAVLNode<K,V>::SPLAVLNode() {
  13. height = - 1;
  14. left = NULL;
  15. right = NULL;
  16. }
  17. // standard constructor
  18. template <typename K, typename V>
  19. SPLAVLNode<K,V>::SPLAVLNode(K k, V v) {
  20. height = 0;
  21. key = k;
  22. value = v;
  23. left = NULL;
  24. right = NULL;
  25. }
  26. /*SPLAVL Implemenation */
  27. //standard constructor
  28. template <typename K, typename V>
  29. SPLAVL<K,V>::SPLAVL() {
  30. size = 0;
  31. root = NULL;
  32. currentCount = 0;
  33. maxCount = 1;
  34. currentRatio = 0;
  35. maxRatio = 1;
  36. }
  37. template <typename K, typename V>
  38. SPLAVL<K,V>::SPLAVL(int maxC, int maxR) {
  39. size = 0;
  40. root = NULL;
  41. currentCount = 0;
  42. maxCount = maxC;
  43. currentRatio = 0;
  44. maxRatio = maxR;
  45. }
  46. template <typename K, typename V>
  47. SPLAVL<K,V>::~SPLAVL() {
  48. traverseAndDelete(root);
  49. }
  50. template <typename K, typename V>
  51. int SPLAVL<K,V>::getSize() {
  52. return size;
  53. }
  54. template <typename K, typename V>
  55. bool SPLAVL<K,V>::isEmpty() {
  56. return size == 0;
  57. }
  58. template <typename K, typename V>
  59. K SPLAVL<K,V>::getMax() {
  60. if (isEmpty()) {
  61. throw std::runtime_error("SPLAVL::getMax called on an empty tree.");
  62. }
  63. return getMaxInSubtree(root);
  64. }
  65. template <typename K, typename V>
  66. K SPLAVL<K,V>::getMin() {
  67. if (isEmpty()) {
  68. throw std::runtime_error("SPLAVL::getMin called on an empty tree.");
  69. }
  70. return getMinInSubtree(root);
  71. }
  72. template <typename K, typename V>
  73. int SPLAVL<K,V>::getHeight() {
  74. if (root == NULL)
  75. return -1;
  76. else
  77. return root->height;
  78. }
  79. template <typename K, typename V>
  80. void SPLAVL<K,V>::setMaxCount(int maxC) {
  81. if(maxC <= 0) {
  82. throw std::runtime_error("maxCount needs to be larger than 0");
  83. }
  84. maxCount = maxC;
  85. }
  86. template <typename K, typename V>
  87. void SPLAVL<K,V>::setMaxRatio(int maxR) {
  88. maxRatio = maxR;
  89. }
  90. template <typename K, typename V>
  91. void SPLAVL<K,V>::insert(K key, V value) {
  92. currentCount++;
  93. if (currentCount >= maxCount){
  94. int height = getHeight();
  95. float size = getSize();
  96. float denom = log (size);
  97. currentRatio = height/denom;
  98. currentCount = 0;
  99. }
  100. root = insertInSubtree(root, key, value);
  101. if (currentRatio > maxRatio){
  102. currentRatio = 0;
  103. }
  104. }
  105. template <typename K, typename V>
  106. void SPLAVL<K,V>::update(K key, V value) {
  107. currentCount++;
  108. if (currentCount >= maxCount){
  109. int height = getHeight();
  110. float size = getSize();
  111. float denom = log (size);
  112. currentRatio = height/denom;
  113. currentCount = 0;
  114. }
  115. //updateInSubtree(root, key, value);
  116. if (contains(key)){
  117. root->value = value;
  118. if (currentRatio > maxRatio){
  119. currentRatio = 0;
  120. }
  121. }
  122. else{
  123. throw std::runtime_error("SPLAVL:update called on nonexistent node");
  124. }
  125. }
  126. template <typename K, typename V>
  127. bool SPLAVL<K,V>::contains(K key) {
  128. currentCount++;
  129. if (currentCount >= maxCount){
  130. int height = getHeight();
  131. float size = getSize();
  132. float denom = log (size);
  133. currentRatio = height/denom;
  134. currentCount = 0;
  135. }
  136. return containsInSubtree(root, key);
  137. if (currentRatio > maxRatio){
  138. currentRatio = 0;
  139. }
  140. }
  141. template <typename K, typename V>
  142. void SPLAVL<K,V>::remove(K key) {
  143. root = removeFromSubtree(root, key);
  144. }
  145. template <typename K, typename V>
  146. V SPLAVL<K,V>::find(K key) {
  147. currentCount++;
  148. if (currentCount >= maxCount){
  149. int height = getHeight();
  150. float size = getSize();
  151. float denom = log (size);
  152. currentRatio = height/denom;
  153. currentCount = 0;
  154. }
  155. if (contains(key)){
  156. if (currentRatio > maxRatio){
  157. currentRatio = 0;
  158. }
  159. return root->value;
  160. }
  161. else{
  162. throw std::runtime_error("SPLAVL:find called on nonexistent node");
  163. }
  164. }
  165. template <typename K, typename V>
  166. Queue< Pair<K,V> >* SPLAVL<K,V>::getPreOrder() {
  167. Queue< Pair<K,V> >* it = new ArrayQueue< Pair<K,V> >();
  168. buildPreOrder(root, it);
  169. return it;
  170. }
  171. template <typename K, typename V>
  172. Queue< Pair<K,V> >* SPLAVL<K,V>::getInOrder() {
  173. Queue< Pair<K,V> >* it = new ArrayQueue< Pair<K,V> >();
  174. buildInOrder(root, it);
  175. return it;
  176. }
  177. template <typename K, typename V>
  178. Queue< Pair<K,V> >* SPLAVL<K,V>::getPostOrder() {
  179. Queue< Pair<K,V> >* it = new ArrayQueue< Pair<K,V> >();
  180. buildPostOrder(root, it);
  181. return it;
  182. }
  183. template <typename K, typename V>
  184. Queue< Pair<K,V> >* SPLAVL<K,V>::getLevelOrder() {
  185. ArrayQueue< SPLAVLNode<K,V>* > levelQ;
  186. Queue< Pair<K,V> >* it = new ArrayQueue< Pair<K,V> >();
  187. levelQ.enqueue(root);
  188. while (!levelQ.isEmpty()) {
  189. SPLAVLNode<K,V>* current = levelQ.dequeue();
  190. if (current != NULL) {
  191. it->enqueue( Pair<K,V>(current->key, current->value) );
  192. levelQ.enqueue(current->left);
  193. levelQ.enqueue(current->right);
  194. }
  195. }
  196. return it;
  197. }
  198. template <typename K, typename V>
  199. K SPLAVL<K,V>::getRootKey() {
  200. return root->key;
  201. }
  202. /* isBalanced- returns true if the tree is balanced
  203. * @return bool: true if the tree is balanced.
  204. */
  205. template <typename K, typename V>
  206. bool SPLAVL<K,V>::isBalanced() {
  207. return isBalancedInSubtree(root);
  208. }