SPLAVL-inl.h 4.8 KB

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