SPLAVL.h 3.5 KB

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