SPLAVL.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef SPLAVL_H_
  2. #define SPLAVL_H_
  3. #include "BST.h"
  4. #include "pair.h"
  5. #include "library/queue.h"
  6. #include "math.h"
  7. // Forward declaration of SPLAVLNode class
  8. template <typename K, typename V> class SPLAVLNode;
  9. template<typename K, typename V>
  10. class SPLAVL: public BST<K,V> {
  11. private:
  12. int size;
  13. SPLAVLNode<K,V>* root;
  14. int currentCount, maxCount;
  15. int currentRatio, maxRatio;
  16. public:
  17. SPLAVL();
  18. ~SPLAVL();
  19. /* All public functions declared/detailed in BST.h */
  20. /* The following methods are defined in SPLAVL-inl.h */
  21. int getSize();
  22. bool isEmpty();
  23. //used for determining SPLAVL starting point
  24. int getHeight();
  25. K getMax();
  26. K getMin();
  27. void setMaxCount(int maxC);
  28. void setMaxRatio(int maxR);
  29. bool isBalanced();
  30. /* dictionary operations */
  31. void insert (K key, V value);
  32. void update (K key, V value);
  33. bool contains (K key);
  34. void remove (K key);
  35. V find (K key);
  36. /* traversal operations */
  37. Queue< Pair<K,V> >* getPreOrder();
  38. Queue< Pair<K,V> >* getInOrder();
  39. Queue< Pair<K,V> >* getPostOrder();
  40. Queue< Pair<K,V> >* getLevelOrder();
  41. K getRootKey();
  42. private:
  43. /*declare our interal private methods */
  44. bool isBalancedInSubtree(SPLAVLNode<K,V>* current);
  45. SPLAVLNode<K,V>* insertInSubtree(SPLAVLNode<K,V>* current, K key, V value);
  46. void updateInSubtree(SPLAVLNode<K,V>* current, K key, V value);
  47. SPLAVLNode<K,V>* removeFromSubtree (SPLAVLNode<K,V>* current, K key);
  48. bool containsInSubtree (SPLAVLNode<K,V>* current, K key);
  49. //SPLAVLNode<K,V>* findInSubtree(SPLAVLNode<K,V>* current, K toSplay);
  50. K getMaxInSubtree(SPLAVLNode<K,V>* current);
  51. K getMinInSubtree(SPLAVLNode<K,V>* current);
  52. void buildPreOrder (SPLAVLNode<K,V>* current, Queue< Pair<K,V> >* it);
  53. void buildInOrder (SPLAVLNode<K,V>* current, Queue< Pair<K,V> >* it);
  54. void buildPostOrder(SPLAVLNode<K,V>* current, Queue< Pair<K,V> >* it);
  55. void traverseAndDelete (SPLAVLNode<K,V>* current);
  56. void computeHeightFromChildren(SPLAVLNode<K,V>* current);
  57. SPLAVLNode<K,V>* balance(SPLAVLNode<K,V>* current);
  58. SPLAVLNode<K,V>* splay(SPLAVLNode<K,V>* current, K key);
  59. /* the six rotations needed to fix each of the six imbalances*/
  60. SPLAVLNode<K,V>* rightRotate(SPLAVLNode<K,V>* current);
  61. SPLAVLNode<K,V>* leftRightRotate(SPLAVLNode<K,V>* current);
  62. SPLAVLNode<K,V>* leftRotate(SPLAVLNode<K,V>* current);
  63. SPLAVLNode<K,V>* rightLeftRotate(SPLAVLNode<K,V>* current);
  64. };
  65. /* SPLAVLNode is templated class that stores data for each node in the
  66. SPLAVL */
  67. template <typename K, typename V>
  68. class SPLAVLNode {
  69. private:
  70. K key;
  71. V value;
  72. //adding height here since we will definitely need it for SPLAVL portion
  73. int height;
  74. /*using parent implementation vs record implementation (ask me if
  75. if you're unsure what this means */
  76. SPLAVLNode<K,V> * left;
  77. SPLAVLNode<K,V> * right;
  78. SPLAVLNode();
  79. SPLAVLNode(K k, V v);
  80. int getHeight();
  81. //so SPLAVL can directly access private aspects of this class
  82. friend class SPLAVL<K,V>;
  83. };
  84. #include "SPLAVL-inl.h"
  85. #include "SPLAVL-private-inl.h"
  86. #endif